First introduce the jar package
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; @ Componentpublic class OkpService {private OkpDao okpDao; public OkpDao getOkpDao () {return okpDao;} @ Resource (name = "okpDaoImpl") public void setOkpDao. okpDao = okpDao;} public void save () {this. okpDao. save ();} public void update () {this. okpDao. update () ;}@ PostConstructpublic void init () {System. out. println ("Run before container creation") ;}@ PreDestroypublic void destory () {System. out. println ("executed after container destruction ");}}
4. face Cutting
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 @ Componentpublic class SaveInter {@ Pointcut ("execution (* com. service. okpService. *(..)) ") public void method () {}; @ Before (" method () ") public void before () {System. out. println ("pre-notification") ;}@ AfterReturning ("method ()") public void doAfter () {System. out. println ("post notification") ;}@ After ("method ()") public void after () {System. out. println ("final notification ");}}
@ Aspect face Cutting class
@ Component Load the class to the spring container
@ Poincut defines a plane
@ Before: Before the method in the execution plane is executed
@ AfterReturning: Execution completed
@ After is equivalent to finally in try {} catch {} finally {} in the method. It is executed before the method is executed.
5. Testing
package com.service;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class OkpServiceTest {@Testpublic void testSave() {BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");OkpService okp=(OkpService) bf.getBean("okpService");okp.save();okp.update();}}
6. Configuration File
To use the Aop annotation, add <aop: aspectj-autoproxy> </aop: aspectj-autoproxy> to the xml configuration file.
Running result:
Log4j: WARN No appenders cocould be found for logger (org. springframework. context. support. ClassPathXmlApplicationContext ).
Log4j: WARN Please initialize the log4j system properly.
Run
Pre-notification
OkpDaoImpl. save ()
Final notification
Post notification
Pre-notification
OkpDaoImpl. update ()
Final notification
Post notification