Spring AOP初回試験


AOP基本概念
切断面(Asppect):複数の通知(拡張処理、切断方法、advice)接続点(Joinpoint):プログラム実行中に明確な点は、プログラム実行中の点と理解されるでしょう.Spring AOPでは、メソッド呼び出しです.通知(拡張処理、カット方法、advice):around、before、after、after returning、after throwingがあります.接点(Point Cut):adviceのJoinPointを挿入できます.ある接続点が要求を満たすと、この接続点は強調処理を追加される.紹介:クラスに方法またはフィールドを追加します.Springは新しいインターフェースを任意の処理されたクラスに導入することを可能にする.ターゲット:AOPフレームにより強調処理されるオブジェクト.SpringにおけるAOPエージェントは、JDK動的エージェント(インターフェースを実現する目的オブジェクト)であってもよく、cglibエージェント(インターフェースを実現しない対象オブジェクト)であってもよい.織り込み(weaving):拡張処理を対象オブジェクトに追加し、拡張対象(AOPエージェント)を作成する過程は織り込みです.編入には2つの方法があります.コンパイル時の強化(Aspring AOP)と運転時の強化(spring AOPなど)があります.
コメントプラス構成で実現
IDEAを利用してweb+mavenを新設するプロジェクト
設定情報
web.xml:
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
appication Contect.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:annotation-config />

    <context:component-scan base-package="com.mitsuhide.*" />
    <!--</context:component-scan>-->

    <aop:aspectj-autoproxy/>

    <mvc:annotation-driven />

</beans>
注意:appication Contect.xmlは、mavenで構成されている場合、必ずresourceに入れてください.そうでないと、クラスパス*やクラスパスパスパスパスが見つけられません.
pom.xml(IDEAのmavenは本当に使いやすいです.この点が一番好きです.)入れました.(aopのはaopとaspectJ、cglibだけらしいです.)
spring-context
spring-webmvc
spring-web
spring-aop
aspectjrt
aspectjweaver
spring-tx
spring-aspects
cglib-nodep
キーコード
コントローラ
@Controller
@RequestMapping("/mvc")
public class HelloController {
    @RequestMapping("/hello")
    public @ResponseBody String hello () {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// Athlete zly = (Athlete)context.getBean("Athlete");
        Player zly = (Player)context.getBean("Athlete");
        return zly.sayHello();
    }
}
ここで面白いことがあります.Athleteは切断方法に縛られていますので、beanを得るにはここで状況を分ける必要があります.
Player zly = (Player)context.getBean("Athlete");
2、Athleteインターフェースが一つも実現されていない場合はcglibの方式で、直接クラスを取得する:
Athlete zly = (Athlete)context.getBean("Athlete");
Asppect
@Aspect
@Component
public class PlayerAspect extends CustomizableTraceInterceptor {
    @Pointcut("execution( * com.mitsuhide.entity.common.Athlete.*(..))")
    private void aspectMethod(){}//       

    @Before("aspectMethod()")
    public void before () {
        System.out.println("       ...... By mitsuhide aspect.");
    }

    @After("aspectMethod()")
    public void after () {
        System.out.println("       ...... By mitsuhide aspect.");
    }
}
ここでもこのように書いてもいいです.
@Before("execution( * com.mitsuhide.entity.common.Athlete.*(..))")
ビーン:
@Component("Athlete")
public class Athlete implements Player {
    @Autowired
    private Basketball ball;

    public Basketball getBall() {
        return ball;
    }

    public void setBall(Basketball ball) {
        this.ball = ball;
    }

    public String sayHello () {
        String helloWords = "Hi, I am a " + ball.getBallName() + " player";
        return helloWords;
    }

    @PostConstruct
    public void init () {
        System.out.println("Athlete bean is initializing! By mitsuhide.");
    }

    @PreDestroy
    public void destroy () {
        System.out.println("Athlete bean is destroying! By mitsuhide.");
    }
}
遭遇した問題
1、appication Contect.xmlが見つかりません.
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ファイルのエラーが見つかりませんでした.これは本道の問題です.xmlファイルをresourceディレクトリに置いて解決できます.
2、麺の切り方は麺類を実行しないで、同時に@Asppectを加えます.@Componentはそばの切り方を実行します.オンラインチュートリアルはほとんどないです.なぜですか?
3、上記で述べたように、インターフェースの代理と種類の代理ネットを区別する必要があります.
aop:configタグにproxy-targt class="true"を追加すればいいです.生成エージェントクラスには2つの方法があります.JDKとCGLOIB、一つはインターフェースに基づいています.一つはクラスに基づいています.上記の属性を追加すると、クラスに基づくcglibの方式を使用し、逆に、書かれていない場合やfalseの場合は、jdkベースのインターフェースによってプロキシクラスが生成される.もちろん、もし自分がインターフェースに基づいていないならば、自動的にcglibの方式を使うことができて、ここでとてもおかしいです.なぜ自動的にcglibの方式を歩いていませんか?proxy-target class属性値がtrueに設定されると、クラスベースのエージェントが機能します.proxy-target class属値がfalseまたはこの属性に設定されている場合、標準のJDKはインターフェースのエージェントに基づいて機能する.
残された問題
spring中contextの取得にはいくつかの方法があります.Application Cotext context=new Class PathXml Application Controtext(「appication Contect.xml」).
AnnotationConfigApple Contect context=new AnnotationConfigAplication Contect(EntityManager.class)
EnttityManager:
@Configuration
@ComponentScan(basePackages = {"com.mitsuhide.*"})
public class EntityManager {
}
@Configration、@ComponentScanは何ですか?検討が必要です.