前文对 Spring IoC 和 Spring AOP 的实现方法进行了整合。如果有不明白的或有质疑的地方可以评论出来,一起探讨问题,帮助别人也是帮助自己!本文探讨的中心主要放在 Spring 的注解上。对于 Spring ,我也是个初学者,文中也许会出现其他作者总结中出现的内容,在这里感谢原作者。

注解的基本概念和原理

注解(Annotation)提供了一种安全的类似注释的机制,为我们在代码中添加信息提供了一种形式化得方法,使我们可以在稍后某个时刻方便的使用这些数据(通过解析注解来使用这些数据),用来将任何的信息或者元数据与程序元素(类、方法、成员变量等)进行关联。其实就是更加直观更加明了的说明,这些说明信息与程序业务逻辑没有关系,并且是供指定的工具或框架使用的。Annotation 像一种修饰符一样,应用于包、类型、构造方法、方法、成员变量、参数及本地变量的申明语句中。

Annotation 其实是一种接口。通过 Java 的反射机制相关的API来访问 Annotation 信息。相关类(框架或工具中的类)根据这些信息来决定如何使用该程序元素或改变它们的行为。Java 语言解释器在工作时会忽略这些 Annotation ,因此在 JVM 中这些Annotation 是“不起作用”的,只能通过配套的工具才能对这些 Annotation 类型的信息进行访问和处理。

自定义一个注解

 import java.lang.annotation.ElementType;  
 import java.lang.annotation.Retention;  
 import java.lang.annotation.RetentionPolicy;  
 import java.lang.annotation.Target;  
   
 // 在运行时执行  
 @Retention(RetentionPolicy.RUNTIME)  
 // 注解适用地方(字段和方法)  
 @Target({ ElementType.FIELD, ElementType.METHOD })  
 public @interface AnnotationTest {  
   
     // 注解的name属性  
     public String name() default "";  
 }

有关注解的详细信息可以查阅相关开发文档,这里不再赘述。

Spring 注解的作用

Spring 的一个核心功能是 IoC,就是将 Bean 初始化加载到容器中,Bean 是如何加载到容器的,可以使用 Spring 注解方式或者 Spring XML 配置方式。
Spring 注解方式减少了配置文件内容,更加便于管理,并且使用注解可以大大提高了开发效率!使用 Spring 框架进行开发,注解是必不可少的一部分,可以说是非常重要了。
使用 Spring 注解需要的 jar 包

  • com.springsource.org.aopalliance-1.0.0.jar
  • com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  • spring-aop-4.2.5.RELEASE.jar
  • spring-aspects-4.2.5.RELEASE.jar

注解类介绍

@Component:标准一个普通的spring Bean类。
@Repository:标注一个DAO组件类。
@Service:标注一个业务逻辑组件类。
@Controller:标注一个控制器组件类。

使用 @Component 注解定义Bean

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

// 通过注解定义一个DAO
<strong>@Component("userDao")
</strong>public class UserDaoImpl implements IUserDao {

    @Override
    public void addUser(User user) {
        // 这里并未实现完整数据库操作,仅为说明问题
        System.out.println("add user");
    }

}

@Component( "userDao" ) 的作用与在 XML 配置文件中编写 <bean id= "userDao" class= "dao.impl.UserDaoImpl" /> 等效。

使用 @Autowired 注解实现Bean组件装配

import org.springframework.beans.factory.annotation.Autowired;
<strong>import org.springframework.beans.factory.annotation.Qualifier;
</strong>import org.springframework.stereotype.Service;

<strong>@Service(</strong><strong>"userService")
</strong>public class UserServiceImpl implements IUserService {
    
    // 声明接口类型的引用和具体实现类解耦合
<strong>    @Autowired
    @Qualifier("userDao")
    </strong>private IUserDao dao;
    
    @Override
    public void addUser(User user) {
        dao.addUser(user);
    }
}

@Autowired 是根据类型进行自动装配的。如果当Spring上下文中存在不止一个 UserDao 类型的bean时,就会抛出 BeanCreationException 异常;如果 Spring 上下文中不存在 UserDao 类型的 Bean ,也会抛出 BeanCreationException 异常。我们可以使用@Qualifier 配合 @Autowired 来解决这些问题。

使用 Java 标准注解 @Resource 完成装配

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

