2011-12-27 7 views
3

私はSpring AOPの初心者です。ロギングにアスペクトを使用しようとしています。ここに私の設定です:Spring AOP:コンテキストビーンのみがアドバイスできますか?

側面:

@Aspect 
public class LoggerAspect { 

@Pointcut("execution(* aop.LoggerAspTest.*(..))") 
private void infoMethods(){} 

@Before("infoMethods()") 
public void logBefore(JoinPoint joinPoint) { 
    Logger logger = Logger.getLogger(joinPoint.getTarget().getClass()); 

    logger.info("joinPoint's kind: " + joinPoint.getKind()); 
    logger.info("joinPoint's args: " + joinPoint.getArgs()); 
    logger.info("joinPoint's source location: " + joinPoint.getSourceLocation()); 
    logger.info("joinPoint's staticPart: " + joinPoint.getStaticPart()); 
    logger.info("joinPoint's targetClass: " + joinPoint.getTarget().getClass()); 
    logger.info("joinPoint's this: " + joinPoint.getThis()); 
} 
} 

テストクラス:

@Component 
public class LoggerAspTest { 
    // test method 
    public void getInfo() { 
    System.out.println("in the logger aspect test method!!!"); 
    } 
} 

クラス - エグゼキュータ:

public class Main { 
    // main 
    public static void main(String[] args) { 
    ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml"); 
    LoggerAspTest aspect = (LoggerAspTest) ctx.getBean("aspectTest"); 
    aspect.getInfo(); 
} 

}

そして最後に - applicationCont ext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 

<aop:aspectj-autoproxy/> 
<bean id="aspectTest" class="aop.LoggerAspTest"/> 
... 

さて、すべてが完全に機能します。 しかし、SpringのApplicationContext.getBean()ではなく、LoggerAspTest aspect = new LoggerAspTest();を介してLoggerAspTestを作成するように、クラス実行プログラム(Main)を変更すると、何もしません。

質問は次のとおりです。「側面はSpringのコンテキストによってインスタンス化されたBeanでのみ機能するのは本当ですか?」私は本当に "グローバルインターセプタ"のように機能することを期待していました。どの方法を先に進めなければならないかを知っています...

ありがとうございます。 Jigarは、それがスプリングビーンする必要がある、と述べたように

+0

する必要があります。どのような方法を進めるべきかを知っている "世界的な迎撃者"はどういう意味ですか?豆は世界的な傍受者になることはできませんか? – Wint

+0

@Wint彼は、傍受されるべきあらゆるクラスを意味します。春の文脈でさえもそれはまったく受け入れられません。 –

+0

みなさんありがとう! – Dmitry

答えて

2

はいそれはスプリングビーン

+0

すぐにお返事ありがとうございます。ですから、@AutowiredアノテーションからBeanを取得するのは正しいのでしょうか?アスペクトも機能しますか? – Dmitry

+0

ところで、(以下で説明するように)「グローバルインターセプタ」を実装するための春の標準的な方法はありますか? – Dmitry

+0

ええと、それは春の文脈から来るはずです。 –

関連する問題