Spring AOP实现方式三之自动扫描注入【附源码】
注解AOP实现 这里唯一不同的就是application 里面
不需要配置每个bean都需要配置了,直接自动扫描
注册,主要知识点是怎么通过配置文件得到bean,
注意类前面的@注解。
源码结构:
1、首先我们新建一个接口,love 谈恋爱接口。
package com.spring.aop; /** * 谈恋爱接口 * * @author Administrator * */ public interface Love { /* * 谈恋爱方法 */ void fallInLove(); void test() throws Exception; }
2、我们写一个Person类实现Love接口 类声明前面加上了
@Component 不加这个我们无法通过载入xml配置文件找到。
package com.spring.aop; import org.springframework.stereotype.Component; /** * 人对象 * * @author luwenbin006@163.com * */ @Component public class Person implements Love { /* * 重写谈恋爱方法 */ @Override public void fallInLove() { System.out.println("谈恋爱了..."); } @Override public void test() throws Exception { // TODO Auto-generated method stub throw new Exception("我就说你们在一起不会幸福的,你能拿我怎么滴?"); } }
3、下面我们来写aop 注解通知类 和前面有点不同哦 @Aspect前面加上了@Service
【执行方法前 执行方法后 执行方法前后也称为环绕方法 方法执行过程中抛出异常】
package com.spring.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterThrowing; 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.Service; /** * 注解方式 aop通知类 * * @author luwenbin006@163.com * */ @Service @Aspect public class LoveHelper { @Pointcut("execution(* com.spring.aop.*..*(..))") private void loveMethod() { }// 定义一个切入点 // 在调用方法之前执行 执行拦截包com.spring.aop.*下所有的方法 @Before("execution(* com.spring.aop.*..*(..))") public void before(JoinPoint point) throws Throwable { System.out.println("before::method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName()); System.out.println("谈恋爱之前必须要彼此了解!"); } // 在调用方法前后执行 @Around("execution(* com.spring.aop.*..*(..))") public Object around(ProceedingJoinPoint point) throws Throwable { System.out.println("around::method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName()); if (point.getArgs().length > 0) { return point.proceed(point.getArgs()); } else { return point.proceed(); } } // 在调用方法之后执行 @After("execution(* com.spring.aop.*..*(..))") public void afterReturning(JoinPoint point) throws Throwable { System.out.println("method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName()); System.out.println("我们已经谈了5年了,最终还是分手了!"); // System.out.println("我们已经谈了5年了,最终步入了结婚的殿堂!"); } // 当抛出异常时被调用 @AfterThrowing(value = "execution(* com.spring.aop.*..*(..))", throwing = "ex") public void doThrowing(JoinPoint point, Throwable ex) { System.out.println("doThrowing::method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName() + " throw exception"); System.out.println(ex.getMessage()); } }
4、配置好application.xml 就配置好bean和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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!--组件扫描 --> <context:component-scan base-package="com.spring.aop" /> <aop:aspectj-autoproxy /> <!-- //定义扫描根路径为leot.test,不使用默认的扫描方式 <context:component-scan base-package="com.spring.aop" use-default-filters="false"> // 扫描符合@Service @Repository的类 <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> --> </beans>
5、编写我们spring getBean 辅助工具类 两种方式, 一种是实现 ApplicationContextAware
接口 一种是继承ApplicationObjectSupport
1、实现ApplicationContextAware接口
package com.spring.aop.springUtil; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Service; @Service public class SpringContextHelper implements ApplicationContextAware { private ApplicationContext context; // 提供一个接口,获取容器中的Bean实例,根据名称获取 public Object getBean(String beanName) { return context.getBean(beanName); } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { this.context = context; } }
2、继承ApplicationObjectSupport类
package com.spring.aop.springUtil; import org.springframework.context.support.ApplicationObjectSupport; import org.springframework.stereotype.Service; @Service public class SpringContextHelper2 extends ApplicationObjectSupport { // 提供一个接口,获取容器中的Bean实例,根据名称获取 public Object getBean(String beanName) { return getApplicationContext().getBean(beanName); } }
6、我们之前的测试类也还是可以运行的哦~~~
package com.spring.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.aop.Love; public class LoveTest { public static void main(String[] args) { ApplicationContext appCtx = new ClassPathXmlApplicationContext( "applicationContext.xml"); Love love = (Love) appCtx.getBean("person"); love.fallInLove(); try { // 测试异常捕获 love.test(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
7、或者写上Junit测试类
package com.love.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.aop.Love; import com.spring.aop.springUtil.SpringContextHelper; import com.spring.aop.springUtil.SpringContextHelper2; import junit.framework.TestCase; public class LoveAopTest extends TestCase { private SpringContextHelper helper; private SpringContextHelper2 helper2; ApplicationContext context; protected void setUp() { /* 开始test前的准备操作:初始化,获取数据连接... */ context = new ClassPathXmlApplicationContext("applicationContext.xml"); helper = (SpringContextHelper) context.getBean("springContextHelper"); helper2 = (SpringContextHelper2) context .getBean("springContextHelper2"); } protected void tearDown() { /* 完成test后的清理工作:关闭文件, 关闭数据连接... */ } public void testCase2() { Love love = (Love) helper.getBean("person"); love.fallInLove(); try { // 测试异常捕获 love.test(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); assertTrue(true); } } public void testCase3() { Love love = (Love) helper2.getBean("person"); love.fallInLove(); try { // 测试异常捕获 love.test(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); assertTrue(true); } } }
8、看控制台输出结果 调用了我们定义的aop拦截方法~~~ ok了
9、源码下载地址:源码下载