2014-03-25 72 views
2

Spring方面注释(之前,之后等)未执行。我没有编译问题,“测试!!!”每次访问该页面时都会打印,但用@Before等注释的方法永远不会运行。任何帮助将不胜感激。Spring aop方面未执行

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> 
+0

将你的方面添加到配置文件中或使用'component-scan'来检索它。目前它不可用,所以什么也不会被应用。 –

+0

我已将添加到xml,但仍然没有任何反应 – Alex

+0

确保所有内容都位于相同的上下文中。如果你同时拥有一个'ContextLoaderListener'和'DispatcherServlet',那么它们就不适用​​于另一个。 –

回答

0

诀窍是我在xml中声明了bean,并从java类中删除了@Component注释。当然,这是一个问题,春天如何扫描我的项目@Component定义的类,但我会自己解决。谢谢您的回答!

+0

您应该删除问题。你没有提供足够的细节给任何人来帮助你,这是否有助于其他人的疑问。 –

+0

没有提供足够的详细资料?你还能提供什么? – Alex

+0

您使用'component-scan'进行新配置并澄清M. Deinum建议的内容。 –