使用注解进行AOP编程步骤:
1.配置spring配置文件,启用spring注解AOP编程
2.定义切面点
@Pointcut("execution(* com.xiaogang.service.impl.ServiceImpl.*(..))")
public void anyMethod(){}
3.引用切面点
@Before(value="anyMethod()")
public void preMethod(){
System.out.println("前置通知");
}
spring配置文件:
示例代码:
执行的切面点的方法:
dao层、service层代码省略
测试代码:
使用xml配置文件进行AOP编程步骤:
spring配置文件:
java代码:
dao层、service代码略;
测试代码如上。
1.配置spring配置文件,启用spring注解AOP编程
2.定义切面点
@Pointcut("execution(* com.xiaogang.service.impl.ServiceImpl.*(..))")
public void anyMethod(){}
3.引用切面点
@Before(value="anyMethod()")
public void preMethod(){
System.out.println("前置通知");
}
spring配置文件:
<?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: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">
<!-- 启用spring注解AOP编程 -->
<aop:aspectj-autoproxy/>
<bean id="daoImpl" class="com.xiaogang.dao.impl.DaoImpl" />
<bean id="serviceImpl" class="com.xiaogang.service.impl.ServiceImpl">
<property name="daoImpl" ref="daoImpl" />
</bean>
<bean id="MyAddtionalService" class="com.xiaogang.aspectj.MyAddtionalService" />
</beans>
示例代码:
执行的切面点的方法:
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;
/*
* 切面:spring中所有的切面点都是方法的调用
*/
@Aspect
public class MyAddtionalService {
//定义切面点
@Pointcut("execution(* com.xiaogang.service.impl.ServiceImpl.*(..))")
public void anyMethod(){}
@Before(value="anyMethod()")
public void preMethod(){
System.out.println("前置通知");
}
@After(value="anyMethod()")
public void backMethod(){
System.out.println("后置通知");
}
@AfterReturning("anyMethod()")
public void afterReturn(){
System.out.println("最终通知");
}
@AfterThrowing("anyMethod()")
public void throwhandler(){
System.out.println("异常通知");
}
@Around("anyMethod()")
public Object around(ProceedingJoinPoint pjp){
Object object=null;
System.out.println("进入方法");
try {
object=pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("退出方法");
return object;
}
}
dao层、service层代码省略
测试代码:
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xiaogang.service.IService;
public class SpringTest {
static ApplicationContext ctx;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ctx = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void testSave(){
IService service = (IService)ctx.getBean("serviceImpl");
service.save();
}
@Test
public void testHello(){
IService service = (IService)ctx.getBean("serviceImpl");
service.hello();
}
}
使用xml配置文件进行AOP编程步骤:
spring配置文件:
<?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: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">
<bean id="daoImpl" class="com.xiaogang.dao.impl.DaoImpl" />
<bean id="serviceImpl" class="com.xiaogang.service.impl.ServiceImpl">
<property name="daoImpl" ref="daoImpl" />
</bean>
<bean id="MyAddtionalService" class="com.xiaogang.aspectj.MyAddtionalService" />
<aop:config>
<aop:aspect id="aspectBean" ref="MyAddtionalService">
<!-- 定义切面 -->
<aop:pointcut id="mycut"
expression="execution(* com.xiaogang.service.impl.ServiceImpl.*(..))"/>
<!-- 定义通知:通知类型和方法名称 切面引用 -->
<aop:after method="backMethod" pointcut-ref="mycut"/>
<aop:before method="preMethod" pointcut-ref="mycut"/>
<aop:after-returning method="afterReturn" pointcut-ref="mycut"/>
<aop:around method="around" pointcut-ref="mycut"/>
<aop:after-throwing method="throwhandler" pointcut-ref="mycut"/>
</aop:aspect>
</aop:config>
</beans>
java代码:
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;
/*
* 切面:spring中所有的切面点都是方法的调用
*/
public class MyAddtionalService {
public void preMethod() {
System.out.println("前置通知");
}
public void backMethod() {
System.out.println("后置通知");
}
public void afterReturn() {
System.out.println("最终通知");
}
public void throwhandler() {
System.out.println("异常通知");
}
public Object around(ProceedingJoinPoint pjp) {
Object object = null;
System.out.println("进入方法");
try {
object = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("退出方法");
return object;
}
}
dao层、service代码略;
测试代码如上。