Spring4.0 代码配置的方式(以下代码) 改成 配置文件xml的方式

dctao114 2018-06-07 10:27:38

package org.activiti.app.servlet;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;

import org.activiti.app.conf.ApplicationConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

public class WebConfigurer
implements ServletContextListener
{
private final Logger log = LoggerFactory.getLogger(WebConfigurer.class);
public AnnotationConfigWebApplicationContext context;

public void setContext(AnnotationConfigWebApplicationContext context)
{
this.context = context;
}

public void contextInitialized(ServletContextEvent sce)
{
this.log.debug("Configuring Spring root application context");

ServletContext servletContext = sce.getServletContext();

AnnotationConfigWebApplicationContext rootContext = null;
if (this.context == null)
{
rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(new Class[] { ApplicationConfiguration.class });
if (rootContext.getServletContext() == null) {
rootContext.setServletContext(servletContext);
}
rootContext.refresh();
this.context = rootContext;
}
else
{
rootContext = this.context;
if (rootContext.getServletContext() == null) {
rootContext.setServletContext(servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootContext);

EnumSet<DispatcherType> disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC);

initSpring(servletContext, rootContext);
initSpringSecurity(servletContext, disps);

this.log.debug("Web application fully configured");
}

private void initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext)
{
this.log.debug("Configuring Spring Web application context");
AnnotationConfigWebApplicationContext appDispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
appDispatcherServletConfiguration.setParent(rootContext);
appDispatcherServletConfiguration.register(new Class[] { AppDispatcherServletConfiguration.class });

this.log.debug("Registering Spring MVC Servlet");
ServletRegistration.Dynamic appDispatcherServlet = servletContext.addServlet("appDispatcher", new DispatcherServlet(appDispatcherServletConfiguration));

appDispatcherServlet.addMapping(new String[] { "/app/*" });
appDispatcherServlet.setLoadOnStartup(1);
appDispatcherServlet.setAsyncSupported(true);

this.log.debug("Registering Activiti public REST API");
AnnotationConfigWebApplicationContext apiDispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
apiDispatcherServletConfiguration.setParent(rootContext);
apiDispatcherServletConfiguration.register(new Class[] { ApiDispatcherServletConfiguration.class });

ServletRegistration.Dynamic apiDispatcherServlet = servletContext.addServlet("apiDispatcher", new DispatcherServlet(apiDispatcherServletConfiguration));

apiDispatcherServlet.addMapping(new String[] { "/api/*" });
apiDispatcherServlet.setLoadOnStartup(1);
apiDispatcherServlet.setAsyncSupported(true);
}

private void initSpringSecurity(ServletContext servletContext, EnumSet<DispatcherType> disps)
{
this.log.debug("Registering Spring Security Filter");
FilterRegistration.Dynamic springSecurityFilter = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());

springSecurityFilter.addMappingForUrlPatterns(disps, false, new String[] { "/*" });
springSecurityFilter.setAsyncSupported(true);
}

public void contextDestroyed(ServletContextEvent sce)
{
this.log.info("Destroying Web application");
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
AnnotationConfigWebApplicationContext gwac = (AnnotationConfigWebApplicationContext)ac;
gwac.close();
this.log.debug("Web application destroyed");
}
}



以上是activiti-app-6.0的代码,但是以代码的方式进行spring的配置,我想改为xml文件方式进行配置,希望有经验的同学帮忙改一下,我等下会上传代码,我找了一些参考的网站(https://my.oschina.net/521cy/blog/702864),对照上面的方式进行配置还是不行
...全文
773 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanyangyang310 2018-10-19
  • 打赏
  • 举报
回复
楼主改成xml格式,是想把activiti的modeler集成进自己的项目吧,我最近也在做这个,最后发现可以不用修改这个配置,在web.xml配置自己需要的东西即可,如我的配置如下
<listener>
<listener-class>org.activiti.app.servlet.WebConfigurer</listener-class>
</listener>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/lab/*</url-pattern>
</servlet-mapping>
dctao114 2018-06-07
  • 打赏
  • 举报
回复
上面是项目代码,按快了没编辑就发了,项目里面有我对项目转配置的尝试(都注释了),默认账号密码都是admin
dctao114 2018-06-07
  • 打赏
  • 举报
回复
链接:https://pan.baidu.com/s/1BeE8euBSO3s1KS2Rz6HObg 密码:6k16
Braska 2018-06-07
  • 打赏
  • 举报
回复
你这段代码是自定义了Listener。跟osc的那篇博客关系不大。 你参考下这篇 web.xml中 Listener的使用

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