I have a Spring 3 MVC app where all of my spring components are defined with annotations vs. being defined in the mvc-servlet.xml. I am trying to integrate AspectJ using the @AspectJ annotations. The problem that I have is that Spring needs to see the aspect as a Spring component as well as AspectJ needing to see the class defined as an Aspect so if I try to use both annotations @Aspect and @Component it blows up on me at application startup.
All documentation that I've been diggin through only give an example of using @AspectJ with the standard definition of spring components(non-annotation based)
How do you do this when using annotations for both spring components and @AspectJ definitions?
mvc-servlet.xml
Advise classCode:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.jonsinfinity"/> <context:annotation-config /> <aop:aspectj-autoproxy /> ...
Stack Trace from startup failureCode:@Component("hibernateAdvisor") @Aspect public class HibernateAdvisor { @Pointcut("execution(* com.jonsinfinity.hbm.custom.*.*(..))") public void doRunAspect(){}; @Autowired HibernateTemplate template = null; @Autowired UserCredentialsDataSourceAdapter userCredentialsDataSourceAdapter = null; @Before("execution(* com.jonsinfinity.hbm.custom.*.*(..))") public void changeCredentials(){ userCredentialsDataSourceAdapter.removeCredentialsFromCurrentThread(); userCredentialsDataSourceAdapter.setCredentialsForCurrentThread("joe", "blah"); userCredentialsDataSourceAdapter.setUsername("joe"); userCredentialsDataSourceAdapter.setPassword("blah"); System.out.println("** HibernateAdvisor.changeCredentials() executed - New Credentials have been set. **"); } @AfterReturning public void clearHibernateTemplate(){ //template.clear(); System.out.println("HibernateAdvisor.clearHibernateTemplate - Complete"); } }
Code:SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateAdvisor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.orm.hibernate3.HibernateTemplate com.jonsinfinity.aspects.HibernateAdvisor.template; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]: Cannot resolve reference to bean 'targetDataSource' while setting bean property 'targetDataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'targetDataSource' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Must set property 'expression' before attempting to match at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) ...


Reply With Quote