013 注解实现自动配置

狂神说Java:https://space.bilibili.com/95256449


jdk1.5支持的注解,spring2.5支持注解

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML. The short answer is “it depends.

要使用注解须知

1、导入约束:context约束

2、配置注解的支持:【重要】

<?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>

环境准备

Cat.java

package com.kuang.pojo;

/**
 * @author Administrator
 */
public class Cat {
    public void shout(){
        System.out.println("miao~");
    }
}

Dog.java

package com.kuang.pojo;

/**
 * @author Administrator
 */
public class Dog {
    public void shout(){
        System.out.println("wang~");
    }
}

People.java

package com.kuang.pojo;

/**
 * @author Administrator
 */
public class People {

    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public Dog getDog() {
        return dog;
    }


    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

beans2.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--开启注解的支持-->
    <context:annotation-config/>

    <bean id="cat11" class="com.kuang.pojo.Cat"/>
    <bean id="cat222" class="com.kuang.pojo.Cat"/>
    <bean id="dog22" class="com.kuang.pojo.Dog"/>
    <bean id="dog222" class="com.kuang.pojo.Dog"/>
    <bean id="people" class="com.kuang.pojo.People"/>
    <bean id="cat" class="com.kuang.pojo.Cat"/>
    <bean id="dog" class="com.kuang.pojo.Dog"/>

</beans>

测试类

@Test
public void test1(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
    People people = context.getBean("people", People.class);
    people.getCat().shout();
    people.getDog().shout();
}

@AutoWired

直接在属性上使用即可!也可以在set方法上使用!

使用@AutoWired我们可以不编写set方法,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字ByName!

<!--开启注解的支持-->
<context:annotation-config/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;

import javax.annotation.Resource;

/**
 * @author Administrator
 */
public class People {

    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public Dog getDog() {
        return dog;
    }


    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

结果:

miao~
wang~

科普

@Nullable 说明这个字段可以为null
public @interface Autowired {
    boolean required() default true;
}
/**
     * 如果显示的定义了Autowired的属性为false,说明这个对象可以为null。否则不能为空
     */
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

@Qualifier(value = “xxx”)

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解完成的时候【@Autowired】。我们可以使用@Qualifier(value = “xxx”)去配合@Autowired的使用,指定一个唯一的bean对象注入!

<!--开启注解的支持-->
<context:annotation-config/>

<bean id="cat11" class="com.kuang.pojo.Cat"/>
<bean id="cat222" class="com.kuang.pojo.Cat"/>
<bean id="dog22" class="com.kuang.pojo.Dog"/>
<bean id="dog222" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
public class People {

    @Autowired
    @Qualifier(value = "cat11")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog22")
    private Dog dog;
    private String name;
}

@Resource

public class People {

    @Resource(name="cat11")
    private Cat cat;
    @Resource(name="dog22")
    private Dog dog;
    private String name;
}

小结:

@AutoWired和@Resource的区别

1、都是用来自动装配的,都可以放在属性字段上

2、@AutoWired先通过ByType的方式实现,而且必须要求这个对象存在!【常用】

3、@Resource默认通过ByName的方式实现,找不到则通过ByType!如果两个都找不到,就报错!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值