码迷,mamicode.com
首页 > 编程语言 > 详细

Spring(七)用@Resource注解完成属性装配

时间:2015-07-24 14:29:00      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

使用到注解需导入jar包:common-annotations.jar
手工装配依赖对象有两种编程方式:
一、在xml配置文件中通过bean节点进行配置,如:

<?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-2.5.xsd">
    <bean id="personDao" class="test.spring.dao.impl.PersonDaoBean" />
    <bean id="personService" class="test.spring.service.impl.PersonServiceBean3">
        <constructor-arg index="0" type="test.spring.dao.PersonDao" ref="personDao"/>
        <constructor-arg index="1" value="LinDL"/>
    </bean>
</beans> 

二、在Java代码中使用@Autowired或@Resource注解方式进行装配。但需在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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- 把针对注解的容器注射到Spring容器中 -->
    <context:annotation-config />

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

</beans> 

这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、
RequireAnnotationBeanPostProcessor

在代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:
@Autowired默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
用于字段上
@Resource
private PersonDao personDao;
用于属性的setter方法上
@Resource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}

一个应用@Resource的例子:

package test.spring.dao;

public interface PersonDao {

    public abstract void add();

}
package test.spring.dao.impl;

import test.spring.dao.PersonDao;


public class PersonDaoBean implements PersonDao {

    @Override
    public void add(){
        System.out.println("执行PersonDaoBean里的test1()方法");
    }
}
package test.spring.service;

public interface PersonService2 {

    public abstract void save();
}
package test.spring.service.impl;

import javax.annotation.Resource;

import test.spring.dao.PersonDao;
import test.spring.service.PersonService2;

public class PersonServiceBean4 implements PersonService2 {

    // 先根据属性名personDao在beans2.xml中查找注入,如果找不到对应的就会按照类型进行注入
    // @Resource
    // private PersonDao personDao;

    // 也可以指定配置文件中的id进行注入
    // @Resource(name = "personDaoxx")
    private PersonDao personDao;

    public PersonServiceBean4() {

    }

    public PersonDao getPersonDao() {
        return personDao;
    }

    // 也可以通过属性的setter方法进行注入,不能用于属性的getter方法
    @Resource
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

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

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!-- 把针对注解的容器注射到Spring容器中 -->
    <context:annotation-config />
    <bean id="personDaoxx" class="test.spring.dao.impl.PersonDaoBean" />
    <bean id="personService" class="test.spring.service.impl.PersonServiceBean4"></bean>
</beans> 
package test.spring.jnit;

import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.service.PersonService2;

public class SpringTest3 {

    @Test
    public void testInject() {
        AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "beans2.xml");
        PersonService2 personService = (PersonService2) applicationContext
                .getBean("personService");
        personService.save();
        applicationContext.close();
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。如需转载,请注明出处:http://blog.csdn.net/lindonglian

Spring(七)用@Resource注解完成属性装配

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/47038663

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!