Spring教程
1、业务类
package spring.example.aop; import org.springframework.stereotype.Component; @Component public class Dao { public void insert(){ System.out.println("insert ..."); } }
2、通知
package spring.example.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class Log { @Before("execution(* spring.example.aop.Dao.insert(..))") public void log() { System.out.println("log around ..."); } }
3、切入配置
package spring.example.aop; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @ComponentScan(value ="spring.example.aop" ) @EnableAspectJAutoProxy(proxyTargetClass = true) public class Config { }
4、主类
package spring.example.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context=new AnnotationConfigApplicationContext(Config.class); Dao dao = context.getBean("dao", Dao.class); dao.insert(); } }