您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: