Spring(七)用@Resource註解完成屬性裝配

來源:互聯網
上載者:User

標籤:

使用到註解需匯入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註解完成屬性裝配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.