<strong>@Service(</strong><strong>"userService")
</strong>public class UserServiceImpl implements IUserService {

    // 查找名为 dao 的 Bean,并注入给 dao 属性
<strong>   @Resource(name = "userDao")
   </strong>private IUserDao dao;

    @Override
    public void addUser(User user) {
        dao.addUser(user);        
    }

}

@Resource 有一个 name 属性,默认情况下,Spring 将这个属性的值解释为要注入的 Bean 的名称。

如果没有显示地指定 Bean 的名称,且无法找到与默认 Bean 名称匹配的 Bean 组件,@Resource 会由按名称查找的方式自动变为按类型匹配的方式进行装配。

使用注解定义切面

Spring 通过集成 AspectJ 实现了以注解的方式定义切面,大大减少了配置文件的工作量。AspectJ是一个面向切面的框架,它扩展了 Java 语言,定义了 AOP 语法,能够在编译期提供代码的织入,所以它有一个专门的编译器用来生成遵守字节编码规范的Class 文件。@AspectJ 是 AspectJ 5 新增的功能,使用 JDK 5.0 注解技术和正规的 AspectJ 切点表达式语言描述切面,因此在使用 @AspectJ 之间,需要保证所使用的 JDK 是 5.0 或其以上版本,否则无法使用注解技术。

要进行 AOP 编程,我们首先要在 Spring 配置文件中引入 aop 命名空间

 <?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"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-4.2.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
 
 </beans>

使用注解标注切面

首先看一下增强类代码

 import org.apache.log4j.Logger;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.AfterReturning;
 import org.aspectj.lang.annotation.AfterThrowing;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
 import org.springframework.stereotype.Component;
 
 <strong>@Aspect
</strong>12 <strong>@Component("serviceAdviceLogging")
</strong>13 public class ServiceAdviceLogging {
     private Logger logger = Logger.getLogger(ServiceAdviceLogging.class);
     
     // 注解定义切点 参数为定义切点的表达式部分
     <strong>@Pointcut("execution(* cn.zdpz.service..*.*(..))")
</strong>18     public void pointcutLogging(){}
     
     // 注解实现后置增强
     <strong>@AfterReturning("pointcutLogging()")
</strong>22     public void afterReturning(JoinPoint joinPoint){
         String simpleClassName = joinPoint.getSignature().getName();
         String methodName = joinPoint.getTarget().getClass().getName();
         logger.info(simpleClassName+"-----"+methodName+"后置增强方法");
     }
     
     // 注解实现异常增强
     <strong>@AfterThrowing(pointcut="pointcutLogging()",throwing="e")
</strong>30     public void afterThrowing(JoinPoint joinPoint,Exception e){
         String simpleClassName = joinPoint.getSignature().getName();
         String methodName = joinPoint.getTarget().getClass().getName();
         logger.info(simpleClassName+"-----"+methodName+"出现异常:"+e.getMessage());
     }
     
     // 注解实现环绕增强
     <strong>@Around("pointcutLogging()")
</strong>38     public Object around(ProceedingJoinPoint proceedingJoinPoint){
      Object obj = null;
         try {
             System.out.println("环绕增强前:");
             obj = proceedingJoinPoint.proceed();
             System.out.println("环绕增强后:");
         } catch (Throwable e) {
             e.printStackTrace();
         }
      return obj;
     }
 }

上面的代码

使用 @Aspect 注解将 ServiceAdviceLogging 定义为切面

使用 @AfterReturning 注解将 afterReturning() 方法定义为后置增强

使用 @AfterThrowing 注解将 afterThrowing() 方法定义为异常增强

使用 @Around 注解将 around() 方法定义为环绕增强

一定要看的 注意点总结

1.被注解的 Java 类当做 Bean 实例,Bean 实例的名称默认是 Bean 类的首字母小写,其他部分不变。@Service 也可以自定义 Bean 名称,但是必须是唯一的!

2.尽量使用对应组件注解的类替换 @Component 注解,在 spring 未来的版本中,@Controller,@Service,@Repository会携带更多语义。并且便于开发和维护。

3.指定了某些类可作为 Spring Bean 类使用后,最好还需要让spring搜索指定路径,在Spring配置文件加入如下配置:


<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan base-package="org.springframework.*"/>

4.使用 AOP 编程要通过 aop 命名空间的 <aop:aspectj-autoproxy /> 声明自动为 spring 容器中那些配置 @aspectJ 切面的 bean 创建代理,织入切面。

<!-- 自动为切面方法中匹配的方法所在的类生成代理对象。 -->
<aop:aspectj-autoproxy />

下次更新MyBatis+Spring整合