我在准备考试时尝试了一个 Maven 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.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
有谁知道为什么之前的建议没有运行以及为什么它没有给我一个错误