<bean>的autowire属性有如下六个取值,他们的说明如下:
1.No:即不启用自动装配,默认为此值。
2.byName:通过属性的名字的方式查找受管bean依赖的对象并为其注入。比如说类Computer有个Print类型的成员print,指定其autowire属性为byName后,Spring Ioc容器会在配置文件中查找id/name属性为printer的bean,然后用Setter方法为期注入。
3.byType:通过属性的类型查找受管bean依赖的对象并为其注入。比如类Computer有个成员printer,类型为Printer。那么,指定其autowire属性为byType后,Spring Ioc容器会查找Class属性为Printer的bean,并使用Setter方法为其注入。
4.constructor:同byType一样,也是通过类型查找依赖对象。以byType的区别在于它不是使用Setter方法注入,而是使用构造子注入。
5.autodetect:在byType和constructor之间自动的选择注入方式。
6.default:由上级标签<beans>的default-autowire属性确定。
注意:在配置bean时,<bean>标签中Autowire属性的优先级比其上级标签高,即是说,如果在上级标签中定义default-autowire属性为byName,而在<bean>中定义为byType时,Spring Ioc容器会优先使用<bean>标签的配置。
现在来测试一下,看看我们自定义的事件AnimalSpeakEvent是不是会在动物“讲话”时发生。
自定义事件:
package ioc.test;
import org.springframework.context.ApplicationEvent;
public class AnimalSpeakEvent extends ApplicationEvent{
/**
*
*/
private static final long serialVersionUID = 1L;
private String animalName;
public String getAnimalName() {
return animalName;
}
public void setAnimalName(String animalName) {
this.animalName = animalName;
}
public AnimalSpeakEvent(Object source){
super(source);
}
public AnimalSpeakEvent(Object source, String animalName){
super(source);
this.animalName=animalName;
}
}
动物:
package ioc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Animal implements ApplicationContextAware{
private ApplicationContext ac;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String speak(){
//既然是一个动物讲话事件,那么就应该在动物讲话的时候发布
ac.publishEvent(new AnimalSpeakEvent(this,this.name));
return "我的名字是"+this.name+",我的年龄是:"+this.age;
}
public void setApplicationContext(ApplicationContext ac){
this.ac=ac;
}
}
监听方法:
package ioc.test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class AnimalEventListener implements ApplicationListener{
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
if(event instanceof AnimalSpeakEvent){
AnimalSpeakEvent a=(AnimalSpeakEvent)event;
System.out.println("时间监听器:"+this.getClass().getSimpleName()+":有一个动物在讲话!它的名字是:"+a.getAnimalName());
}
}
}
测试类:
package ioc.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args){
AbstractApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Animal animal=(Animal)ac.getBean("Animal");
System.out.println(animal.speak());
}
}
applicationContext.xml
<?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="Listener" class="ioc.test.AnimalEventListener"></bean>
<bean id="Animal" class="ioc.test.Animal">
<property name="name" value="老虎"></property>
<property name="age" value="5"></property>
</bean>
</beans>