質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Q&A

解決済

1回答

7564閲覧

SpringAOPの@Beforeをつけたメソッドが実行されない。

goligoli

総合スコア6

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

0グッド

0クリップ

投稿2019/10/16 01:48

前提・実現したいこと

AOPを初めて触るため、System.out.println()で簡易的なAOPを実現したい。

発生している問題・エラーメッセージ

現在、システムとしてエラーはないのですが、@Aspectをつけたメソッドが実行されません。
@AspectをつけたメソッドはSystem.out.println()でメッセージを出す簡易的なメソッドです。

該当のソースコード

機能しないAOPクラス

Java

1package jp.mitchy.aop; 2 3import org.aspectj.lang.JoinPoint; 4import org.aspectj.lang.annotation.After; 5import org.aspectj.lang.annotation.Aspect; 6import org.aspectj.lang.annotation.Before; 7import org.springframework.stereotype.Component; 8 9@Component 10@Aspect 11public class AspectTest { 12 13 @Before("execution(* *..*Controller.*(..))") 14 public void aop(JoinPoint joinpoint) { 15 16 System.out.println("AOP完了!!" + joinpoint.getSignature()); 17 System.out.println("---------------AOPPPPPPPPPPP!!!!!!!!---------------"); 18 19 } 20 21 @After("execution(* *..*Controller.*(..))") 22 public void aop2(JoinPoint joinpoint) { 23 System.out.println("AOP完了!!" + joinpoint.getSignature()); 24 System.out.println("---------------AOPPPPPPPPPPP!!!!!!!!---------------"); 25 26 } 27} 28

コントローラです。レガシープロジェクトそのままですが。

Java

1package jp.mitchy.aop; 2 3import java.text.DateFormat; 4import java.util.Date; 5import java.util.Locale; 6 7import org.slf4j.Logger; 8import org.slf4j.LoggerFactory; 9import org.springframework.stereotype.Controller; 10import org.springframework.ui.Model; 11import org.springframework.web.bind.annotation.RequestMapping; 12import org.springframework.web.bind.annotation.RequestMethod; 13 14/** 15 * Handles requests for the application home page. 16 */ 17@Controller 18public class HomeController { 19 20 private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 21 22 /** 23 * Simply selects the home view to render by returning its name. 24 */ 25 @RequestMapping(value = "/", method = RequestMethod.GET) 26 public String home(Locale locale, Model model) { 27 logger.info("Welcome home! The client locale is {}.", locale); 28 29 Date date = new Date(); 30 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 31 32 String formattedDate = dateFormat.format(date); 33 34 model.addAttribute("serverTime", formattedDate ); 35 36 return "home"; 37 } 38 39} 40

root-context.xml

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 https://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 https://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd"> 12 13 <!-- Root Context: defines shared resources visible to all other web components --> 14 <context:component-scan 15 base-package="jp.mitchy.aop" /> 16 17 <aop:aspectj-autoproxy /> 18 <aop:config proxy-target-class="true" /> 19 20</beans>

servlet-context.xml

xml

1<?xml version="1.0" encoding="UTF-8"?> 2<beans:beans 3 xmlns="http://www.springframework.org/schema/mvc" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:beans="http://www.springframework.org/schema/beans" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/mvc 8 https://www.springframework.org/schema/mvc/spring-mvc.xsd 9 http://www.springframework.org/schema/beans 10 https://www.springframework.org/schema/beans/spring-beans.xsd 11 http://www.springframework.org/schema/context 12 https://www.springframework.org/schema/context/spring-context.xsd"> 13 14 <!-- DispatcherServlet Context: defines this servlet's request-processing 15 infrastructure --> 16 17 <!-- Enables the Spring MVC @Controller programming model --> 18 <annotation-driven /> 19 20 <!-- Handles HTTP GET requests for /resources/** by efficiently serving 21 up static resources in the ${webappRoot}/resources directory --> 22 <resources mapping="/resources/**" location="/resources/" /> 23 24 <!-- Resolves views selected for rendering by @Controllers to .jsp resources 25 in the /WEB-INF/views directory --> 26 <beans:bean 27 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 28 <beans:property name="prefix" value="/WEB-INF/views/" /> 29 <beans:property name="suffix" value=".jsp" /> 30 </beans:bean> 31 32 <context:component-scan 33 base-package="jp.mitchy.aop" /> 34 35</beans:beans> 36 37</beans>

試したこと

デバックを掛けてみたのですが、AOPクラスを経由せず、Controllerだけが処理を行われていました。
それ以外は、調べてもわかりませんでした。

補足情報(FW/ツールのバージョンなど)

エクリプスで開発を行っているのですが、左に出ている赤い矢印にカーソルを当てると、「advises jp.mitchy.aop.HomeController.home(Locale, Model)」と表示されているのでAOPには成功しているみたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/10/16 03:17

そもそも webアプリで System.out.println が間違えてる。 出力される設定なのか?
goligoli

2019/10/16 03:32

実行されているかを確認したかったので、使ってみたのですが、よくないですかね?
guest

回答1

0

ベストアンサー

コードや設定内容1つ1つには特に大きな問題はなく、Aspect対象の指定方法も問題はないのですが(私の環境では動作済みです)
気になるのは、

root-context.xmlとservlet-context.xmlに、context:component-scanが重複しているのですが、root-contextに設定されているaop設定が動いていないことから察するに、root-context.xmlが実行時に読み込まれていないと思われるので、servlet-context.xmlにroot-contextの設定内容を書いて動くかどうか確認されてはいかがでしょうか。

web.xmlなども確認して root-context.xmlがSpringの設定ファイルとして指定できているかどうかも確認は必要でしょう。

投稿2019/10/16 02:47

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

goligoli

2019/10/16 03:40

試して見たらできました!有難うございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問