# AOP實現方式二-自定義類來實現【主要是切面定義】 ###### tags: `Spring-AOP` 1.切面:橫切關注點,被模塊化的特殊對象,即是一個類 ```java= public class DiyPointCut { public void before(){ System.out.println("方法執行前"); } public void after(){ System.out.println("方法執行後"); } } ``` 2.在applicationContext.xml,將自定義類導入 並且去定義**切入點**以及**通知**切入點 ```xml= <!--方式二:自定義類--> <bean id="diy" class="com.kuang.diy.DiyPointCut"/> <aop:config> <!--自定義切面:ref要引用的類--> <aop:aspect ref="diy"> <!--切入點--> <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/> <!--通知--> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config> ``` 3.實驗類 ```java= public class MyTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //注意點:動態代理代理的是接口 UserService userService = (UserService) context.getBean("userService"); userService.add(); } } ``` 4.結果 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up