Spring框架根据数据某些规则给引用类型赋值,不用我们在给引用类型赋值。
第一种通过配置文件方式:
1. 通过 autowire="byName"
Java类中引用类型的属性名和Spring容器中的配置文件< bean >的id名称一样, 且数据类型是一致的,这样容器中的bean,Spring会自动给引用类型赋值。
beans.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.xsd">
<bean id="schools" class="cn.com.Ycy.spring.Beans.school">
<property name="name" value="北京大学"/>
<property name="address" value="北京"/>
<property name="id" value="0052"/>
</bean>
<bean id="student2" class="cn.com.Ycy.spring.Beans.student" autowire="byName">
<property name="name" value="Ycy"/>
<property name="age" value="21"/>
</bean>
</beans>
测试类
@Test
public void test08(){
String config = "bean01/applicationContext.xml";
ApplicationContext ac= new ClassPathXmlApplicationContext(config);
Object student = ac.getBean("student2");
System.out.println(student);
}
autowire="byName":创建对象时,spring就找cn.com.Ycy.spring.Beans.student的类的引用类型属性名
在用这个引用类型名去找和这个值相同的id(在整个applicationContext.xml 寻找)
2. 通过 autowire="byType"
Java类中的引用类型的数据类型和Spring容器中(配置文件)<bean>的class是同源的关系,这样<bean>才可以赋值给引用类型
<bean id="student2" class="cn.com.Ycy.spring.Beans.student" autowire="byType">
<property name="name" value="王二"/>
<property name="age" value="21"/>
</bean>
<bean id="mySchool" class="cn.com.Ycy.spring.Beans.school">
<property name="name" value="北京大学"/>
<property name="address" value="北京"/>
<property name="id" value="0052"/>
</bean>
student 类(省略set方法)
public class student {
private String name;
private int age;
private school school;
}
分析:
就是把
<bean id="mySchool" class="cn.com.Ycy.spring.Beans.school">
<property name="name" value="北京大学"/>
<property name="address" value="北京"/>
<property name="id" value="0052"/>
</bean>
这个赋值给 private school school
同源关系解析:
1. java类中的引用类型的数据类型和bean的class的值是一样的
2. java类中的引用类型的数据类型和bean的class的值是父子关系的
3. java类中的引用类型的数据类型和bean的class的值是接口和实现类关系
就好像是这样的关系:
private school schools = new xxx();
1. school school = new school();
2. school school = new primarySchool();(父子关系)
3. school school = new schoolImp();接口和实现类关系
new xxx()就是赋值
第二种通过注解和配置文件方式:@Autowired
一般的属性赋值在bean中完成,引用类型通过注解完成,这个注解相当于在配置文件中的autowire=“byType”
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"
xmlns:context="http://www.springframework.org/schema/context"
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:annotation-config/>
<bean id="mystudent" class="cn.com.Ycy.spring.domain.bao01.student">
<property name="name" value="张三"/>
<property name="age" value="21"/>
</bean>
<bean id="school" class="cn.com.Ycy.spring.domain.bao01.school">
<property name="name" value="北京大学"/>
<property name="address" value="北京"/>
<property name="id" value="001"/>
</bean>
</beans>
在这里一定要加上< context:annotation-config/ >不然会报错的
public class student {
private String name;
private int age;
@Autowired
private school schools;
}
使用上了@Autowired就可以删除类中的setschool()方法,其他的set方法是要加上的
@Qualifier:
1. @Autowired是根据类型自动注入的,加上@Qualifier就可以根据byName的方式自动注入了
2. @Qualifier不可以单独使用
<?xml version="1.0" encoding="UTF-8"?>
<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"
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:annotation-config/>
<bean id="mystudent" class="cn.com.Ycy.spring.domain.bao01.student">
<property name="name" value="张三"/>
<property name="age" value="21"/>
</bean>
<bean id="myschool" class="cn.com.Ycy.spring.domain.bao01.school">
<property name="name" value="清华大学"/>
<property name="address" value="北京"/>
<property name="id" value="001"/>
</bean>
</beans>
public class student {
private String name;
private int age;
@Autowired
@Qualifier(value = "myschool")
private school schools;
}
@Qualifier(value = "myschool")的值是对应xml配置文件的id
@Resource:
1. @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
2. 其次再进行默认的byName方式进行装配;
3. 如果没有这个name,就按byType的方式自动装配
<?xml version="1.0" encoding="UTF-8"?>
<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"
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:annotation-config/>
<bean id="mystudent" class="cn.com.Ycy.spring.domain.bao01.student">
<property name="name" value="张三"/>
<property name="age" value="21"/>
</bean>
<bean id="myschool" class="cn.com.Ycy.spring.domain.bao01.school">
<property name="name" value="清华大学"/>
<property name="address" value="北京"/>
<property name="id" value="001"/>
</bean>
</beans>
public class student {
private String name;
private int age;
@Resource(name = "myschool")
private school schools;
}
如果是不写name,直接就是@Resource,就是按照byType
总结一下:
@Autowired与@Resource异同:
1、@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。
2、@Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用
3、@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。