Add XML, JavaConfig and Autowired examples.
This commit is contained in:
parent
fab4aec7a1
commit
8f4df6b903
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.beaninjectiontypes.autowiredexample;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class BeanInjectorAutowiredExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanInjectorAutowiredExample.class);
|
||||
SimpleAutowiredBean simpleBean = (SimpleAutowiredBean) ctx.getBean("simpleAutowiredBean");
|
||||
simpleBean.doSomething();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.beaninjectiontypes.autowiredexample;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SimpleAutowiredBean {
|
||||
|
||||
@Autowired
|
||||
private SimpleAutowiredDependency simpleAutowiredDependency;
|
||||
|
||||
public void doSomething() {
|
||||
System.out.println("I'm a Simple Bean. I'm doing something!");
|
||||
simpleAutowiredDependency.doSomethingElse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.beaninjectiontypes.autowiredexample;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SimpleAutowiredDependency {
|
||||
|
||||
public void doSomethingElse() {
|
||||
System.out.println("I'm a simple autowired dependency! I'm doing something!");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.beaninjectiontypes.javaconfigexample;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class BeanInjectorConfig {
|
||||
|
||||
@Bean
|
||||
public SimpleDependency simpleDependency() {
|
||||
return new SimpleDependency();
|
||||
}
|
||||
|
||||
// The following illustrates constructor injection:
|
||||
|
||||
@Bean
|
||||
public SimpleBeanConstructorInjection simpleBeanConstructorInjection() {
|
||||
return new SimpleBeanConstructorInjection(simpleDependency());
|
||||
}
|
||||
|
||||
// The following illustrates setter injection:
|
||||
|
||||
@Bean
|
||||
public SimpleBeanSetterInjection simpleBeanSetterInjection() {
|
||||
SimpleBeanSetterInjection simpleBeanSetterInjection = new SimpleBeanSetterInjection();
|
||||
simpleBeanSetterInjection.setSimpleDependency(new SimpleDependency());
|
||||
return simpleBeanSetterInjection;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.beaninjectiontypes.javaconfigexample;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class BeanInjectorJavaConfigExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanInjectorConfig.class);
|
||||
SimpleBeanConstructorInjection simpleBeanConstructorInjection = (SimpleBeanConstructorInjection) ctx.getBean("simpleBeanConstructorInjection");
|
||||
simpleBeanConstructorInjection.doSomething();
|
||||
|
||||
System.out.println("******************");
|
||||
|
||||
SimpleBeanSetterInjection simpleBeanSetterInjection = (SimpleBeanSetterInjection) ctx.getBean("simpleBeanSetterInjection");
|
||||
simpleBeanSetterInjection.doSomething();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.beaninjectiontypes.javaconfigexample;
|
||||
|
||||
public class SimpleBeanConstructorInjection {
|
||||
|
||||
private SimpleDependency simpleDependency;
|
||||
|
||||
SimpleBeanConstructorInjection(SimpleDependency simpleDependency) {
|
||||
this.simpleDependency = simpleDependency;
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
System.out.println("I'm a Simple Bean. My dependency is wired using constructor injection - I'm doing something!");
|
||||
simpleDependency.doSomethingElse();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.beaninjectiontypes.javaconfigexample;
|
||||
|
||||
public class SimpleBeanSetterInjection {
|
||||
|
||||
private SimpleDependency simpleDependency;
|
||||
|
||||
public void doSomething() {
|
||||
System.out.println("I'm a Simple Bean. My dependency is wired using setter injection - I'm doing something!");
|
||||
simpleDependency.doSomethingElse();
|
||||
}
|
||||
|
||||
public void setSimpleDependency(SimpleDependency simpleDependency) {
|
||||
this.simpleDependency = simpleDependency;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.beaninjectiontypes.javaconfigexample;
|
||||
|
||||
public class SimpleDependency {
|
||||
|
||||
public void doSomethingElse() {
|
||||
System.out.println("I'm a simple dependency! I'm doing something (else)!");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.beaninjectiontypes.xmlconfigexample;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class BeanInjectorXMLExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-injector.xml");
|
||||
SimpleBeanConstructorInjection simpleBeanConstructorInjection = (SimpleBeanConstructorInjection) ctx.getBean("simpleBeanConstructorInjection");
|
||||
simpleBeanConstructorInjection.doSomething();
|
||||
|
||||
System.out.println("********************");
|
||||
SimpleBeanSetterInjection simpleBeanSetterInjection = (SimpleBeanSetterInjection) ctx.getBean("simpleBeanSetterInjection");
|
||||
simpleBeanSetterInjection.doSomething();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.beaninjectiontypes.xmlconfigexample;
|
||||
|
||||
public class SimpleBeanConstructorInjection {
|
||||
|
||||
private SimpleDependency simpleDependency;
|
||||
|
||||
SimpleBeanConstructorInjection(SimpleDependency simpleDependency) {
|
||||
this.simpleDependency = simpleDependency;
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
System.out.println("I'm a Simple Bean. My dependency is wired using constructor injection - I'm doing something!");
|
||||
simpleDependency.doSomethingElse();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.beaninjectiontypes.xmlconfigexample;
|
||||
|
||||
public class SimpleBeanSetterInjection {
|
||||
|
||||
private SimpleDependency simpleDependency;
|
||||
|
||||
public void setSimpleDependency(SimpleDependency simpleDependency) {
|
||||
this.simpleDependency = simpleDependency;
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
System.out.println("I'm a Simple Bean. My dependency is wired using setter injection - I'm doing something!");
|
||||
simpleDependency.doSomethingElse();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.beaninjectiontypes.xmlconfigexample;
|
||||
|
||||
public class SimpleDependency {
|
||||
|
||||
public void doSomethingElse() {
|
||||
System.out.println("I'm a simple dependency! I'm doing something (else)!");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="simpleDependency"
|
||||
class="com.baeldung.beaninjectiontypes.xmlconfigexample.SimpleDependency" />
|
||||
|
||||
<bean id="simpleBeanConstructorInjection"
|
||||
class="com.baeldung.beaninjectiontypes.xmlconfigexample.SimpleBeanConstructorInjection">
|
||||
<constructor-arg ref="simpleDependency" />
|
||||
</bean>
|
||||
|
||||
<bean id="simpleBeanSetterInjection"
|
||||
class="com.baeldung.beaninjectiontypes.xmlconfigexample.SimpleBeanSetterInjection">
|
||||
<property name="simpleDependency" ref="simpleDependency" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
Loading…
Reference in New Issue