在Spring,“通过自动检测自动装配”,“通过构造函数自动装配”是指选,如果默认构造函数(参数与任何数据类型),以其他方式使用“按类型自动装配”。
请参见Spring “通过自动检测自动装配”的一个例子。自动装配(对"panda" bean 的实现基地)的“kungfu” Bean 到 “panda”,通过构造函数或类型。
1. AutoDetect – 按构造函数装配
如果默认的构造函数提供,自动检测将构造自动装配。
package com.gp6.autoAssemblyBean.AutoDetect;
public class KungFu {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Language [name=" + name + "]";
}
}
package com.gp6.autoAssemblyBean.AutoDetect;
public class Panda {
private KungFu kungfu;
public Panda(KungFu kungfu) {
System.out.println("autowiring by constructor");
this.kungfu = kungfu;
}
public KungFu getKungfu() {
return kungfu;
}
public void setKungfu(KungFu kungfu) {
System.out.println("autowiring by type");
this.kungfu = kungfu;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="panda" class="com.gp6.autoAssemblyBean.AutoDetect.Panda" autowire="autodetect" />
<bean id="kungfu" class="com.gp6.autoAssemblyBean.AutoDetect.KungFu" >
<property name="name" value="Tai Ji" />
</bean>
</beans>
package com.gp6.autoAssemblyBean.AutoDetect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/gp6/autoAssemblyBean/AutoDetect/applicationContext.xml");
Panda panda = (Panda) context.getBean("panda");
System.out.println(panda);
}
}
输出
autowiring by constructor
com.gp6.autoAssemblyBean.AutoDetect.Panda@48e61a35
2. AutoDetect – 按类型装配
如果没有找到默认的构造函数,自动检测将按类型选择装配。
package com.gp6.autoAssemblyBean.AutoDetect.type;
public class KungFu {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Language [name=" + name + "]";
}
}
package com.gp6.autoAssemblyBean.AutoDetect.type;
public class Panda {
private KungFu kungfu;
public KungFu getKungfu() {
return kungfu;
}
public void setKungfu(KungFu kungfu) {
System.out.println("autowiring by type");
this.kungfu = kungfu;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="panda" class="com.gp6.autoAssemblyBean.AutoDetect.type.Panda" autowire="autodetect" />
<bean id="kungfu" class="com.gp6.autoAssemblyBean.AutoDetect.type.KungFu" >
<property name="name" value="Tai Ji" />
</bean>
</beans>
package com.gp6.autoAssemblyBean.AutoDetect.type;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/gp6/autoAssemblyBean/AutoDetect/type/applicationContext.xml");
Panda panda = (Panda) context.getBean("panda");
System.out.println(panda);
}
}
网友评论