admin管理员组文章数量:1318048
小司机带你优雅的实现AOP,IOC
大多数时候我们使用spring任然是使用XML的配置方式,申明id对应类等等,再依次getBean等等
这个过程比较繁琐且并不美观,那么小编今天带你使用纯注解方式实现AOP, IOC,该技能赶紧get起来
首先定一个服务类:
@Component
public class Service {public void sayWhat(String str) {System.out.println("A says " + str);}}
我们需要在调用sayWhat时进行切面操作
再定定义一个拦截器也就是切面了:
package com.demo.aopb;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class Interceptor {@Pointcut("execution(* com.demo.aopb.Service.*(..))")private void cutPoint() {}@Before("cutPoint() && args(str)")public void doAccessCheck(String str) {System.out.println("前置通知 name=" + str);}@AfterReturning("cutPoint()")public void doAfter() {System.out.println("后置通知");}@After("cutPoint()")public void last() {System.out.println("最终通知");}@AfterThrowing("cutPoint()")public void doAfterThrow() {System.out.println("例外通知");}@Around("cutPoint()")public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {System.out.println("进入环绕通知");Object object = pjp.proceed();// 执行该方法System.out.println("退出方法");return object;}
}
最后是用注解替换掉我们陈旧的xml配置
package com.demo.aopb;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration
@ComponentScan(basePackages = "com.demo.aopb")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AppConfig {
}
最后再来个测试:
package com.demo.aopb;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class Main {@Autowiredprivate Service service;@Testpublic void test() {service.sayWhat("wwwww");}}
跑一个,最后结果:
三月 23, 2017 12:05:03 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
三月 23, 2017 12:05:03 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
信息: Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
三月 23, 2017 12:05:03 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
三月 23, 2017 12:05:03 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
信息: Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
三月 23, 2017 12:05:03 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@163e9ab, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1531931, org.springframework.test.context.support.DirtiesContextTestExecutionListener@25e70]
三月 23, 2017 12:05:03 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@181a1bc: startup date [Thu Mar 23 12:05:03 CST 2017]; root of context hierarchy
进入环绕通知
前置通知 name=wwwww
A says wwwww
退出方法
最终通知
后置通知
转载请注明出处:
本文标签: 小司机带你优雅的实现AOPIOC
版权声明:本文标题:小司机带你优雅的实现AOP,IOC 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1701492663a417816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论