简体   繁体   English

Spring AOP方面未执行

[英]Spring aop aspects not executing

Spring aspects annotations (before, after, etc) are not executing. Spring方面注释(之前,之后等)未执行。 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. 每次访问该页面时都会打印,但是用@Before注释的方法永远不会运行。 Any help will be greatly appreciated. 任何帮助将不胜感激。

Controller.java 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 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 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 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>

What did the trick is that I declared the beans in the xml and removed the @Component annotation from the java classes. 诀窍是,我在xml中声明了bean,并从java类中删除了@Component注释。 Definitely it is a problem with how spring scans my project for @Component defined classes, but I will figure that out on my own. 春季如何扫描我的项目中@Component定义的类绝对是一个问题,但是我会自己解决这个问题。 Thank you for your answers! 谢谢您的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM