1.SpringFrameworkのAOPコンセプト
Springフレームワークの重要なコンポーネントは、アスペクト指向プログラミング(AOP)フレームワークです。アスペクト指向プログラミングでは、プログラムロジックをいわゆる懸念事項と呼ばれるさまざまな部分に分解する必要があります。アプリケーションの複数のポイントにまたがる機能は横断的関心事と呼ばれ、これらの横断的関心事は概念的にアプリケーションのビジネスロジックから独立しています。
この概念は非常に抽象的かもしれませんが、AOPがメソッドまたはプログラムをインターセプトするために使用されるインターセプターと同等であることを知っておく必要があります。メソッドの実行前または実行後に関数を追加できます。
2、AOP関連の用語
関連用語
通知の種類(カットインタイプ)
- 事前通知<aop:before> </ aop:before>:メソッドが実行される前に通知を実行します。
- 通知後<aop:after> </ aop:after>:メソッドが実行された後、結果に関係なく通知が実行されます。
- 戻り後の通知<aop:after-returning> </ aop:after-returning>:メソッドの実行後、通知はメソッドが正常に完了した場合にのみ実行できます。
- 例外がスローされた後の通知<aop:after-throwing> </ aop:after-throwing>:メソッドが実行された後、通知はメソッドが終了して例外をスローした場合にのみ実行できます。
- アラウンド通知<aop:around> </ aop:round>:提案メソッドが呼び出される前後に通知を実行します。
3.例(預金および預金業務)
1.関連カテゴリカタログ
2.BankDao(CheckおよびEmpDaoはオプションです)
package sc.dao;
public interface BankDao {
//转账
public void remirt();
//存钱
public void save();
}
3、AdminCheck
package sc.impl;
import sc.dao.Check;
public class AdminCheck implements Check{
public void check(){
System.out.println("权限验证开始");
}
}
4. BankDaoImpl(EmpDaoImplは不要)
package sc.impl;
import sc.dao.BankDao;
public class BankDaoImpl implements BankDao{
@Override
public void remirt() {
System.out.println("转账的业务");
}
@Override
public void save() {
System.out.println("存钱的业务");
}
}
5、logManager
package sc.impl;
public class LogManager {
public void writelog(){
System.out.println("日志正在写入");
}
}
6、TransactionManager
package sc.impl;
public class TransactionManager {
public void begin(){
System.out.println("开始业务逻辑");
}
public void commit(){
System.out.println("提交业务逻辑");
}
}
7、spring.xml
<?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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
">
<bean id="adminCheck" class="sc.impl.AdminCheck"></bean>
<bean id="transactionManager" class="sc.impl.TransactionManager"></bean>
<bean id="logManager" class="sc.impl.LogManager"></bean>
<bean id="bankDao" class="sc.impl.BankDaoImpl"></bean>
<bean id="empDao" class="sc.impl.EmpDaoImpl"></bean>
<!--开始spring aop 面向切面-->
<aop:config>
<!--切入点-->
<aop:pointcut id="service" expression="execution(* sc.impl.*.*(..))"></aop:pointcut>
<aop:aspect ref="adminCheck">
<!--在此之前切入-->
<aop:before method="check" pointcut-ref="service"></aop:before>
</aop:aspect>
<aop:aspect ref="transactionManager">
<aop:before method="begin" pointcut-ref="service"></aop:before>
</aop:aspect>
<!--after method 用于那个方法是最终通知 谁在最前面 最后输出就在最后 -->
<aop:aspect ref="logManager">
<aop:after method="writelog" pointcut-ref="service"></aop:after>
</aop:aspect>
<aop:aspect ref="transactionManager">
<aop:after method="commit" pointcut-ref="service"></aop:after>
</aop:aspect>
</aop:config>
</beans>
8、テスト
package sc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sc.dao.BankDao;
import sc.dao.Check;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
BankDao bankDao = (BankDao) ac.getBean("bankDao");
bankDao.remirt();
System.out.println("----------------------");
bankDao.save();
}
}
操作の結果
、コードの冗長性が低下する可能性があります。検証の開始、ビジネスロジックの開始、ビジネスロジックの送信、およびログの書き込みは1回だけですが、 2回使用します。