Java Spring aop方面未执行
Spring方面注释(之前、之后等)未执行。我没有编译问题,每次我访问该页面时都会打印“test!!!”,但是用@Before等注释的方法永远不会运行。任何帮助都将不胜感激 Controller.javaJava Spring aop方面未执行,java,spring,spring-mvc,annotations,aspectj,Java,Spring,Spring Mvc,Annotations,Aspectj,Spring方面注释(之前、之后等)未执行。我没有编译问题,每次我访问该页面时都会打印“test!!!”,但是用@Before等注释的方法永远不会运行。任何帮助都将不胜感激 Controller.java @Controller @RequestMapping("/person/") public class Controller { @Autowired private Content content; @RequestMapping(method=RequestM
@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>
关键在于我在xml中声明了bean,并从java类中删除了@Component注释。spring如何扫描我的项目中@Component定义的类肯定是个问题,但我会自己解决这个问题。谢谢你的回答 将您的特性添加到配置文件中,或使用组件扫描
拾取它。目前它不可用,因此将不应用任何内容。我已添加到xml中,但仍然没有发生任何事情确保所有内容都在相同的上下文中。如果您同时拥有ContextLoaderListener
和DispatcherServlet
两个方面,则其中一个方面的定义不适用于另一个方面。您应该删除该问题。您没有为任何人提供足够的详细信息来帮助您,这对其他任何人都有帮助是值得怀疑的。没有提供足够的详细信息?您还希望我提供什么?您的新配置,包括组件扫描
,以及关于M.Deinum建议的澄清。