标签:style blog http color io 使用 ar java for
日志,安全和事务管理都是AOP可以应用的地方。
分布于应用中多处的功能被称为横切关注点。
切点参数的含义:
execution(* com.spring.service.AService.*(..))
第一个*表示任意的返回类型,com.spring.service.AService制定了一个接口,第二*表示包含接口的任意方法,..表示任意的参数类型。指定了接口AService,则对实现了接口的任意子类都包含在内。
execution(* com.spring.service.AService.*(..)) && within(com.spring.service.impl.*)
表示同时要是com.spring.service.impl包下面的AService接口的实现类。表达式间的操作符包括&&,||,!或者and,or,not。
如果需要知道一个方法执行花费了多长时间,环绕通知是很合适来完成的。
为切点增加参数信息execution(* com.spring.service.AService.*(String)) and args(thoughts),将AService实现类方法中的String类型的thoughts作为参数传递给拦截器。
使用注解时@Pointcut需要依附于一个方法,方法本身不重要,可以是空实现。
基于注解及自动扫描实现IoC和AOP。
配置文件contextByAspect.xml的信息:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <context:annotation-config /> <!-- 自动扫描(自动注入) --> <context:component-scan base-package="com.spring..*"/> <aop:aspectj-autoproxy/> </beans>
定义接口类:
package com.spring.service;
/**
* 接口A
*/
public interface AService {
public void fooA(String _msg);
public void barA();
}
定义实现类:
package com.spring.service.impl;
import org.springframework.stereotype.Component;
import com.spring.service.AService;
/**
*接口A的实现类
*/
@Component("aService")
public class AServiceImplByAspect implements AService {
public AServiceImplByAspect(){
System.out.println("a begin!");
}
public void barA() {
System.out.println("AServiceImpl.barA()");
}
public void fooA(String _msg) {
System.out.println("AServiceImpl.fooA(msg:"+_msg+")");
}
}
定义切面:
package com.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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 TestAspectByAspect {
@Pointcut(value = "execution(* com.spring.service.AService.*(..))")
public void before(){
}
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;
}
@Before(value = "before()")
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());
}
private void sendEx(String ex) {
//TODO 发送短信或邮件提醒
}
}
定义单元测试类:
package com.spring.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import com.spring.service.AService;
import com.spring.service.impl.BServiceImplByAspect;
public class AOPTestByAspect extends AbstractDependencyInjectionSpringContextTests {
@Autowired(required = false)
private AService aService;
protected String[] getConfigLocations() {
String[] configs = new String[] { "/contextByAspect.xml"};
return configs;
}
/**
* 测试正常调用
*/
public void testCall()
{
System.out.println("SpringTest JUnit test");
aService.fooA("JUnit test fooA");
aService.barA();
}
public void setAService(AService service) {
aService = service;
}
}
标签:style blog http color io 使用 ar java for
原文地址:http://www.cnblogs.com/lnlvinso/p/4001972.html