Spring依赖注入(注解方式)
在Spring中,尽管使用XML配置文件就可以实现Bean的装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给程序的维护与升级带来一定的困难。
Java从JDK5.0以后,提供了Annotation(注解)功能,Spring也提供了对注解技术的全面支持。
一,Spring中注入常用的注解如下:
1,@Component
可以使用此注解描述Spring中的Bean,它是一个泛化的概念,表示一个组件(Bean),可以作用在任何层次。使用时只需要将该注解标注在相应的类上即可。
2,@Repository
用于将数据访问层(DAO层)的类标识为Spring中的Bean,其功能与@Component相同。
3,@Service
用于将业务层(Service层)的类标识为Spring中的Bean,其功能与@Component相同。
4,@Controller
用于将控制层的类标识为Spring中的Bean,其功能与@Component相同。
5,@Autowired
用于对Bean的属性变量,属性的Set方法以及构造方法进行标注,配合对应的注解处理器完成Bean的自动配置工作。默认按照Bean的类型(type)进行装配。
6,@Resource
作用与@Autowired一样。区别在于@Autowired默认按照Bean类型(type)注入,而@Resource默认按照Bean实例名称进行装配
@Resource 中有两个重要属性:name 和 type。
Spring 将 name 属性解析为 Bean 实例名称,type 属性解析为 Bean 实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。
如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。
7,@Qualifier
与@Autowired注解配合使用,会将默认的按照Bean类型注入改为按照Bean的实例名称装配,Bean的实例名称由@Qualifier注解的参数指定。
二,应用实例
1.创建DAO层接口
在 src 目录下创建一个名为 com.ljt.spring的包,在该包下创建一个名为 PersonDao 的接口,并添加一个 add() 方法。
package com.ljt.spring;
public interface PersonDao {
public void add();
}
2.创建DAO层接口实现类
在 com.ljt.spring 包下创建 PersonDao 接口的实现类 PersonDaoImpl
package com.ljt.spring;
import org.springframework.stereotype.Repository;
@Repository("personDao")
public class PersonDaoImpl implements PersonDao {
@Override
public void add() {
System.out.println("Dao层的add()方法执行了");
}
}
上述代码中,使用 @Repository 注解将 PersonDaoImpl 类标识为 Spring 中的 Bean,其写法相当于xml配置文件中 <bean id="personDao"class=“com.ljt.spring.PersonDaoImpl”/> 的书写。然后在 add() 方法中输出一句话,用于验证是否成功调用了该方法。
3.创建Service层接口
在 com.ljt.spring包下创建一个名为 PersonService 的接口,并添加一个 add() 方法。
package com.ljt.spring;
public interface PersonService {
public void add();
}
4.创建Service层的实现类
在 com.mengma.annotation 包下创建 PersonService 接口的实现类 PersonServiceImpl。
package com.ljt.spring;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("personService")
public class PersonServiceImpl implements PersonService {
@Resource(name = "personDao")
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
@Override
public void add() {
personDao.add(); //调用personDao中的add()方法
System.out.println("Service层的add()方法执行了");
}
}
使用 @Service 注解将 PersonServiceImpl 类标识为 Spring 中的 Bean,其写法相当于xml配置文件中 <bean id="personService"class=“com.ljt.spring.PersonServiceImpl”/>
然后使用 @Resource 注解标注在属性 personDao 上(也可标注在 personDao 的 setPersonDao() 方法上),这相当于配置文件中 <property name="personDao"ref=“personDao”/> 的写法。最后在该类的 add() 方法中调用 personDao 中的 add() 方法。
5.创建Controller层
在 com.ljt.spring 包下创建一个名为 PersonController 的类
package com.ljt.spring;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller("personController")
public class PersonController {
@Resource(name = "personService")
private PersonService personService;
public PersonService getPersonService() {
return personService;
}
public void add(){
personService.add(); //调用personService中的add()方法
System.out.println("Controller层的add()方法执行了");
}
}
使用 @Controller 注解标注 PersonController 类,其写法相当于在配置文件中编写 <bean id="personController"class=“com.ljt.spring.PersonController”/>。
然后使用了 @Resource 注解标注在 personService 上,这相当于在配置文件内编写 <property name="personService"ref=“personService”/>。
最后在其 add() 方法中调用了 personService 中的 add() 方法。
6.编写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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用context,通知spring扫描指定目录,进行注解解析-->
<context:component-scan base-package="com.ljt.spring"/>
</beans>
使用 context :component-scan 元素进行注解的扫描,其 base-package 属性用于告诉 spring 所需要注解扫描的目录。
7.创建测试类查看测试结果
创建测试类AnnotationTest
package com.ljt.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationTest {
@Test
public void test(){
//初始化spring容器,加载xml配置文件,实例化bean
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//获取PersonController实例
PersonController personController = (PersonController) applicationContext.getBean("personController");
//调用PersonController中的add()方法
personController.add();
}
}
通过加载配置文件spring.xml并获取 personController 的实例,然后调用该实例的 add() 方法。
使用 JUnit 测试运行 test() 方法,测试结果如下所示
Dao层的add()方法执行了
Service层的add()方法执行了
Controller层的add()方法执行了