大多數時候我們使用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 {
@Autowired
private Service service;
@Test
public 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
退出方法
最終通知
後置通知
轉載請註明出處:http://blog.csdn.net/goodsave