spring__自动装配
Bean 的自动装配
- 自动装配是Spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装备属性
- 在Spring中有三种装配的方式
- 在xml中显示的装配
- 在java中显示配置
- 隐式的自动装配bean
- 环境搭建
- 一个人有两个宠物
- ByName 自动装配
- 
<!-- byNmae 会自动在容器上下文中查找和自己对象set方法后面的值对应的id --> <bean id="people" class="edu.cqupt.pojo.People" autowire="byName"> <property name="name" value="shi-lin"/> <!--<property name="cat" ref="cat"/>--> <!--<property name="dog" ref="dog"/>--> </bean>ByType 自动装配 
1 <!-- byNmae 会自动在容器上下文中查找和自己对象set方法后面的值对应的id --> 2 3 <!-- byType 会自动在容器上下文中查找和自己对象属性类型相同的bean --> 4 5 <bean id="people" class="edu.cqupt.pojo.People" autowire="byType"> 6 7 <property name="name" value="shi-lin"/> 8 9 <!--<property name="cat" ref="cat"/>--> 10 11 <!--<property name="dog" ref="dog"/>--> 12 13 </bean>
- 小结:
- byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值唯一
- byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
- 注解实现装配
- JDK 1,5 开始支持注解、 Spring 2.5 开始支持注解
- 要使用注解
- 导入约束。 contex约束
- 配置注解的支持 <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>- @AutuoWried
 直接在属性上使用即可,也可以在set方式上使用,使用AutoWired我们可以不用编写Set方法了,前提是这个自动的属性在IOC(Spring)容器中存在,且符合名字byName - @Nullable字段标记了这个注解,说明这个字段可以为null
 1 public @interface Autowired { 2 3 boolean required() default true; 4 5 } 6 7 8 9 // required = false 说明这个对象可以为null,否则不允许为空 10 11 @Autowired(required = false) 12 13 private Dog dog; 14 15 @Autowired 16 17 private Cat cat; 18 19 private String name;- 如果自动装配的环境比较复杂,自动装配无法通过一个注解【@Autuowired】完成的时候,我们可以使用@Qualifier(value = "xxx")去配合@Autuowired使用,指定一个唯一的bean注入使用。
- @Resource 注解
 @Autowired/@Resource实现自动注入这两个注解用来代替bean容器xml配置文件中的autowire属性,实现引用类型属性的自动装配。 
public class Person {
    private String name; // 姓名
    private int age; // 年龄
    private String gender; // 性别
    @Autowired // 该注解代表,当从容器中取出一个Person时,会自动按照规则寻找一个bean为address赋值
    private Address address; // 联系地址
    // getters&setters...    
}
public class Address {
    private String country;
    private String city;
    private String street;
    // getters&setters...    
}
annotation.xml配置文件信息如下:
<!-- 该标签代表开启注解配置方式 -->
<context:annotation-config/>
    
<bean id="person" class="com.briup.spring.day02.annotation.Person">
    <!-- 这里只需要正常为基本属性赋值即可,address属性不用声明 --> 
    <property name="name" value="张三"/>
    <property name="age" value="20"/>
    <property name="gender" value="男"/>
</bean>
<bean id="addr" class="com.briup.spring.day02.annotation.Address">
    <property name="country" value="中国"></property>
    <property name="city" value="昆山"></property>
    <property name="street" value="学院路"></property>
</bean>
测试代码:
@Test
public void test1() {
    String path = "annotation.xml";
    ApplicationContext context = new ClassPathXmlApplicationContext(path);
    // 打印输出可以看到,容器中id为address的bean被注入到了id为person的bean中
    System.out.println(context.getBean("person"));
}
输出结果为
>> Console输出:
Person [name=张三, age=20, gender=男, address=Address [country=中国, city=昆山, street=学院路]]
注解使用的注意事项:
1)@Autowired使用后需要在xml文件加入以下配置才能生效:<context:annotation-config/>
2)@Autowired注解可以写在成员变量、setter方法、构造器函数上面
3)@Autowired默认按照byType匹配的方式进行注入,如果没有一个bean的类型是匹配的则会抛异常,如果有多个bean的类型都匹配成功了,那么再按byName方式进行选择。
4)@Autowired如果最终匹配不成功(注意:一定是一个都没有找到的情况)则会抛出异常。但是如果设置为 @Autowired(required=false),则最终匹配不成功没有不会抛出异常。
5)@Autowired可以结合@Qualifier(“beanName”)来使用,则可以达到byName的效果
了解@Resource注解
@Resource和**@Autowired**功能相同,用法相同,
不同点:Autowired默认是先根据Type进行匹配,而Resource先根据Name进行匹配。
注意事项:
1)@Resource使用后需要在xml文件加入以下配置才能生效:<context:annotation-config/>
2)@Resource的作用和@Autowired差不多,只不过 @Resource是默认先用byName,如果找不到合适的就再用byType来注入
3)@Resource有两个属性:name和type
 如果使用name属性则表示要byName匹配,使用type属性则表示要byType匹配:
 @Resource(“name”) // byName的方式
 @Resource(“type”) // byType == @Autowired
Component注解
Component注解作用在某个类上,代表将该类的实例纳入到容器中进行管理。
相当于我们在xml中定义了一个。
@Component和上面的@Autowired或@Resource结合使用可以极大程度地减少xml配置文件的内容。
注意事项:
1)@Component注解可以直接定义bean,而无需在xml定义。
但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义。
2)@Component注解直接写在类上面即可。
3)@Component有一个可选的参数,用于指定bean的名称。
4)@Component如果不指定参数,则bean的名称为当前类的类名小写。
5)@Component使用之后需要在xml文件配置一个标签开启扫描注解功能:
<context:component-scan base-package="" />
6)<context:component-scan base-package="com.briup.day02.annotation" />
base-package属性用来定义哪些包下的类需要被扫描。
注意:扫描范围是该包下的所有级别路径(包括子包)。
7)@Component定义的bean默认情况下都是单例模式的,如果要让这个bean变为非单例,可以再结合这个@Scope注解来达到目标@Scope(“prototype”)
8)结合SpringMVC
@Component是Spring中所有bean组件的通用形式,@Repository 、@Service、@Controller则是 @Component的细化,用来表示更具体的组件用途(主要为了提高语义性),分别对应了持久化层、服务层和表现层。但是至少到现在为止这个四种注解的实质区别很小(甚至几乎没有),作用都是把当前类注册为spring容器中的一个bean。
IoC容器的作用
1、帮我们管理对象的生命周期(什么时候创建、什么时候销毁)
2、帮我们管理对象之间的关联关系
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号