AOP
- AOP(アスペクト指向プログラミング)は、手段:アスペクト指向プログラミング
- AOPは、ソフトウェア開発のホットスポットで、重要な要素は、Springフレームワークである、関数型プログラミングはタイプYanshengファンで、OOPの続きです
春AOPを実現使います
方法1:設定強化クラス
- 輸入依存度
<!--织入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
インタフェースを実装する書く2
package com.yang.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void serach();
}
- 実装クラスを書きます
import com.yang.service.UserService;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加用户");
}
public void delete() {
System.out.println("删除用户");
}
public void update() {
System.out.println("更新用户");
}
public void serach() {
System.out.println("查询用户");
}
}
- 強化された書き込みクラス、強化フロント、リア強化
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogAfter implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法,返回了"+returnValue);
}
}
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()
+"的"+method.getName()+"方法被执行了");
}
}
- オブジェクトを作成することと同じ、Beanにサインアップ
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="log" class="com.yang.config.LogAfter"/>
<bean id="afterlog" class="com.yang.config.LogAfter"/>
- AOPの設定ファイルを追加します。
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
- 設定AOP
<!--aop的配置-->
<aop:config>
<!--切入点,增强那些-->
<aop:pointcut id="pc-userservice" expression="execution(* com.yang.service.Impl.UserServiceImpl.*(..))"/>
<!--环绕-->
<aop:advisor advice-ref="log" pointcut-ref="pc-userservice"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pc-userservice"/>
</aop:config>
- テスト
@Test
public void test2(){
// bean就是创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
第二の方法:構成セクション、クラス(オブジェクトを作成することです豆)を強化するための方法があります
「実行(* com.yang.service.Impl.UserServiceImpl。*は、( ...))」
これは、オブジェクトを作成するための鍵である、に書き込む必要があります
- 輸入依存度
- 実装クラスを書くための書き込みインタフェース
package com.yang.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void serach();
}
package com.yang.service.Impl;
import com.yang.service.UserService;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加用户");
}
public void delete() {
System.out.println("删除用户");
}
public void update() {
System.out.println("更新用户");
}
public void serach() {
System.out.println("查询用户");
}
}
- (複数の方法を含む)の書き込み強化クラス
package com.yang.config;
public class zichuanglei {
public void before(){
System.out.println("=====qian=====");
}
public void after(){
System.out.println("=====hou=====");
}
}
- 豆の設定、完全なXML設定
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="ces" class="com.yang.config.zichuanglei"/>
<aop:config>
<!--配置切面,需要配置标签来控制-->
<aop:aspect ref="ces">
<aop:pointcut id="pc-userservice" expression="execution(* com.yang.service.Impl.UserServiceImpl.*(..))"/>
<!--方法来自于切面-->
<aop:before method="before" pointcut-ref="pc-userservice"/>
<aop:after method="after" pointcut-ref="pc-userservice"/>
</aop:aspect>
</aop:config>
</beans>
- テスト
@Test
public void test2(){
// bean就是创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
実現注意事項
- Daobao
- 書き込みインタフェースと実装クラス
- 強化されたライティングクラス
package com.yang.config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//切面原理
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====qian=====");
}
@After("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====hou=====");
}
@Around("execution(* com.yang.service.Impl.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前");
System.out.println(joinPoint.getSignature());
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
- サイン豆
<bean id="userService" class="com.yang.service.Impl.UserServiceImpl"/>
<bean id="AnnotationPointcut" class="com.yang.config.AnnotationPointcut"/>
<!--让spring识别注解,开启识别-->
<aop:aspectj-autoproxy/>
<!--aop的配置-->
- テスト
MyBatisのスプリング