spring18-4: spring aop
生活随笔
收集整理的這篇文章主要介紹了
spring18-4: spring aop
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
切面?
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation;public class MyAspect implements MethodInterceptor{@Overridepublic Object invoke(MethodInvocation mi) throws Throwable {System.out.println("前");// 手動執行目標方法Object obj = mi.proceed();System.out.println("后");return obj;} }?接口
public interface UserService {void addUser();void updateUser(); }實現類?
public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("config adduser...");}@Overridepublic void updateUser() {System.out.println("config updateUser...");} }?配置文件
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 1.創建目標類 --><bean id="userService" class="com.atchina.c_spring_aop.UserServiceImpl"/><!-- 2.創建切面類 --><bean id="myAspect" class="com.atchina.c_spring_aop.MyAspect"/><!-- 3. aop編程使用<aop:config>配置<aop:pointcut> 切入點<aop:advisor>特殊的切面advice-ref="" 通知的引用pointcut-ref="" 切入點的引用expression="" 切入點表達式--> <aop:config><aop:pointcut expression="execution(* *.*(..))" id="myPointCut"/><aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut" /></aop:config> </beans>測試類:
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactoryBean {@Testpublic void test(){String xmlPath = "com/atchina/c_spring_aop/applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);UserService userService = (UserService)ac.getBean("userService");userService.addUser();} }?
總結
以上是生活随笔為你收集整理的spring18-4: spring aop的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring18-3: 工厂bean代理
- 下一篇: spring28: aspectJ--基