1、byType自动装配
-
编写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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.beyond.pojo.Cat"/> <bean class="com.beyond.pojo.Dog"/> <!-- autowire="byType"表示会自动在容器上下文中查找,和自己对象属性类型(class)对应的 bean,但是必须要保证这个类型全局唯一 --> <bean id="people" class="com.beyond.pojo.People" autowire="byType"> <property name="name" value="小明"/> </bean> </beans>
-
测试。
import com.beyond.pojo.People; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test01(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); //喵~ //汪~ people.getCat().shut(); people.getDog().shut(); } }
小结:
- byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自己注入的属性的set方法的值一致;
- byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自己注入的属性的类型一致。
2、使用注解实现自动装配
使用注解须知:
-
导入约束:context约束
-
配置注解支持:context:annotation-config/
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>
@Autowired:
-
可以直接在属性上使用即可!也可以在set方法上使用;
-
使用@Autowired注解,我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC容器中存在。
-
拓展:
//如果显示定义了Autowired的required的属性为false,说明这个对象可以为null,否则不允许为空 @Autowired(required = false) private Cat cat;
如果自动装配的环境比较复杂,自动装配无法通过一个注解完成的时候,我们可以使用去配合的使用,指定一个唯一的bean对象注入。
@Autowired @Qualifier(value = "dog3") private Dog dog;
<bean id="dog1" class="com.beyond.pojo.Dog"/> <bean id="dog2" class="com.beyond.pojo.Dog"/> <bean id="dog3" class="com.beyond.pojo.Dog"/> <bean id="dog4" class="com.beyond.pojo.Dog"/> <bean id="people" class="com.beyond.pojo.People"/>
@Resource:
@Resource注解是Java自己自动装配的注解,第一步先查找beanid,看是否与对象属性的set方法的值一致;如果不一致,在查找beanclass,看是否与对象的属性的类型一致,要保证对象的属性的类型唯一;如果不唯一,也可以使用@Resource(name = “xxx”)来指定bean。
-
编写实体类;
package com.beyond.pojo; import javax.annotation.Resource; public class People { private String name; @Resource private Cat cat; @Resource(name = "dog3") private Dog dog; public String getName() { return name; } public void setName(String name) { this.name = name; } public Cat getCat() { return cat; } public Dog getDog() { return dog; } @Override public String toString() { return "People{" + "name='" + name + '\'' + ", cat=" + cat + ", dog=" + dog + '}'; } }
-
编写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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="catiiiiiiug" class="com.beyond.pojo.Cat"/> <bean id="dog1" class="com.beyond.pojo.Dog"/> <bean id="dog2" class="com.beyond.pojo.Dog"/> <bean id="dog3" class="com.beyond.pojo.Dog"/> <bean id="dog4" class="com.beyond.pojo.Dog"/> <bean id="people" class="com.beyond.pojo.People"/> </beans>
-
测试。
import com.beyond.pojo.People; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test01(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); //喵~ //汪~ people.getCat().shut(); people.getDog().shut(); } }
小结:
@Autowired与@Resource的区别:
- 都是用来自动装配的,都可以放在属性字段上使用;
- @Autowired 通过byType的方式实现,必须要求这个对象存在;
- @Resource 默认先通过byName的方式实现,再通过byType实现!