2012-12-28 51 views
3

我想要的start();春@AspectJ:建議不適用

調用之前代碼編織這是在識別TestClass我想諮詢:

package com.test; 

public class TestClass { 

    public static void main(String[] args) { 

     new TestClass().start(); 
    } 
    private void start() { 

     System.out.println("Test started"); 
    } 
} 

這是包括諮詢方面:

package com.test; 

import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.springframework.stereotype.Component; 

@Aspect 
@Component 
public class LogAspect { 

    @Before("call(void com.test.TestClass.start())") 
    public void logBefore(JoinPoint joinPoint) { 
     System.out.println("logBefore() is running"); 
    } 

} 

這是我spring.xml:

<beans xmlns="http://www.springframework.org/schema/beans" 
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/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 

<aop:aspectj-autoproxy/> 
<context:annotation-config/> 
<context:component-scan base-package="com.test"/> 

我也試圖在這樣的XML明確命名豆:

<aop:aspectj-autoproxy/> 
<bean id="test" class="com.test.TestClass" /> 
<bean id="aspect" class="com.test.LogAspect" /> 

這是我的項目設置(我使用了Spring工具套件版本3.1.0):

Project setup

結果是TestClass.start叫就好了,但不應用建議。

我需要更改什麼才能應用建議?

謝謝。

編輯:我終於得到它的工作:

根據您的建議編輯我的代碼後,我看了一下這tutorial。這導致我讓我的TestClass 實現一個接口。這解決了這個問題。

這是我最後的安裝:

final setup

的TestClass中,包括它的接口:

package com.test; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

interface TestClass { 
    void start(); 
} 

public class TestClassImpl implements TestClass { 

    public static void main(String[] args) { 
     ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
       "Spring-Config.xml"); 

     TestClass t1 = (TestClass) appContext.getBean("myTest"); 
     t1.start(); 
    } 

    @Override 
    public void start() { 
     System.out.println("test"); 
    } 
} 

的方面:

package com.test; 

import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 

@Aspect 
public class LogAspect { 
    @Before("execution(void com.test.TestClass.start(..))") 
    public void logBefore(JoinPoint joinPoint) { 

     System.out.println("logging before " 
       + joinPoint.getSignature().getName()); 
    } 
} 

和Spring-config.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    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/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 

    <aop:aspectj-autoproxy/>  
    <bean id="myTest" class="com.test.TestClassImpl" /> 
    <bean id="logAspect" class="com.test.LogAspect" /> 

</beans> 

感謝您的幫助。

+0

我沒有得到你的代碼的工作,雖然我沒有使用Maven做實際的編織。 – Reimeus

+0

@Reimeus:您能否將我的Maven項目發送給我的電子郵件地址?我想弄明白這一點。 –

+0

所有你需要的是POM,這裏是一個[很好的例子(http://mojo.codehaus.org/aspectj-maven-plugin/usage.html) – Reimeus

回答

1

A call AspectJ切入點適用於方法爲時從另一個類別調用。正如你調用這個直接自己,你應該使用執行切入點,所以更改:

@Before("call(void com.test.TestClass.start())") 

@Before("execution(void com.test.TestClass.start())") 

也讓Spring創建bean,而不是直接創建一個自己。


旁白:注意,你可以保持您Spring XML文件從源文件中分離出來,並src/resources他們將ClassPathXmlApplicationContext有所回升。

+1

另外通過Spring應用程序上下文實例化'Test'類,而不是通過調用'new'。 –