Spring的自动装配机制
2007-03-26 17:04
369 查看
Spring提供一种自动装配,功能,就是不再使用ref进行手工装配bean,这种方式可以减少配置文件的代码量,但是,在大型项目中,不推荐使用,容易混乱,Spring提供byName,byType,constructor,autpdetect四种自动装备方式:
定义接口:
package Bean.autowire;
public interface Dog ...{
public void chop();
}
package Bean.autowire;
public interface Person ...{
public void useDog();
}
定义实现类:
package Bean.autowire;
public class Gundog implements Dog ...{
private String name;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public void chop() ...{
System.out.println(name);
}
}
package Bean.autowire;
import Bean.autowire.Person;
public class Chinese implements Person ...{
public void useDog() ...{
gundog.chop();
}
public Chinese(Dog dog)...{
this.setGundog(dog);
}
private Dog gundog;
public Dog getGundog() ...{
return gundog;
}
public void setGundog(Dog gundog) ...{
this.gundog = gundog;
}
}
配置文件:
byName方式,必须在实现类Chinese中有一个setter方法,必须是set+bean名,首字母大写,如:
public void setGundog(Dog gundog) {
this.gundog = gundog;
}
byType方式,必须有setter方法,要求setter方法的参数类型与容易bean的类型相同,如:
public void setDog(Dog dog) {
this.dog = dog;
}
constructor方式,必须有何bean类型复合,数量复合的构造函数,如:
public Chinese(Dog dog){
this.setGundog(dog);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- byname
<bean id="chinese" class="Bean.autowire.Chinese" autowire="byName"></bean>
<bean id="gundog" class="Bean.autowire.Gundog">
<property name="name">
<value>byname</value>
</property>
</bean>
-->
<!-- bytype
<bean id="chinese" class="Bean.autowire.Chinese" autowire="byType"></bean>
<bean id="gundog" class="Bean.autowire.Gundog">
<property name="name">
<value>bytype</value>
</property>
</bean>
-->
<!-- byconstructor
<bean id="chinese" class="Bean.autowire.Chinese" autowire="constructor"></bean>
<bean id="dog" class="Bean.autowire.Gundog">
<property name="name">
<value>byconstructor</value>
</property>
</bean>
-->
</beans>
测试代码:
public static void main(String[] args) throws Exception ...{
String path=new Test().getClass().getResource("/").getPath();
String realpath=path.substring(1, path.length());
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/autowire.xml");
Person person1=(Person)context.getBean("chinese");
person1.useDog();
}
运行结果:
可以分别打印注入bean的属性,byname,bytype.constructor
autodetect是自动根据bean的内部结构选择constructor或者byType
定义接口:
package Bean.autowire;
public interface Dog ...{
public void chop();
}
package Bean.autowire;
public interface Person ...{
public void useDog();
}
定义实现类:
package Bean.autowire;
public class Gundog implements Dog ...{
private String name;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public void chop() ...{
System.out.println(name);
}
}
package Bean.autowire;
import Bean.autowire.Person;
public class Chinese implements Person ...{
public void useDog() ...{
gundog.chop();
}
public Chinese(Dog dog)...{
this.setGundog(dog);
}
private Dog gundog;
public Dog getGundog() ...{
return gundog;
}
public void setGundog(Dog gundog) ...{
this.gundog = gundog;
}
}
配置文件:
byName方式,必须在实现类Chinese中有一个setter方法,必须是set+bean名,首字母大写,如:
public void setGundog(Dog gundog) {
this.gundog = gundog;
}
byType方式,必须有setter方法,要求setter方法的参数类型与容易bean的类型相同,如:
public void setDog(Dog dog) {
this.dog = dog;
}
constructor方式,必须有何bean类型复合,数量复合的构造函数,如:
public Chinese(Dog dog){
this.setGundog(dog);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- byname
<bean id="chinese" class="Bean.autowire.Chinese" autowire="byName"></bean>
<bean id="gundog" class="Bean.autowire.Gundog">
<property name="name">
<value>byname</value>
</property>
</bean>
-->
<!-- bytype
<bean id="chinese" class="Bean.autowire.Chinese" autowire="byType"></bean>
<bean id="gundog" class="Bean.autowire.Gundog">
<property name="name">
<value>bytype</value>
</property>
</bean>
-->
<!-- byconstructor
<bean id="chinese" class="Bean.autowire.Chinese" autowire="constructor"></bean>
<bean id="dog" class="Bean.autowire.Gundog">
<property name="name">
<value>byconstructor</value>
</property>
</bean>
-->
</beans>
测试代码:
public static void main(String[] args) throws Exception ...{
String path=new Test().getClass().getResource("/").getPath();
String realpath=path.substring(1, path.length());
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/autowire.xml");
Person person1=(Person)context.getBean("chinese");
person1.useDog();
}
运行结果:
可以分别打印注入bean的属性,byname,bytype.constructor
autodetect是自动根据bean的内部结构选择constructor或者byType
相关文章推荐
- Java的注解机制——Spring自动装配的实现原理
- Java注解机制之Spring自动装配实现原理
- Java的注解机制——Spring自动装配的实现原理
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- Java的注解机制——Spring自动装配的实现原理
- Java的注解机制——Spring自动装配的实现原理
- Spring对bean的装配机制(一)——隐式自动装配
- spring 使用注解自动装配和自动扫描机制
- Java的注解机制——Spring自动装配的实现原理
- Java的注解机制——Spring自动装配的实现原理
- Java的注解机制——Spring自动装配的实现原理
- Spring9 : Autowire(自动装配)机制
- Java的注解机制——Spring自动装配的实现原理
- Java的注解机制——Spring自动装配的实现原理
- Spring中bean的三种装配机制之——自动装配
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- Spring容器拆分、自动装配和依赖检查机制,基于注解的sessionFactory
- Java的注解机制——Spring自动装配的实现原理
- Java注解机制之Spring自动装配实现原理详解
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提