Spring AOP编程实例
2015-05-14 12:21
337 查看
摘要: AOP 可以解耦合,将业务之间的代码完全隔离但是却可以通过注解进行交互。spring AOP真的很好用。
整个类包如下:
测试结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
开始around添加日志
before添加日志execution(Integer com.yuan.service.UserService.add(String))
添加用户信息1。123oscar
结束around添加日志
after添加日志
比如:
通知类-用于说明切面类具有的功能.MethodBeforeAdvice
切面类-用于说明具体的需要切入的功能,这个是重点要做的,比如添加日志的功能就在这里log。
代理对象-用于申明要往哪里去切入日志功能的业务逻辑类proxyFactory。
被代理对象-具体说明业务逻辑模块。比如Userserviceimpl 用于实现添加用户等功能。
这里首先申明了切面类,
然后申明了业务逻辑类,
然后申明了AOP配置,配置切点和切面以及切入的方法
整个类包如下:
一、通过注释实现切面编程,如下是具体各个类
1.1前置通知类
注意:如果采用了AOP配置文件,这个前置通知就不需要写了,只需要通过注释说明就可以了。前置通知在切面类里面申明。package com.yuan.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class MymethodBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { // TODO Auto-generated method stub } }
1.2 业务
package com.yuan.service; public interface UserService { public Integer add(String abc); public void aduser(String abc); }
1.3 业务实现
package com.yuan.service.impl; import org.springframework.stereotype.Service; import com.yuan.service.UserService; @Service public class UserServiceimpl implements UserService { @Override public Integer add(String abc) { System.out.println("添加用户信息1。"+abc); return 123; } @Override public void aduser(String abc) { // TODO Auto-generated method stub System.out.println("添加用户信息2。"); } }
1.4切面类-前置通知和后置通知以及环绕通知都在这里配置
package com.yuan.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect //切面类 public class Log { /*** * 配置切入点,该方法无方法体 * 注意切入申明的格式,execution(Integer(返回的类型) com.yuan.service.UserService.add(String)) * */ @Pointcut("execution(* com.yuan.service..ad*(..))")//申明切人点,一个切入点会有很多的连接点 public void daddff(){};//配置切入点,该方法无方法体 /*** * 配置前置通知 * @param joinpoint */ @Before("daddff()") public void dddbefore1(JoinPoint joinPoint){ System.out.println("before添加日志"+joinPoint); } /*** * 配置后置通知 * @param joinpoint */ @After("daddff()") public void after(){ System.out.println("after添加日志"); } /*** * 配置环绕通知 * @param joinpoint */ @Around("daddff()") public void around(JoinPoint joinPoint){ System.out.println("开始around添加日志"); ProceedingJoinPoint pjp=(ProceedingJoinPoint)joinPoint; try { pjp.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("结束around添加日志"); } }
1.5 配置文件bean.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com" /> <aop:aspectj-autoproxy/>//采用了注释实现自动装配实现代理 </beans>
1.6测试类
package com.yuan.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yuan.service.UserService; public class AopTest { public static void main(String[] args){ ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ac.getBean("userServiceimpl"); userService.add("123oscar"); //userService.aduser("456"); } }
测试结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
开始around添加日志
before添加日志execution(Integer com.yuan.service.UserService.add(String))
添加用户信息1。123oscar
结束around添加日志
after添加日志
1.7 说明
如果不采用spring的配置文件以及注释来申明的话,就需要实现很多的类。比如:
通知类-用于说明切面类具有的功能.MethodBeforeAdvice
切面类-用于说明具体的需要切入的功能,这个是重点要做的,比如添加日志的功能就在这里log。
代理对象-用于申明要往哪里去切入日志功能的业务逻辑类proxyFactory。
被代理对象-具体说明业务逻辑模块。比如Userserviceimpl 用于实现添加用户等功能。
二、通过aop配置文件来申明切面类,不用注释
如果不用任何注释。这里首先申明了切面类,
然后申明了业务逻辑类,
然后申明了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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- <context:component-scan base-package="com" /> <aop:aspectj-autoproxy/>--> <!-- 切面类 --> <bean id="log" class="com.yuan.aop.Log1"></bean> <!-- 用户管理 --> <bean id="userServiceimpl" class="com.yuan.service.impl.UserServiceimpl"></bean> <!-- aop --> <aop:config> <!-- 切点 --> <aop:pointcut expression="execution(* com.yuan.service..*.*(..))" id="pointcut"/> <aop:aspect ref="log"> <!-- 通知 --> <aop:before method="dddbefore1" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>
2.1 如下是去掉注释的切面类
package com.yuan.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; @Component public class Log1 { /*** * 配置切入点,该方法无方法体 * */ public Integer dddbefore1(JoinPoint joinPoint){ System.out.println("before添加日志"+joinPoint); return 123; } public void after(){ System.out.println("after添加日志"); } public void around(JoinPoint joinPoint){ System.out.println("开始around添加日志"); ProceedingJoinPoint pjp=(ProceedingJoinPoint)joinPoint; try { pjp.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("结束around添加日志"); } }
相关文章推荐
- spring aop切面编程基础实例
- spring aop 2.0 编程(二)一个实例
- spring aop 2.0 编程(二)一个实例
- SpringAOP编程实例
- JAVA_WEB项目之Spring中使用AOP编程运用到Lucene中实现解耦
- 【spring-boot】spring aop 面向切面编程初接触
- Spring面向切面编程——Spring实现AOP方式——自定义类实现
- 黑马程序员-AOP代理以及类spring的Aop框架实例
- Spring AOP编程的教程
- 通过实例理解Spring的Bean工厂和AOP框架
- Spring AOP切面解决数据库读写分离实例详解
- 反射实现 AOP 动态代理模式实例说明(Spring AOP 的实现 原理)
- Spring AOP: Spring之面向方面编程
- Spring的AOP编程
- spring的AOP(四)----Spring AOP 编程(Advisor)
- Spring Aop切面编程配置与实现
- Spring第五篇【cglib、手动实现AOP编程】
- Spring Core Programming(Spring核心编程) - AOP Concepts(AOP基本概念)
- Spring 中 AOP 思想实例解析
- spring-AOP编程出现org.springframework.beans.factory.BeanCreationException