2013-02-01 78 views
1

我正在嘗試一個Maven Spring AOP示例,同時爲考試而學習,並將arcos帶來以下問題。之前建議不執行SPRING AOP

我有一個簡單的建議,我需要解僱之前....

我的代碼如下

我的POM定義爲

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org /xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

    <groupId>my.chrispie</groupId> 
    <artifactId>MyMavenSpringProject</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>MyMavenSpringProject</name> 
    <url>http://maven.apache.org</url> 

<properties> 
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
<spring.version>3.0.0.RELEASE</spring.version> 
</properties> 
<dependencies> 
<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>3.8.1</version> 
    <scope>test</scope> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>${spring.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>${spring.version}</version> 
</dependency> 

<!-- Spring AOP + AspectJ --> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aop</artifactId> 
    <version>${spring.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.aspectj</groupId> 
    <artifactId>aspectjrt</artifactId> 
    <version>1.6.11</version> 
</dependency> 

<dependency> 
    <groupId>org.aspectj</groupId> 
    <artifactId>aspectjweaver</artifactId> 
    <version>1.6.11</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
      <version>1.2</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compile</goal> <!-- use this goal to weave all your main classes --> 
         <goal>test-compile</goal> <!-- use this goal to weave all your test classes --> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

    </plugins> 

我有觀衆類

package my.chrispie.example.objects; 

import org.aspectj.lang.ProceedingJoinPoint; 

public class Audience { 

public void takeSeats() { 
    System.out.println("The audience is taking their seats"); 
} 

public void turnOffCellPhones() { 
    System.out.println("Turn off phones"); 
} 

public void applaud() { 
    System.out.println("CLAP CLAP CLAP"); 
} 

public void demandRefund() { 
    System.out.println("BOO BOO BOO"); 
} 

public void perform() { 
    System.out.println(".............PERFORMING"); 
} 

public void watchPerformance(ProceedingJoinPoint jointpoint) { 
    try { 
     System.out.println("Audience is taking there seats"); 
     long start = System.currentTimeMillis(); 

     jointpoint.proceed(); 

     long end = System.currentTimeMillis(); 

     System.out.println("Perfomance took " + (end - start)); 
    } catch (Throwable t) { 
     System.out.println("Boo we want our money back"); 
    } 
} 

}

和一個配置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:aop="http://www.springframework.org/schema/aop" 
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-2.5.xsd"> 


<bean id="audienceExampleTest1" class="my.chrispie.example.objects.Audience">  </bean> 
<bean id="audienceExampleTest2" class="my.chrispie.example.objects.Audience"></bean> 

<aop:config> 
    <aop:aspect ref="audienceExampleTest1"> 

     <aop:before pointcut="execution(* my.chrispie.example.objects.Audience.perform(..))" 
     method="takeSeats"/>  

     <aop:after method="takeSeats" pointcut="execution(* my.chrispie.example.objects.Audience.perform(..)) "/> 
    </aop:aspect> 

</aop:config> 

<aop:config> 
    <aop:aspect ref="audienceExampleTest2"> 
     <aop:pointcut expression="execution(* my.chrispie.example.objects.Audience.perform(..))" 
     id="myPcId"/> 

     <aop:before method="takeSeats" pointcut-ref="myPcId"/>   
     <aop:around method="takeSeats" pointcut-ref="myPcId"/> 

    </aop:aspect> 

</aop:config>    

</beans> 

和主類作爲

package my.chrispie.MyMavenSpringProject; 

import my.chrispie.example.objects.Audience; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.FileSystemXmlApplicationContext; 


public class App 
{ 

private car myCar1; 
private car myCar2; 

@Autowired 
public car myCar3; 
public car myCar4; 



public static void main(String[] args) 
{ 
    System.out.println("Starting"); 

    App app = new App(); 
    app.testAOPExample1(); 
    System.out.println("Ending"); 

} 

public void testAOPExample1() { 
    ApplicationContext context = getAppContext(); 
    Audience a = (Audience)context.getBean("audienceExampleTest1"); 
    a.perform(); 
    a.perform(); 
} 

public ApplicationContext getAppContext() { 
    ApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml"); 
    return context; 
} 

} 

但是當我運行它,它給出了下面的輸出,而不表現出任何效果,即在徵求意見之前。

Starting 
Feb 02, 2013 12:44:37 AM org.springframework.context.support.AbstractApplicationContext  prepareRefresh 
INFO: Refreshing  org.[email protected]2b3954b1: startup date  [Sat Feb 02 00:44:37 CAT 2013]; root of context hierarchy 
Feb 02, 2013 12:44:37 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader  loadBeanDefinitions 
INFO: Loading XML bean definitions from file [F:\DEV\EclipseWorkSpaces \STS1\MyMavenSpringProject\src\main\resources\applicationContext.xml] 
Feb 02, 2013 12:44:37 AM  org.springframework.beans.factory.support.DefaultListableBeanFactory  preInstantiateSingletons 
INFO: Pre-instantiating singletons in  org.springframework.beans.[email protected]: defining  beans  [audienceExampleTest1,audienceExampleTest2,org.springframework.aop.config.internalAutoProxy Creator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,myPcId]; root of factory hierarchy 
.............PERFORMING 
.............PERFORMING 
Ending 

有誰知道爲什麼之前的建議是不會運行,爲什麼不給我一個錯誤

回答

3

這是因爲你沒有在配置文件中指定的看點Bean的任何地方。添加以下內容

<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> --> 
    <bean id="audienceExampleTest1" class="my.chrispie.example.objects.Audience">  </bean> 
    <bean id="audienceExampleTest2" class="my.chrispie.example.objects.Audience"></bean> 

    <!-- Aspect --> 
    <bean id="Aop" class="my.chrispie.example.objects.Audience" /> 

    <aop:config> 

    <aop:aspect id="aspect" ref="Aop" > 

    <aop:pointcut id="beforethis" 
      expression="execution(* my.chrispie.example.objects.Audience.perform(..))"/> 

       <aop:before pointcut-ref="beforethis" method="takeSeats" />  

      <aop:after pointcut-ref="beforethis" method="turnOffCellPhones" /> 
     </aop:aspect> 

    </aop:config> 

另請參閱此Aspect Oriented Programming with Spring我希望這有助於!

+1

附加:另外它更容易使用AOP註釋,而不是XML配置文件,你也可以在你的takeSeats()方法之前放置@Before「執行(* my.chrispie.example.objects.Audience.perform(..))」 –