プログラミングマガジン

プログラミングを中心にIT技術をできるだけわかりやすくまとめます。

  • ホーム
  • Spring Framework
  • 【Spring Framework】AOP(アスペクト指向プログラミング)を実装してみる。…
 
 
     
  • サーバー言語  
    • Python
    • Ruby
    • PHP
    • SQL
  •  
  • インフラ  
       
    • AWS
    •  
    • 基本
    • Git
  • Web
       
    • Web開発
    • JavaScript
    • Vue.js
    • React
  •  
  • 設計  
       
    • 実装設計
    • DB設計
  • 問い合わせ
  

【Spring Framework】AOP(アスペクト指向プログラミング)を実装してみる。(設定クラスから実装)

05.13

  • miyabisan2
  • コメントを書く

この記事は1分で読めます

前回の記事では、AOPを設定ファイルから実行してみました。

【Spring Framework】AOP(アスペクト指向プログラミング)を実装してみる。(設定ファイルから実装)

次は、設定用のクラスを作って、AOPを実装してみましょう。

AOPをクラスファイルからの設定で実装してみる。

実装イメージは、下記のクラス図を参照下さい。

Main.java(利用するプログラムです。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.springapp;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class Main {
 
    public static void main(String[] args) {
 
     ApplicationContext app =  new AnnotationConfigApplicationContext(AopConfig.class);
 
        //AOP適用前
        TestBean bean1 = (TestBean) app.getBean("TestBean");
        bean1.HelloWorld();
 
        System.out.println("--------------------");
 
        //AOP適用後
        TestBean bean2 = (TestBean) app.getBean("ProxyFactoryBean");
        bean2.HelloWorld();
    }
 
}

クラスからAOPの設定を読み込む構文

設定クラスを読み込むために下記の構文を使っています。

1
ApplicationContext app = new AnnotationConfigApplicationContext(設定クラス名.class);

その他の構文に関しては、設定ファイルから読み込む場合の構文とそれほど違いはありません。

TestBean.java(AOPを適用するクラスです。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.springapp;
 
public class TestBean {
    private String helloWorld;
 
    public TestBean() {
        super();
        this.helloWorld = "Hello World!";
    }
 
    public TestBean(String helloWorld) {
        this.helloWorld = helloWorld;
    }
 
    public void HelloWorld() {
        System.out.println(this.helloWorld);
    }
}

AOPClass.java(AOPで挿入する処理のクラスです。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.springapp;
 
import java.lang.reflect.Method;
 
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
 
public class AOPClass implements MethodBeforeAdvice, AfterReturningAdvice {
 
@Override
public void before(Method method, Object[] args,Object target) throws Throwable {
System.out.println("事前処理");
}
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("事後処理");
}
 
}

AopConfig.java(AOPの設定クラスです。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.springapp;
 
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class AopConfig {
 
private TestBean testBean = new TestBean("Hello World!");
    private AOPClass aopclass = new AOPClass();
 
    @Bean
    TestBean TestBean() {
        return testBean;
    }
 
    @Bean
    AOPClass AOPClass() {
        return aopclass;
    }
 
    @Bean
    ProxyFactoryBean ProxyFactoryBean() {
 
     ProxyFactoryBean bean = new ProxyFactoryBean();
 
     bean.setTarget(testBean);
        bean.setInterceptorNames(new String[]{"AOPClass"});
 
        return bean;
    }
 
}

下記のアノテーションを使っています。

使っているアノテーション

アノテーション 説明
@Configuration 設定クラスとして宣言するためのアノテーションです。これを設定しておけば、メインプログラムから設定用クラスとして呼び出せます。
@Bean Bean設定のアノテーションです。これを設定しておけば、メインプログラムから「getBean("Bean名")」でオブジェクトを取得できます。

実行結果

スポンサーリンク
  • 2018 05.13
  • miyabisan2
  • コメントを書く
  • Spring Framework
  • Tweets Twitter
  • このエントリーをはてなブックマークに追加
  • LINEで送る

関連記事

  1. 2018 05.19

    【Spring Boot】「Thymeleaf(タイムリーフ)」で画面にまとまった値を渡そう。

  2. 2018 05.06

    【Spring Framework】Springのコア機能(DI :依存性の注入)について

  3. 2018 05.06

    【Spring Framework】DI(依存性の注入)を実装してみる。(アノテーションによる設定用クラスから)

  4. 2018 05.06

    【Spring Framework】Springによる開発を始めよう(STSのインストール~「Hello World」まで)

  5. 2018 05.13

    【Spring Framework】AOP(アスペクト指向プログラミング)を実装してみる。(設定ファイルから実装)

  6. 2018 05.19

    【Spring Boot】SpringBootの基本、javax.servlet.ServletException: Circular view path [~]: would dispatch back to the current handler URL [/~] again. が出て困った話。

  • コメント ( 0 )
  • トラックバック ( 0 )
  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

返信をキャンセルする。

【Spring Framework】AOP(アスペクト…

【Spring Framework】AOP(アスペクト…

RETURN TOP

著者プロフィール

エンジニア歴10年で過去に業務系、Webデザイン、インフラ系なども経験あります。現在はWeb系でフロントエンド開発中心です。

詳細なプロフィールはこちら

スポンサーリンク

カテゴリー

  • Android
  • API
  • AWS
  • C++
  • CSS
  • C言語
  • DDD
  • DevOps
  • Django
  • Docker
  • Git
  • GitLab
  • GraphQL
  • Hasura
  • Java
  • JavaScript
  • Kubernetes
  • Laravel
  • linux
  • MySQL
  • Next.js
  • nginx
  • Node.js
  • NoSQL
  • Nuxt.js
  • Oracle
  • PHP
  • Python
  • React
  • Redux
  • Rspec
  • Ruby
  • Ruby on Rails
  • Sass
  • Spring Framework
  • SQL
  • TypeScript
  • Unity
  • Vue.js
  • WebRTC
  • Webサービス開発
  • Webデザイン
  • Web技術
  • インフラ
  • オブジェクト指向
  • システム開発
  • セキュリティ
  • その他
  • データベース
  • デザインパターン
  • テスト
  • ネットワーク
  • プログラミング全般
  • マイクロサービス
  • マイクロソフト系技術
  • マルチメディア
  • リファクタリング
  • 副業
  • 未分類
  • 業務知識
  • 設計
  • 関数型言語
RETURN TOP

Copyright ©  プログラミングマガジン | プライバシーポリシー