Spring框架学习

文章40 |   阅读 14965 |   点赞0

来源:https://blog.csdn.net/yerenyuan_pku/category_6457009.html

@Autowire注解与自动装配

x33g5p2x  于2021-12-18 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(380)

前面我们已经学会使用@Resource注解注入属性,并且我们还编码剖析了@Resource注解的实现原理。现在我们来学习使用@Autowire注解注入属性,本文是建立在编码剖析@Resource注解的实现原理的案例基础上的。

用@Autowire注解完成属性装配

@Autowire注解和@Resource一样,同样也可以标注在字段或属性的setter方法上,但它默认按类型装配。
我们将@Autowire注解标注在字段上,如将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService {
    @Autowired private PersonDao personDao;

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Override
    public void save() {
        personDao.add();
    }
}

接着将SpringTest类的代码改为:

public class SpringTest {

    @Test
    public void instanceSpring() {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
        PersonService personService = (PersonService) ctx.getBean("personService");
        personService.save();
        ctx.close();
    }

}

测试instanceSpring()方法,可发现Eclipse控制台打印:

如果我们想使用按名称装配,可以结合@Qualifier注解一起使用,如将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService {
    @Autowired @Qualifier("personDaoxxxx") private PersonDao personDao;

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Override
    public void save() {
        personDao.add();
    }
}

再次测试instanceSpring()方法,可发现Eclipse控制台打印:

注意:@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如:

@Autowired(required=true) @Qualifier("personDaoxxxx") private PersonDao personDao;

required=true代表字段personDao必须要注入值,也即是说在Spring容器中根据类型找不到对应的bean,那就会报异常;required=false意味着在Spring容器中根据类型找不到对应的的bean,就会把该字段设为null。

依赖注入——自动装配依赖对象

对于自动装配,大家了解一下就可以了,实在不推荐大家使用。例子:

<bean id="..." class="..." autowire="byType"/>

autowire属性取值如下:

  • byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null。
  • byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null。
  • constructor与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
  • autodetect:通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式

现在我们重点关注byType和byName这两个属性值。为了试验,我们将PersonServiceBean类的代码改为:

public class PersonServiceBean implements PersonService {
    private PersonDao personDao;

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Override
    public void save() {
        personDao.add();
    }
}

此时要想自动装配依赖对象,试着将Spring的配置文件修改为:

<?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-4.2.xsd">

    <context:annotation-config/>

    <bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" autowire="byType"></bean>
</beans>

此时测试SpringTest类的instanceSpring()方法,可看到Eclipse控制台打印:

若将Spring的配置文件修改为:

<?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-4.2.xsd">

    <context:annotation-config/>

    <bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" autowire="byName"></bean>
</beans>

再次测试SpringTest类的instanceSpring()方法,发现报空指针异常,明显就是没有为属性注入值,所以我们将Spring的配置文件中的

<bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>

修改为:

<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>

此时再次测试SpringTest类的instanceSpring()方法,可发现Eclipse控制台打印:

相关文章