SpringAop与SpringMVC整合(controller层对AOP支持)
2017-04-21 14:59
519 查看
在Web应用中,有在web层添加AOP切面支持的业务(比如:想在web层缓存、记日志、统一添加验证)等;使用AOP的好处是,不会对现有业务代码污染,耦合度低;
1、问题
完成web层AOP配置之后,发现不起作用未生效;
2、问题原因
Spring 父子容器问题,项目在加载配置文件的过程中,Spring配置和SpringMVC配置是分开加载的;在项目最初就加载了(org.springframework.web.context.ContextLoaderListener)spring.xml,生成一个WebApplicationContext,放置在ServletContext中 key:ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName()
+ ".ROOT";
The created application context will be registered into the ServletContext under the attribute name WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 而Spring-mvc.xml实在DispatcherServlet下加载成功的,
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}生成子容器 存放与ServletContext key :WebApplicationContext.class.getName() + ":" + getServletContext().getContextPath()) + "/" + getServletName()
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
if (this.contextId != null) {
wac.setId(this.contextId);
}
else {
// Generate default id...
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());
}
}
wac.setServletContext(getServletContext());
wac.setServletConfig(getServletConfig());
wac.setNamespace(getNamespace());
wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
// The wac environment's #initPropertySources will be called in any case when the context
// is refreshed; do it eagerly here to ensure servlet property sources are in place for
// use in any post-processing or initialization that occurs below prior to #refresh
ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
}
postProcessWebApplicationContext(wac);
applyInitializers(wac);
wac.refresh();
}
要想Springmvc AOP生效,必须要处于同一容器空间。
spring.xml配置不扫描controller
<context:component-scan base-package="com.package">
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
spring-mvc.xml
<context:component-scan base-package="com.package.web">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
spring.xml Aop配置
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="myAspect" class="com.package.MyAspect"/>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.package.web.*.*(..))"/>
<aop:aspect ref="myAspect">
<aop:around method="aroundMethod" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
1、问题
完成web层AOP配置之后,发现不起作用未生效;
2、问题原因
Spring 父子容器问题,项目在加载配置文件的过程中,Spring配置和SpringMVC配置是分开加载的;在项目最初就加载了(org.springframework.web.context.ContextLoaderListener)spring.xml,生成一个WebApplicationContext,放置在ServletContext中 key:ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName()
+ ".ROOT";
The created application context will be registered into the ServletContext under the attribute name WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 而Spring-mvc.xml实在DispatcherServlet下加载成功的,
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}生成子容器 存放与ServletContext key :WebApplicationContext.class.getName() + ":" + getServletContext().getContextPath()) + "/" + getServletName()
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
if (this.contextId != null) {
wac.setId(this.contextId);
}
else {
// Generate default id...
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());
}
}
wac.setServletContext(getServletContext());
wac.setServletConfig(getServletConfig());
wac.setNamespace(getNamespace());
wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
// The wac environment's #initPropertySources will be called in any case when the context
// is refreshed; do it eagerly here to ensure servlet property sources are in place for
// use in any post-processing or initialization that occurs below prior to #refresh
ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
}
postProcessWebApplicationContext(wac);
applyInitializers(wac);
wac.refresh();
}
要想Springmvc AOP生效,必须要处于同一容器空间。
spring.xml配置不扫描controller
<context:component-scan base-package="com.package">
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
spring-mvc.xml
<context:component-scan base-package="com.package.web">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
spring.xml Aop配置
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="myAspect" class="com.package.MyAspect"/>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.package.web.*.*(..))"/>
<aop:aspect ref="myAspect">
<aop:around method="aroundMethod" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
相关文章推荐
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(四)Spring AOP中使用log4j实现http请求日志入mongodb
- Spring ,Springmvc,mybatis三大框架整合的配置文件支持
- Spring整合Mongodb,Maven的依赖,Spring配置,MongoDB的公共操作类,使用SpringMVC的Controller进行测试并返回结果的案例
- 《Spring攻略》第3章 Spring AOP和AspectJ支持
- Spring对AOP的支持1
- Spring对AOP的支持
- 实例简述Spring AOP之对AspectJ语法的支持
- spring支持的AOP类型
- Axis2整合Spring AOP
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- Spring AOP 概念理解及@AspectJ支持
- Spring对AOP的支持 推荐
- 整合spring 支持声明式事务的hibernate
- Spring Aop支持的两种方式
- Spring对AOP的支持2
- [原]spring学习笔记9.3-Spring对Aop的支持-配置文件的方式
- [原]spring学习笔记9.2-Spring对Aop的支持-annotation的方式
- spring2.5整合hibernate3.3的泛型Dao并加入spring jdbc的支持
- DWR高级主题之DWR与spring集成(使用SpringMVC时dwr:controller标签说明)