2013-03-27 48 views
2

我看点类将是,使用AspectJ不能在春季工作的AOP?

@Configuration 
@EnableAspectJAutoProxy 
@Component 
@Aspect 
public class AspectClass { 

    @Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())") 
    public void logBefore(JoinPoint joinPoint) { 

     System.out.println("Before running the beforeAspect() in the AopTest.java class!"); 
     System.out.println("Hijacked Method name : " + joinPoint.getSignature().getName()); 
     System.out.println("************************"); 
    } 

} 

我的其他Java类

public class AopTest { 

    public void beforeAspect() { 
     System.out.println("This is beforeAspect() !"); 
    } 
} 

我的主类是

public class MainMethod { 

    public static void main(String[] args) {  
     ApplicationContext context = new FileSystemXmlApplicationContext("ApplicationContext/applicationContext.xml"); 
     AopTest test = (AopTest)context.getBean("bean1"); 
     test.beforeAspect(); 
    } 
} 

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

    <bean id="bean1" class="com.pointel.aop.test1.AopTest" /> 

</beans> 

在此@Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")中的AspectClass将不会在beforeAspect()之前执行AopTest,此时运行Main方法。

好的答案是肯定赞赏的。

+2

不要让你的配置类与你的Aspect类相同。另外,你需要'@ ComponentScan'你的Aspect所在的包。 – 2013-03-27 14:57:57

+0

我删除了'@ Configuration'并包含'@ ComponentScan',但仍然无法工作。 – 2013-03-27 15:04:25

+0

请看下面,有几件事情你错过了。 – 2013-03-27 15:12:30

回答

3

首先,如果您打算使用基于注释的配置,请使用AnnotationConfigApplicationContext而不是FileSystemXmlApplicationContext。摆脱applicationContext.xml文件,并简单地在您的配置类中添加@Bean方法。事情是这样的:

@Configuration 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages = "your.aspect.package") 
public class AspectConfig { 
    @Bean 
    public AopTest aopTest() { 
     return new AopTest(); 
    } 
} 

在你的主

public class MainMethod { 

    public static void main(String[] args) {  
     AnnotationConfigApplicationContextcontext = new AnnotationConfigApplicationContext(AspectConfig.class); 
     // don't forget to refresh 
     context.refresh(); 
     AopTest test = (AopTest)context.getBean("aopTest"); 
     test.beforeAspect(); 
    } 
} 

AspectClass你应该有@Component@Aspect,你的方法应该有通知或切入点注解像@Before。它需要是@Component,所以Spring知道要扫描它。

+0

感谢您的回复......我是否想要删除'AspectClass'中的四个注释? – 2013-03-27 15:25:40

+0

在'AspectClass'中你应该有'@ Component','@ Aspect',并且你的方法应该有'@ Before'之类的建议或切入点注释。它需要是一个'@ Component',所以Spring知道要扫描它。更新我的回答 – 2013-03-27 15:26:48

+0

'context.refresh();'需要什么?没有''context.refresh();''的解决方案。 – 2013-03-27 15:28:52

1

这里有些代码需要在xml中添加才能使用批注-1.for @component注释。

xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd" 

2.after使用组件扫描来获得其使用@Component注释所有注解bean类,并使用AOP autoproxy-

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

的例子visit- www.technicaltoday.com/p/spring.html

+0

感谢您的回答...我不需要用'xml'来完成它,并纯粹寻找基于'AspectJ'的注解。 – 2013-03-28 06:30:26

0

你缺少您的方面课程中的切入点定义。

例如;

@Pointcut("execution(* *.advice(..))") 
public void logBefore(){} 

@Before("logBefore()") 
public void beforeAdvicing(){ 
System.out.println("Listen Up!!!!"); 
} 

您首先必须确定要编织您的方面。你可以通过使用Point cut来做到这一点。它是你在@Before注释中给出的切点名称。看看我的博客文章了解更多信息@http://dinukaroshan.blogspot.com/2010/06/aop-with-spring.html

0

我在beans配置中看不到您的AspectClass。你也应该声明它是一个Bean。