Spring(七)用@Resource和@Autowired註解完成屬性裝配及自動裝配
阿新 • • 發佈:2019-01-07
使用到註解需匯入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();
}
}
@Autowired的應用
package test.spring.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import test.spring.dao.PersonDao;
import test.spring.service.PersonService2;
public class PersonServiceBean5 implements PersonService2 {
// 按型別進行裝配
// @Autowired
// private PersonDao personDao;
// 按名稱進行裝配
// @[email protected]("personDaoxx")
// private PersonDao personDao;
/*
* required=true,如果在Spring中根據型別找不到對應的容器,就會報沒有成功地為這個欄位注入值;
* required=false,如果在Spring容器裡面找不到與型別匹配的bean,就會把這個變數設為null
*/
@Autowired(required = true)
@Qualifier("personDaoxx")
private PersonDao personDao;
public PersonServiceBean5() {
}
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void save() {
personDao.add();
}
}
自動裝配,使用自動裝配方式難以除錯,程式中的bug通常在執行過程中出現問題才發現,而且容易出現難以預料的問題,故不建議使用。
beans3.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">
<bean id="personDaoxx" class="test.spring.dao.impl.PersonDaoBean" />
<bean id="personService" class="test.spring.service.impl.PersonServiceBean6" autowire="byType"></bean>
</beans>
package test.spring.service.impl;
import test.spring.dao.PersonDao;
import test.spring.service.PersonService2;
public class PersonServiceBean6 implements PersonService2 {
private PersonDao personDao;
public PersonServiceBean6() {
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void save() {
personDao.add();
}
}
package test.spring.jnit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.service.PersonService2;
public class SpringTest5 {
@Test
public void testAutowired() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"beans3.xml");
PersonService2 personService = (PersonService2) applicationContext
.getBean("personService");
personService.save();
}
}