使用到注解需导入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;
// 按名称进行装配
// @Autowired@Qualifier("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();
}
}