Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP
下面是使用AspectJ注解实现AOP的Java Project
首先是位于classpath下的applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> 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"> <!-- 启用AspectJ对Annotation的支持 --> <aop:aspectj-autoproxy/> <bean id="userManager" class="com.jadyer.annotation.UserManagerImpl"/> <bean id="securityHandler" class="com.jadyer.annotation.SecurityHandler"/> </beans>然后是服务层接口以及实现类
package com.jadyer.annotation; public interface UserManager { public void addUser(String username, String password); public void delUser(int userId); public String findUserById(int userId); public void modifyUser(int userId, String username, String password); } /** * 上面的UserManager是服务层的接口 * 下面的UserManagerImpl是服务层接口的实现类 */ package com.jadyer.annotation; public class UserManagerImpl implements UserManager { public void addUser(String username, String password) { System.out.println("------UserManagerImpl.addUser() is invoked------"); } public void delUser(int userId) { System.out.println("------UserManagerImpl.delUser() is invoked------"); } public String findUserById(int userId) { System.out.println("------UserManagerImpl.findUserById() is invoked------"); return "铁面生"; } public void modifyUser(int userId, String username, String password) { System.out.println("------UserManagerImpl.modifyUser() is invoked------"); } }接下来是使用AspectJ注解标注的切入类
package com.jadyer.annotation; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspectpublic class SecurityHandler { /** * 定义Pointcut * @see Pointcut的名称为addAddMethod(),此方法没有返回值和参数 * @see 该方法就是一个标识,不进行调用 */ @Pointcut("execution(* add*(..))") //匹配所有以add开头的方法 private void addAddMethod(){}; /** * 定义Advice * @see 表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上 */ //@Before("addAddMethod()") @After("addAddMethod()") private void checkSecurity() { System.out.println("------【checkSecurity is invoked】------"); } }最后是客户端测试类
package com.jadyer.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring对AOP的支持:采用Annotation方式 * @see ------------------------------------------------------------------------------------- * @see Spring提供的AOP功能还是很强大的,支持可配置,它的默认实现使用的就是JDK动态代理 * @see 使用Spring的AOP不需要继承相关的东西,也不需要实现接口 * @see 但有个前提条件:由于是JDK动态代理,所以若想生成代理,该类就必须得实现一个接口才行 * @see 如果该类没有implements接口的话,仍去使用Spring的默认AOP实现时,那么就会出错 * @see 通常需要生成代理的类都是服务层的类,所以通常都会抽一个接口出来。即养成面向接口编程的习惯 * @see ------------------------------------------------------------------------------------- * @see 采用Annotation方式完成AOP示例的基本步骤,如下 * @see 1、Spring2.0的依赖包配置。新增Annotation支持 * @see * SPRING_HOME//dist//spring.jar * @see * SPRING_HOME//lib//log4j//log4j-1.2.14.jar * @see * SPRING_HOME//lib//jakarta-commons//commons-logging.jar * @see * SPRING_HOME//lib//aspectj//*.jar * @see 2、将横切性关注点模块化,建立SecurityHandler.java * @see 3、采用注解指定SecurityHandler为Aspect * @see 4、采用注解定义Advice和Pointcut * @see 5、启用AspectJ对Annotation的支持,并且将目标类和Aspect类配置到IoC容器中 * @see 6、开发客户端 * @see ------------------------------------------------------------------------------------- */public class Client { public static void main(String[] args) { ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("userManager"); userManager.addUser("张起灵", "02200059"); } }下面是使用XML配置文件实现AOP的Java Project
首先是位于src根目录中的applicationContext-cglib.xml文件
首先是位于src根目录中的applicationContext-cglib.xml文件
.....轉自:http://www.jb51.net/article/95161.htm

浙公网安备 33010602011771号