Frage

Spring aspects annotations (before, after, etc) are not executing. I have no compilation problems, "test!!!" is being printed every time I visit that page, but the methods annotated with @Before, etc are never run. Any help will be greatly appreciated.

Controller.java

@Controller
@RequestMapping("/person/")
public class Controller {

    @Autowired
    private Content content;

    @RequestMapping(method=RequestMethod.GET,value="list")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView();

        content.test();

        mav.addObject("content", content);
        mav.setViewName("list");
        return mav;
    }
}

Content.java

 package com.myfirm.myproject.model;

 @Component
 public class Content implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    public Content() {
    }

    public void test () {
        System.out.println("test!!!");
    }
}

ContentAspect.java

@Aspect
@Component
public class ContentAspect {

    @Pointcut ("execution(* com.myfirm.myproject.model.Content.test(..))")
    public void performance() {
    }

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

AppConfig.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    default-autowire="byName">


    <bean id="placeholderConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}/${db.db}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>

    <bean id="jdbcTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- this registers beans annotated with @Aspect -->  
    <aop:aspectj-autoproxy />  
</beans>
War es hilfreich?

Lösung

What did the trick is that I declared the beans in the xml and removed the @Component annotation from the java classes. Definitely it is a problem with how spring scans my project for @Component defined classes, but I will figure that out on my own. Thank you for your answers!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top