首先引入jar包
aspectjrt.jar
aspectjweaver.jar
1、dao
package com.dao;
public interface OkpDao {
public void save();
public void update();
}
2、impl
package com.dao.impl;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.dao.OkpDao;
@Component("okpDaoImpl")
public class OkpDaoImpl implements OkpDao{
public void save() {
System.out.println("OkpDaoImpl.save()");
}
public void update() {
System.out.println("OkpDaoImpl.update()");
}
}
3、service
package com.service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.dao.OkpDao;
@Component
public class OkpService {
private OkpDao okpDao;
public OkpDao getOkpDao() {
return okpDao;
}
@Resource(name="okpDaoImpl")
public void setOkpDao(OkpDao okpDao) {
this.okpDao = okpDao;
}
public void save(){
this.okpDao.save();
}
public void update(){
this.okpDao.update();
}
@PostConstruct
public void init(){
System.out.println("容器创建前执行");
}
@PreDestroy
public void destory(){
System.out.println("容器销毁后执行");
}
}
4、切面类
package com.service;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
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 SaveInter {
@Pointcut("execution(* com.service.OkpService.*(..))")
public void method(){};
@Before("method()")
public void before(){
System.out.println("前置通知");
}
@AfterReturning("method()")
public void doAfter(){
System.out.println("后置通知");
}
@After("method()")
public void after(){
System.out.println("最终通知");
}
}
@Aspect切面类
@Component 将该类加载到spring容器
@Poincut 定义一个切面
@Before 当执行切面中的方法前,会执行
@AfterReturning 方法执行完执行
@After 相当于方法中的try{}catch{}finally{}中的finally,执行完方法前会执行
5、测试类
package com.service;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class OkpServiceTest {
@Test
public void testSave() {
BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");
OkpService okp=(OkpService) bf.getBean("okpService");
okp.save();
okp.update();
}
}
6、配置文件
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
使用Aop注解,xml配置文件中要加入<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
运行结果:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
容器创建前执行
前置通知
OkpDaoImpl.save()
最终通知
后置通知
前置通知
OkpDaoImpl.update()
最终通知
后置通知