package com.luo.spring.guides.iocdi.xml.constructorconfusion;
import org.springframework.context.support.GenericXmlApplicationContext;
/**
* @author : archer
* @date : Created in 2022/11/30 14:46
* @description :
*/
public class ConstructorConfusion {
private String someValue;
public ConstructorConfusion(String someValue) {
System.out.println("ConstructorConfusion(String) called");
this.someValue = someValue;
}
public ConstructorConfusion(int someValue) {
System.out.println("ConstructorConfusion(int) called");
this.someValue = "Number: " + Integer.toString(someValue);
}
public String toString() {
return someValue;
}
}
app-context-xml.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="constructorConfusion"
class="com.luo.spring.guides.iocdi.xml.constructorconfusion.ConstructorConfusion">
<constructor-arg type="int">
<value>90value>
constructor-arg>
bean>
beans>
测试
package com.luo.spring.guides.iocdi.xml.constructorconfusion;
import org.springframework.context.support.GenericXmlApplicationContext;
/**
* @author : archer
* @date : Created in 2022/11/30 14:51
* @description :
*/
public class Main {
public static void main(String... args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:iocdi/app-context.xml");
ctx.refresh();
ConstructorConfusion cc = (ConstructorConfusion) ctx.getBean("constructorConfusion");
System.out.println(cc);
ctx.close();
}
}
输出
ConstructorConfusion(int) called
Number: 90
package com.luo.spring.guides.iocdi.xml.setter;
public class Dependency {
@Override
public String toString() {
return "Dependency{}";
}
}
package com.luo.spring.guides.iocdi.xml.setter;
public class SetterInjection {
private Dependency dependency;
public void setDependency(Dependency dependency) {
this.dependency = dependency;
}
@Override
public String toString() {
return dependency.toString();
}
}
app-context-xml.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dependency" class="com.luo.spring.guides.iocdi.xml.setter.Dependency"/>
<bean id="setterInjection" class="com.luo.spring.guides.iocdi.xml.setter.SetterInjection"
p:dependency-ref="dependency"/>
beans>
测试
package com.luo.spring.guides.iocdi.xml.setter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : archer
* @date : Created in 2022/11/30 15:22
* @description :
*/
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext
("iocdi/injection/setter/app-context-xml.xml");
SetterInjection mr = ctx.getBean("setterInjection", SetterInjection.class);
System.out.println(mr);
}
}
输出
Dependency{}
package com.luo.spring.guides.iocdi.annotation.constructorconfusion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Service;
@Service("constructorConfusion")
public class ConstructorConfusion {
private String someValue;
public ConstructorConfusion(String someValue) {
System.out.println("ConstructorConfusion(String) called");
this.someValue = someValue;
}
@Autowired
public ConstructorConfusion(@Value("90") int someValue) {
System.out.println("ConstructorConfusion(int) called");
this.someValue = "Number: " + Integer.toString(someValue);
}
public String toString() {
return someValue;
}
}
app-context-annotation.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="com.luo.spring.guides.iocdi.annotation"/>
<bean id="message" class="java.lang.String"
c:_0="I hope that someone gets my message in a bottle"/>
<bean id="message2" class="java.lang.String"
c:_0="I know I won't be injected :("/>
beans>
测试
package com.luo.spring.guides.iocdi.annotation.constructorconfusion;
import org.springframework.context.support.GenericXmlApplicationContext;
/**
* @author : archer
* @date : Created in 2022/11/30 14:51
* @description :
*/
public class Main {
public static void main(String... args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:iocdi/app-context-annotation.xml");
ctx.refresh();
ConstructorConfusion cc = (ConstructorConfusion) ctx.getBean("constructorConfusion");
System.out.println(cc);
ctx.close();
}
}
输出
ConstructorConfusion(int) called
Number: 90
package com.luo.spring.guides.iocdi.annotation.setter;
import org.springframework.stereotype.Component;
@Component("dependency")
public class Dependency {
@Override
public String toString() {
return "Dependency{}";
}
}
package com.luo.spring.guides.iocdi.annotation.setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("setterInjection")
public class SetterInjection {
private Dependency dependency;
@Autowired
public void setDependency(Dependency dependency) {
this.dependency = dependency;
}
@Override
public String toString() {
return dependency.toString();
}
}
配置类
package com.luo.spring.guides.iocdi.annotation.setter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author : archer
* @date : Created in 2022/11/30 15:32
* @description :
*/
@ComponentScan(basePackages = {"com.luo.spring.guides.iocdi.annotation"})
@Configuration
public class SetterInjectionConfiguration {
}
测试
package com.luo.spring.guides.iocdi.annotation.setter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author : archer
* @date : Created in 2022/11/30 15:33
* @description :
*/
public class Main {
public static void main(String... args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext
(SetterInjectionConfiguration.class);
SetterInjection mr = ctx.getBean("setterInjection", SetterInjection.class);
System.out.println(mr);
}
}
输出
Dependency{}
package com.luo.spring.guides.iocdi.annotation.field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by iuliana.cosmina on 2/15/17.
*/
@Service("singer")
public class Singer {
@Autowired
private Inspiration inspirationBean;
public void sing() {
System.out.println("... " + inspirationBean.getLyric());
}
}
package com.luo.spring.guides.iocdi.annotation.field;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* Created by iuliana.cosmina on 2/15/17.
*/
@Component
public class Inspiration {
private String lyric = "I can keep the door cracked open, to let light through";
public Inspiration(@Value("For all my running, I can understand") String lyric) {
this.lyric = lyric;
}
public String getLyric() {
return lyric;
}
public void setLyric(String lyric) {
this.lyric = lyric;
}
}
测试
package com.luo.spring.guides.iocdi.annotation.field;
import com.luo.spring.guides.iocdi.annotation.InjectionConfiguration;
import com.luo.spring.guides.iocdi.annotation.setter.SetterInjection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
/**
* @author : archer
* @date : Created in 2022/11/30 20:46
* @description :
*/
public class Main {
public static void main(String... args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext
(InjectionConfiguration.class);
Singer singer = ctx.getBean("singer", Singer.class);
Singer singerBean = ctx.getBean(Singer.class);
System.out.println(singer == singerBean);
singerBean.sing();
singer.sing();
}
}
输出
ConstructorConfusion(int) called
true
… For all my running, I can understand
… For all my running, I can understand