原标题:Spring AOP 的两种配置方式 XML和注解配置
一
注解配置AOP
注解配置AOP,大致分为三步:
1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2. 开发需要被拦截的类。
3. 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AoP容器管理。
第1步
使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before、@Around、@AfterReturning、@After、@AfterThrowing)。
第2步
开发需要被拦截的类。
第3步
将切面配置到xml中,当然也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AoP容器管理。
下面是一个例子demo:
beany.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xmlns:context="http://www.springframework.org/schema/context "
xmlns:aop="http://www.springframework.org/schema/aop "
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
scope="singleton">
PersonService.java
package com.Aop.service;
public interface PersonService {
public String save(String user);
public void update(String user);
}
PersonServiceBean.java
package com.Aop.service.impl;
import com.Aop.service.PersonService;
public class PersonServiceBean implements PersonService {
private String user = null;
/**
* @param user
*/
public PersonServiceBean(String user) {
this.user = user;
}
public PersonServiceBean() {}
/**
* @return the user
*/
public String getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(String user) {
this.user = user;
}
public String save(String user){
System.out.println("PersonServiceBean.save() is running");
return "Jamson";
}
public void update(String user){
System.out.println("PersonServiceBean.update() is running");
}
}
切面的定义
package com.Aop.inteceptor;
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;
@Aspect
public class InteceptorClass {
//这里的定义步骤:返回类型 package.class.method(parameter)
@Pointcut("execution (* com.Aop.service.impl.PersonServiceBean.*(..))")
private void myPointCutMethod(){};
@Before("myPointCutMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println("before advice:"+name);
}
@AfterReturning(pointcut="myPointCutMethod()", returning="result")
public void doWriteLog(String result){
System.out.println("after advice"+":"+result);
}
@After("myPointCutMethod()")
public void doMemoryClear(){
System.out.println("finally advice");
}
@AfterThrowing(pointcut="myPointCutMethod()", throwing="e")
public void doWriteErrorLog(Exception e){
System.out.println("Exception advice");
}
@Around("myPointCutMethod()")
public Object doAroundMethod(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("enter around advice method.");
Object obj = pjp.proceed();
System.out.println("exit around advice method.");
return obj;
}
}
测试类
package junit.test;
import org.junit.BeforeClass;
import com.Aop.service.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringAOPTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("beany.xml");
PersonService personService = (PersonService)context.getBean("personService");
personService.save("Jamson");
}
}
二
XML配置AOP
XML配置开发AOP,分为四步:
第1步
Service层的开发:PersonService.java/PersonServiceBean.java 同注解方式。
第2步
切面的开发。
代码
package com.Aop.inteceptor;
import org.aspectj.lang.ProceedingJoinPoint;
public class InteceptorXML {
public void doAccessCheck(){
System.out.println("before advice");
}
public void doWriteLog(){
System.out.println("after advice"+":");
}
public void doMemoryClear(){
System.out.println("finally advice");
}
public void doWriteErrorLog(){
System.out.println("Exception advice");
}
public Object doAroundMethod(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("enter around advice method.");
Object obj = pjp.proceed();
System.out.println("exit around advice method.");
return obj;
}
}
第3步
XML的配置。
代码
第4步
测试。
代码
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.Aop.service.PersonService;
public class SpringAOPXMLTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)context.getBean("personService");
personService.save("Jamson");
}
}
文章来源:
http://club.1688.com/article/22918141.html
Java程序员联盟
微信号:
javalm
(←长按复制)
倾力打造Java程序员联盟第一品牌返回搜狐,查看更多
责任编辑: