AOP使用代码:
一、Dao层
接口:AopTestDao
package com.sshcp.dao; public interface AopTestDao { public void aopTest(); }
实现类:AopTestDaoImpl
package com.sshcp.dao.impl; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.sshcp.dao.AopTestDao; @Component("aopTestDao") public class AopTestDaoImpl implements AopTestDao { public void aopTest() { // TODO Auto-generated method stub System.out.println("dao 层 "); } }
service层:
接口类:AopServiceI
package com.sshcp.service.aopi; public interface AopServiceI { public void aopTestMothod(); }
实现类:AopServiceImpl
package com.sshcp.service.aopi.impl; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.sshcp.dao.AopTestDao; import com.sshcp.service.aopi.AopServiceI; @Component("aopServiceI") public class AopServiceImpl implements AopServiceI { private AopTestDao aopTestDao ; // 注入dao层AopTestDao public AopTestDao getAopTestDao() { return aopTestDao; } @Resource public void setAopTestDao(AopTestDao aopTestDao) { this.aopTestDao = aopTestDao; } public void aopTestMothod() { // TODO Auto-generated method stub System.out.println("aop test service here "); } }
切面类:AspectTest
package com.sshcp.aspect; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @org.aspectj.lang.annotation.Aspect @Component public class AspectTest { @Pointcut("execution(* com.sshcp.service.aopi.impl.AopServiceImpl.aop*(..)))") public void testMothod(){ } @Before("testMothod()") public void beforeMothe(){ System.out.println("121212121212121212 Before......."); } @After("testMothod()") public void afterMothe(){ System.out.println("2323232323232323232 After........"); } }
Spring xml 中加入如下配置:
<context:annotation-config /> <aop:aspectj-autoproxy /> <context:component-scan base-package="com.sshcp" />
测试代码:
package com.hibernate.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sshcp.service.aopi.AopServiceI; public class AopTest { @Test public void test(){ ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext-c3p0.xml"); AopServiceI as = (AopServiceI)app.getBean("aopServiceI"); as.aopTestMothod(); } }
测试打印出的信息:
121212121212121212 Before.......
aop test service here
2323232323232323232 After........
搞定
相关推荐
使用Spring配置文件实现AOP
一个介绍了AOP四种配置方式的Demo,方便您快速掌握AOP的配置。
详细讲解了aop的xml配置和注解的方式配置,和本人的博客想应
spring-aop标签和配置文件两种方式实例,spring要求2.5及以上
使用动态代理实现面向切面编程、基于AspectJ注解的方式配置AOP、基于XML文件的方式配置AOP
基于注解配置和使用spring AOP(spring mvc框架)
SpringAop xml方式配置通知
ssh添加aop配置简单实例,配置文件支持一个方法以及可支持匹配型多个方法
里面包含了多种Spring AOP配置,十分详细。
JBossAOP插件的安装与配置.exe
Spring 2.x aop配置详解 Spring 2.x aop配置详解 Spring 2.x aop配置详解 Spring 2.x aop配置详解 Spring 2.x aop配置详解 Spring 2.x aop配置详解
sping aop注解配置
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
Spring之AOP配置文件详解.txt Spring之AOP配置文件详解.txt 帮助你更快张握Java的学习!
SpringAop面向切面编程
AOP的相关概念,基于XML的AOP的配置,基于注解的AOP配置.
AOP基于注解配置.zip
Spring AOP配置的实例,最基本的Spring AOP配置
spring Aop配置示例说明,通过模拟mvc实现,service层异常补货.rar
Xml文件配置实现AOP通知