在建议不执行SPRING AOP之前

时间:2013-02-01 22:48:50

标签: java spring maven aop spring-aop

我正在为考试而学习Maven Spring AOP的例子,并且arcos遇到了以下问题。

我有一个简单的建议,我需要解雇....

我的代码如下

我的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;
}

}

但是当我运行它时它会提供以下输出而不会显示调用before建议的任何影响。

Starting
Feb 02, 2013 12:44:37 AM org.springframework.context.support.AbstractApplicationContext     prepareRefresh
INFO: Refreshing     org.springframework.context.support.FileSystemXmlApplicationContext@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.factory.support.DefaultListableBeanFactory@3d92d11: 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

有谁知道为什么之前的建议没有运行以及为什么它没有给我一个错误

1 个答案:

答案 0 :(得分:3)

这是因为您没有在配置文件中的任何位置指定Aspect 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我希望这有帮助!

相关问题