abh*_*nag 2 java aop spring-mvc spring-aop
我使用以下结构但失败但无法创建代理调用.
在controller-servlet.xml中
<context:component-scan base-package="com.controller" />
<mvc:annotation-driven/>
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:default-servlet-handler/>
Run Code Online (Sandbox Code Playgroud)
在application-context.xml中
<context:component-scan base-package="com.common" />
<context:component-scan base-package="com.dao" />
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />
<!-- Aspect Bean Definition
<bean id="aspectBean" class="com.common.AspectImple" /> -->
Run Code Online (Sandbox Code Playgroud)
Aspect类
package com.common;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectImple {
@Before("execution(* com.controller.JobController.*(..))")
public void beforeImpl() {
System.out.println(" Before Done ");
}
}
Run Code Online (Sandbox Code Playgroud)
控制器类
@Controller
public class JobController {
@RequestMapping(method = RequestMethod.GET, value = "/xyz")
public ModelAndView abc(HttpServletRequest request,
HttpServletResponse response) {
...
m1(10);
...
}
}
public void m1(int i){
System.out.println(" AOP Done ");
}
Run Code Online (Sandbox Code Playgroud)
AOP不工作,在调用abc(..)&m1(..)方法时,beforeImpl()没有被调用.有人可以帮我解决这个问题.
搬家怎么样
<context:component-scan base-package="com.dao" />
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />
Run Code Online (Sandbox Code Playgroud)
从application-context.xml到controller-servlet.xml?
要应用的方面和bean需要位于同一ApplicationContext中,但ApplicationContext不知道WebApplicationContext.
小智 5
我遇到了同样的问题,但正在使用基于注释的配置。为了让 AOP 为 @Controller 工作,这样做就成功了:
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan(basePackages = {"com.mypackage"})
public class MvcConfig extends WebMvcConfigurationSupport
{
...
@Bean
public MyAspect myAspect()
{
return new MyAspect();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7788 次 |
| 最近记录: |