Skip to content

Commit 8f4df6b

Browse files
author
orrym
committed
Add XML, JavaConfig and Autowired examples.
1 parent fab4aec commit 8f4df6b

13 files changed

Lines changed: 212 additions & 0 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.beaninjectiontypes.autowiredexample;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
@ComponentScan
10+
public class BeanInjectorAutowiredExample {
11+
12+
public static void main(String[] args) {
13+
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanInjectorAutowiredExample.class);
14+
SimpleAutowiredBean simpleBean = (SimpleAutowiredBean) ctx.getBean("simpleAutowiredBean");
15+
simpleBean.doSomething();
16+
}
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.beaninjectiontypes.autowiredexample;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class SimpleAutowiredBean {
8+
9+
@Autowired
10+
private SimpleAutowiredDependency simpleAutowiredDependency;
11+
12+
public void doSomething() {
13+
System.out.println("I'm a Simple Bean. I'm doing something!");
14+
simpleAutowiredDependency.doSomethingElse();
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.beaninjectiontypes.autowiredexample;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class SimpleAutowiredDependency {
7+
8+
public void doSomethingElse() {
9+
System.out.println("I'm a simple autowired dependency! I'm doing something!");
10+
}
11+
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.beaninjectiontypes.javaconfigexample;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class BeanInjectorConfig {
8+
9+
@Bean
10+
public SimpleDependency simpleDependency() {
11+
return new SimpleDependency();
12+
}
13+
14+
// The following illustrates constructor injection:
15+
16+
@Bean
17+
public SimpleBeanConstructorInjection simpleBeanConstructorInjection() {
18+
return new SimpleBeanConstructorInjection(simpleDependency());
19+
}
20+
21+
// The following illustrates setter injection:
22+
23+
@Bean
24+
public SimpleBeanSetterInjection simpleBeanSetterInjection() {
25+
SimpleBeanSetterInjection simpleBeanSetterInjection = new SimpleBeanSetterInjection();
26+
simpleBeanSetterInjection.setSimpleDependency(new SimpleDependency());
27+
return simpleBeanSetterInjection;
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.beaninjectiontypes.javaconfigexample;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
public class BeanInjectorJavaConfigExample {
7+
8+
public static void main(String[] args) {
9+
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanInjectorConfig.class);
10+
SimpleBeanConstructorInjection simpleBeanConstructorInjection = (SimpleBeanConstructorInjection) ctx.getBean("simpleBeanConstructorInjection");
11+
simpleBeanConstructorInjection.doSomething();
12+
13+
System.out.println("******************");
14+
15+
SimpleBeanSetterInjection simpleBeanSetterInjection = (SimpleBeanSetterInjection) ctx.getBean("simpleBeanSetterInjection");
16+
simpleBeanSetterInjection.doSomething();
17+
}
18+
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.beaninjectiontypes.javaconfigexample;
2+
3+
public class SimpleBeanConstructorInjection {
4+
5+
private SimpleDependency simpleDependency;
6+
7+
SimpleBeanConstructorInjection(SimpleDependency simpleDependency) {
8+
this.simpleDependency = simpleDependency;
9+
}
10+
11+
public void doSomething() {
12+
System.out.println("I'm a Simple Bean. My dependency is wired using constructor injection - I'm doing something!");
13+
simpleDependency.doSomethingElse();
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.beaninjectiontypes.javaconfigexample;
2+
3+
public class SimpleBeanSetterInjection {
4+
5+
private SimpleDependency simpleDependency;
6+
7+
public void doSomething() {
8+
System.out.println("I'm a Simple Bean. My dependency is wired using setter injection - I'm doing something!");
9+
simpleDependency.doSomethingElse();
10+
}
11+
12+
public void setSimpleDependency(SimpleDependency simpleDependency) {
13+
this.simpleDependency = simpleDependency;
14+
}
15+
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.beaninjectiontypes.javaconfigexample;
2+
3+
public class SimpleDependency {
4+
5+
public void doSomethingElse() {
6+
System.out.println("I'm a simple dependency! I'm doing something (else)!");
7+
}
8+
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.beaninjectiontypes.xmlconfigexample;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
public class BeanInjectorXMLExample {
7+
8+
public static void main(String[] args) {
9+
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-injector.xml");
10+
SimpleBeanConstructorInjection simpleBeanConstructorInjection = (SimpleBeanConstructorInjection) ctx.getBean("simpleBeanConstructorInjection");
11+
simpleBeanConstructorInjection.doSomething();
12+
13+
System.out.println("********************");
14+
SimpleBeanSetterInjection simpleBeanSetterInjection = (SimpleBeanSetterInjection) ctx.getBean("simpleBeanSetterInjection");
15+
simpleBeanSetterInjection.doSomething();
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.beaninjectiontypes.xmlconfigexample;
2+
3+
public class SimpleBeanConstructorInjection {
4+
5+
private SimpleDependency simpleDependency;
6+
7+
SimpleBeanConstructorInjection(SimpleDependency simpleDependency) {
8+
this.simpleDependency = simpleDependency;
9+
}
10+
11+
public void doSomething() {
12+
System.out.println("I'm a Simple Bean. My dependency is wired using constructor injection - I'm doing something!");
13+
simpleDependency.doSomethingElse();
14+
}
15+
16+
}

0 commit comments

Comments
 (0)