1 AOP
1 AOPは何か
:AOP(アスペクト指向プログラミング)の手段アスペクト指向プログラミング、事前にコンパイルおよび実行時の動的エージェントを介して、プログラム機能の統一されたメンテナンスを実現技術が。AOPは、OOPの継続であり、ソフトウェア開発のホットスポットであり、Springフレームワークの重要なコンテンツであり、関数型プログラミングの派生パラダイムです。AOPを使用すると、ビジネスロジックのさまざまな部分を分離できるため、ビジネスロジックのさまざまな部分間の結合が減り、プログラムの再利用性が向上し、開発の効率が向上します。
2春におけるAopの役割
3Springを使用してAopを実装する
依存関係をインポートする
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
3.1 SpringAPIによる実装
まず、ビジネスインターフェイスと実装クラスを作成します
package com.zs.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void search();
}
package com.zs.service;
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("addUser");
}
@Override
public void delete() {
System.out.println("deleteUser");
}
@Override
public void update() {
System.out.println("updateUser");
}
@Override
public void search() {
System.out.println("searchUser");
}
}
次に、拡張クラスを作成するために、拡張前と拡張後の2つを記述します。
package com.zs.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
//o 返回值
//method被调用的方法
//objects 被调用的方法的对象的参数
//o1 被调用的目标对象
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(o1.getClass().getName() + "的" + method.getName() + "方法被执行了,返回值" + o );
}
}
package com.zs.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
@Override
//method : 要执行的目标对象的方法
//objects : 被调用的方法的参数
//Object : 目标对象
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName() + "的" + method.getName() + "方法被执行了");
}
}
最後に、Springファイルに移動して登録し、aopカットイン実装を実装します。インポートの制約に注意してください。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zs.service.UserServiceImpl"/>
<bean id="before" class="com.zs.log.BeforeLog"/>
<bean id="after" class="com.zs.log.AfterLog"/>
<!-- 配置aop:
-->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.zs.service.UserServiceImpl.*(..))"/>
<!-- 配置环绕-->
<aop:advisor advice-ref="before" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="after" pointcut-ref="pointcut"/>
</aop:config>
</beans>
テスト
import com.zs.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 动态代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.delete();
}
}
3.2Aopを達成するためのカスタムクラス
ターゲットビジネスクラスは変更されませんが、userServiceImplの
最初のステップです。独自のエントリクラスを作成します。
package com.zs.log;
public class LogPointCut {
public void before() {
System.out.println("======before======");
}
public void after() {
System.out.println("======after======");
}
}
スプリング構成に移動
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="logPointCut" class="com.zs.log.LogPointCut"/>
<bean id="service" class="com.zs.service.UserServiceImpl"/>
<aop:config>
<!-- 配置切面-->
<aop:aspect id="" ref="logPointCut">
<!-- 配置切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.zs.service.UserServiceImpl.*(..))"/>
<!-- 配置通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
テスト
import com.zs.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("service");
userService.delete();
}
}
3.1アノテーションを使用した実装
ステップ1:アノテーションによって実装された拡張クラスを作成する
package com.zs.log;
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.zs.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("---------方法执行前---------");
}
@After("execution(* com.zs.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("---------方法执行后---------");
}
@Around("execution(* com.zs.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
System.out.println("签名:"+jp.getSignature());
//执行目标方法proceed
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
ステップ2:Spring構成ファイルで、Beanを登録し、アノテーションをサポートする構成を追加します
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="service" class="com.zs.service.UserServiceImpl"/>
<!--第三种方式:注解实现-->
<bean id="annotationPointcut" class="com.zs.log.AnnotationPointcut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>