一、创建maven项目springAOP
pom.xml:
4.0.0
com.sheng.example
springAOP
war
1.0-SNAPSHOT
springAOP Maven Webapp
http://maven.apache.org
UTF-8
5.1.0.Final
4.3.2.RELEASE
junit
junit
4.12
test
org.hibernate
hibernate-core
${hibernate.version}
org.hibernate
hibernate-ehcache
${hibernate.version}
net.sf.ehcache
ehcache
2.10.1
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-test
${spring.version}
test
org.aspectj
aspectjweaver
1.8.9
org.springframework
spring-websocket
${spring.version}
org.springframework
spring-messaging
${spring.version}
org.springframework
spring-webmvc
${spring.version}
commons-fileupload
commons-fileupload
1.3.1
com.fasterxml.jackson.core
jackson-core
2.8.2
com.fasterxml.jackson.core
jackson-databind
2.8.2
c3p0
c3p0
0.9.1.2
org.springframework
spring-orm
${spring.version}
mysql
mysql-connector-java
6.0.3
org.apache.commons
commons-dbcp2
2.1.1
com.alibaba
fastjson
1.2.12
org.apache.maven.plugins
maven-compiler-plugin
1.7
web.xml:
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:ApplicationContext.xml
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
true
1
dispatcherServlet
/
index.jsp
# Set root logger level to error
log4j.rootLogger=INFO, Console, File
###### Console appender definition #######
# All outputs currently set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c{3}] %m%n
#log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n
###### File appender definition #######
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=spring.log
log4j.appender.File.Append=false
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n
package com.sheng.example.springaop.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* Created by Dell on 2017/1/20.
*/
public class TestAspect {
public void doAfter(JoinPoint jp) {
System.out.println("log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");
return retVal;
}
public void doBefore(JoinPoint jp) {
System.out.println("log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println("method " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw exception");
System.out.println(ex.getMessage());
}
}
TestAnnotationAspect.java 基于注解的切面
package com.sheng.example.springaop.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
* Created by Dell on 2017/1/20.
*/
@Aspect
public class TestAnnotationAspect {
@Pointcut("execution(* com.sheng.example.springaop.service.*.*(..))")
private void pointCutMethod() {
}
//声明前置通知
@Before("pointCutMethod()")
public void doBefore() {
System.out.println("前置通知");
}
//声明后置通知
@AfterReturning(pointcut = "pointCutMethod()", returning = "result")
public void doAfterReturning(String result) {
System.out.println("后置通知");
System.out.println("---" + result + "---");
}
//声明例外通知
@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知");
System.out.println(e.getMessage());
}
//声明最终通知
@After("pointCutMethod()")
public void doAfter() {
System.out.println("最终通知");
}
//声明环绕通知
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入方法---环绕通知");
Object o = pjp.proceed();
System.out.println("退出方法---环绕通知");
return o;
}
}
package com.sheng.example.springaop.service;
/**
* Created by Dell on 2017/1/20.
*/
public interface AOPService {
String aoptest();
}
package com.sheng.example.springaop.service.impl;
import com.sheng.example.springaop.service.AOPService;
import org.springframework.stereotype.Service;
/**
* Created by Dell on 2017/1/20.
*/
@Service
public class AOPServiceImpl implements AOPService {
@Override
public String aoptest() {
return "aoptest";
}
}
MyController.java:
package com.sheng.example.springaop.controller;
import com.sheng.example.springaop.service.AOPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Dell on 2017/1/20.
*/
@RestController
public class MyController {
@Autowired
private AOPService aopService;
@RequestMapping("/springaop")
public String springAop(){
return aopService.aoptest();
}
}
可以看到控制台输出:
log Begining method: com.sheng.example.springaop.service.impl.AOPServiceImpl.aoptest
进入方法---环绕通知
前置通知
后置通知
---aoptest---
最终通知
退出方法---环绕通知
process time: 0 ms
log Ending method: com.sheng.example.springaop.service.impl.AOPServiceImpl.aoptest
MyTest.java
package com.sheng.example.test;
import com.sheng.example.springaop.service.AOPService;
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;
/**
* Created by Dell on 2017/1/20.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:ApplicationContext.xml")
public class MyTest {
@Autowired
private AOPService aopService;
@Test
public void testAOP(){
System.out.println(aopService.aoptest());
}
}