在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。
Java 从 JDK 5.0 以后,提供了 Annotation(注解)功能,Spring 也提供了对 Annotation 技术的全面支持。Spring3 中定义了一系列的 Annotation(注解),常用的注解如下。
1)@Component
可以使用此注解描述 Spring 中的 Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可。
2)@Repository
用于将数据访问层(DAO层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
3)@Service
通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
4)@Controller
通常作用在控制层(如 Struts2 的 Action),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
5)@Autowired
用于对 Bean 的属性变量、属性的 Set 方法及构造函数进行标注,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。
6)@Resource
其作用与 Autowired 一样。其区别在于 @Autowired 默认按照 Bean 类型装配,而 @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.mengma.annotation 的包,在该包下创建一个名为 PersonDao 的接口,并添加一个 add() 方法,如下所示。
package com.mengma.annotation;
public interface PersonDao {
public void add();
}
2. 创建 DAO 层接口的实现类
在 com.mengma.annotation 包下创建 PersonDao 接口的实现类 PersonDaoImpl,编辑后如下所示。
package com.mengma.annotation;
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,其写法相当于配置文件中 <bean id="personDao"class="com.mengma.annotation.PersonDaoImpl"/> 的书写。然后在 add() 方法中输出一句话,用于验证是否成功调用了该方法。
3. 创建 Service 层接口
在 com.mengma.annotation 包下创建一个名为 PersonService 的接口,并添加一个 add() 方法,如下所示。
package com.mengma.annotation;
public interface PersonService {
public void add();
}
4. 创建 Service 层接口的实现类
在 com.mengma.annotation 包下创建 PersonService 接口的实现类 PersonServiceImpl,编辑后如下所示。
package com.mengma.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@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,其写法相当于配置文件中 <bean id="personService"class="com.mengma.annotation.PersonServiceImpl"/> 的书写。 然后使用 @Resource 注解标注在属性 personDao 上(也可标注在 personDao 的 setPersonDao() 方法上),这相当于配置文件中 <property name="personDao"ref="personDao"/> 的写法。最后在该类的 add() 方法中调用 personDao 中的 add() 方法,并输出一句话。
5. 创建 Action
在 com.mengma.annotation 包下创建一个名为 PersonAction 的类,编辑后如下所示。
package com.mengma.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
@Controller("personAction")
public class PersonAction {
@Resource(name = "personService")
private PersonService personService;
public PersonService getPersonService() {
return personService;
}
public void add() {
personService.add(); // 调用personService中的add()方法
System.out.println("Action层的add()方法执行了...");
}
}
上述代码中,首先使用 @Controller 注解标注 PersonAction 类,其写法相当于在配置文件中编写 <bean id="personAction"class="com.mengma.annotation.PersonAction"/>。
然后使用了 @Resource 注解标注在 personService 上,这相当于在配置文件内编写 <property name="personService"ref="personService"/>。
最后在其 add() 方法中调用了 personService 中的 add() 方法,并输出一句话。
6. 创建 Spring 配置文件
在 com.mengma.annotation 包下创建一个名为 applicationContext.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:tx="http://www.springframework.org/schema/tx"
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/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用context命名空间,通知spring扫描指定目录,进行注解的解析-->
<context:component-scan base-package="com.mengma.annotation"/>
</beans>
与之前的配置文件相比,上述代码的<beans>元素中增加了第 7 行、第 15 行和第 16 行中包含有 context 的代码,然后在第 18 行代码中,使用 context 命名空间的 component-scan 元素进行注解的扫描,其 base-package 属性用于通知 spring 所需要扫描的目录。
7. 创建测试类
在 com.mengma.annotation 包下创建一个名为 AnnotationTest 的测试类,编辑后如下所示。
package com.mengma.annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationTest {
@Test
public void test() {
// 定义Spring配置文件路径
String xmlPath = "com/mengma/annotation/applicationContext.xml";
// 初始化Spring容器,加载配置文件,并对bean进行实例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
xmlPath);
// 获得personAction实例
PersonAction personAction = (PersonAction) applicationContext
.getBean("personAction");
// 调用personAction中的add()方法
personAction.add();
}
}
上述代码中,首先通过加载配置文件并获取 personAction 的实例,然后调用该实例的 add() 方法。
8. 运行程序并查看结果
使用 JUnit 测试运行 test() 方法,运行成功后,输出结果如图

吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于Annotation装配Bean的更多相关文章

  1. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:继承的应用

    class Array{ // 表示数组 private int temp[] ; // 整型数组 private int foot ; // 定义添加位置 public Array(int len) ...

  2. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:匿名内部类

    interface A{ public void printInfo() ; // } class B implements A{ // 实现接口 public void printInfo(){ S ...

  3. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:包装类

    public class WrapperDemo01{ public static void main(String args[]){ int x = 30 ; // 基本数据类型 Integer i ...

  4. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:Object类

    class Demo{ // 定义Demo类,实际上就是继承了Object类 }; public class ObjectDemo01{ public static void main(String ...

  5. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:宠物商店实例分析

    interface Pet{ // 定义宠物接口 public String getName() ; public String getColor() ; public int getAge() ; ...

  6. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:抽象类与接口的应用

    abstract class A{ // 定义抽象类A public abstract void print() ; // 定义抽象方法print() }; class B extends A { / ...

  7. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:instanceof关键字

    class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...

  8. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:对象的多态性

    class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...

  9. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:接口的基本实现

    interface A{ // 定义接口A public static final String AUTHOR = "李兴华" ; // 全局常量 public abstract ...

  10. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:final关键字

    final class A{ // 使用final定义类,不能有子类 }; class B extends A{ // 错误,不能被继承 }; class A{ public final void p ...

随机推荐

  1. 《ORACLE数据库管理与开发》第三章学习之常用函数记录

    <ORACLE数据库管理与开发>第三章学习之常用函数记录 注:文章中的*代表所要操作的列名 1.lower(*)/upper(*),将此列下的值转为小写/大写 2.initcap(*):把 ...

  2. redis学习记录1 特性与优点

    1.存储结构:字符串.散列.列表.集合.有序集合. redis存储结构的优势:数据在redis中的储存方式和其在程序中的储存方式相近:redis对不同数据类型提供非常方便的操作方式,如使用集合类型储存 ...

  3. 在 Linux 上实现一段时间后自动登出非活动用户

    参考 编辑 ~/.bashrc 或 ~/.bash_profile 文件: $ vi ~/.bashrc 或, $ vi ~/.bash_profile 将下面行加入其中: TMOUT=100 这会让 ...

  4. Scala 线性化规则和 super 操作

    如果一个类有多个父类,且父类的有相同的函数 f,在子类和父类中调用 super.f 都是按从右到左的调用函数的顺序. 这个规则名为:Linearization Rules 如下的代码 trait Ba ...

  5. 项目启动报错:Communications link failure

    2017-12-29 10:43:19,776 ERROR [com.alibaba.druid.pool.DruidDataSource] - <init datasource error, ...

  6. MQ的调用

    mq调用(相关dll) using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collecti ...

  7. Golang的类型转换实战案例

    Golang的类型转换实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型概述 基础数据类型概述,博主推荐阅读: 布尔型: https://www.cnblogs. ...

  8. 2.9 学习总结 之 【Android】体温统计APP

    一.说在前面 昨天 学习了JQ的相关知识 今天 编写体温统计APP 我的工程源码:https://github.com/xiaotian12-call/Take-body-temperature 二. ...

  9. 32位CPU和64位CPU 区别

    操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的CPU设计. 64位操作系统针对的64位的CPU设计.操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的C ...

  10. 如何从Domino迁移到Exchange 2010

      从Domino 6.x迁移到Exchange 2010利用了微软提供的工具:Microsoft Transporter Suite,该工具不支持从Domino 6.X直接迁移至Exchange 2 ...