我正在尝试将我的旧项目从Spring 3.0.3.RELEASE迁移到4.0.9.RELEASE。从那一刻开始,我就将pom.xml依赖项更改为4.0.9。DispatcherServlet无法在应用程序上下文中找到Bean。
以下是我的文件
web.xml
<context-param>
<param-name>bootstrapConfig</param-name>
<param-value>
/com/core/choreographer/bootstrap/bootstrap-config.xml
</param-value>
</context-param>
<listener>
<listener-class>
com.core.bootstrap.BootstrapManager
</listener-class>
</listener>
<listener>
<listener-class>com.spring.http.SpringHttpContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>http-remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/remote/springhttp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>http-remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
bootstrap-config.xml具有所有要插入到Spring上下文中的bean。BootstrapManager读取它,它是ServletContextListener的子级,并插入到spring上下文中。如下。
public class BootstrapManager implements ServletContextListener
{
public void contextInitialized( ServletContextEvent ctxEvt )
{
URL cfgURL = "location to the xml file" ;
ServletContext context = ctxEvt.getServletContext() ;
bootstrap.initialize( cfgURL ) ;
}
}
public class Bootstrap
{
public void initialize( final URL cfgURL ) throws BootstrapException
{
XMLConfiguration config = new XMLConfiguration() ;
URL dtdURL = ReflectionUtil.getResource( Bootstrap.class, "bootstrap-config.dtd" ) ;
// register it at the configuration
config.registerEntityId( "-//Bootstrap//DTD 1.0//EN", dtdURL ) ;
config.setValidating( true ) ;
config.setURL( cfgURL ) ;
config.load() ;
initBootstrapElements( config ) ;
}
private void initBootstrapElements( final XMLConfiguration config ) throws BootstrapException
{
HierarchicalConfiguration cfg = null ;
int numElements = config.getList( "bootstrap-element[@class]" ).size() ;
for( int i = 0 ; i < numElements ; i++ )
{
cfg = config.configurationAt( "bootstrap-element(" + i + ")" ) ;
initElement( cfg ) ;
}
}
public void initElement( final HierarchicalConfiguration config ) throws BootstrapException
{
String className = null ;
String propName = null ;
Object propVal = null ;
HierarchicalConfiguration propCfg = null ;
BootstrapElement element = null ;
className = config.getString( "[@class]" ) ;
element = (BootstrapElement) ReflectionUtil.createInstance( className.trim() ) ;
beanWrapper.setWrappedInstance( element ) ;
int numProps = config.getList( "property[@name]" ).size() ;
for( int i = 0 ; i < numProps ; i++ )
{
propCfg = config.configurationAt( "property(" + i + ")" ) ;
propName = propCfg.getString( "[@name]" ) ;
propVal = propCfg.getProperty( "[@value]" ) ;
if( propVal == null )
{
propVal = propCfg.getList( "value" ) ;
}
beanWrapper.setPropertyValue( propName, propVal ) ;
}
element.initialize() ;//This called the below new ClassPathXmlApplicationContext(
}
}
ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext( (String []) cfgLocations.toArray( new String [ 0 ] ) ) ;
然后我有一个ContextLoaderListener
public class SpringHttpContextLoaderListener extends ContextLoaderListener {
private ContextLoader loader;
protected ContextLoader createContextLoader() {
loader = new SpringHttpContextLoader();
return loader;
}
public ContextLoader getContextLoader(){
return loader;
}
}
这是ContextLoader
public class SpringHttpContextLoader extends ContextLoader {
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException {
SpringObjectFactory fact = BaseSpringObjectFactory.getInstance();
return fact.getApplicationContext();
}
}
在Spring 3.0.3中,所有这些都完美运行。当我尝试将所有Jar替换为4.0.9时。springhttp-servlet.xml中定义的Bean找不到bootstrap-config.xml中定义的Bean。
任何建议都将受到高度赞赏。我被困在过去的2-3周中。
我发现问题出在SpringHttpContextLoaderListener
。它覆盖了一种方法createContextLoader
,在Spring 3版getContextLoader
中已弃用,因此在Spring 4版中将其删除。因此,未将自定义上下文作为父上下文加载。
public class SpringHttpContextLoaderListener extends ContextLoaderListener {
private ContextLoader loader;
protected ContextLoader createContextLoader() {
loader = new SpringHttpContextLoader();
return loader;
}
public ContextLoader getContextLoader(){
return loader;
}
}
解决方案覆盖loadParentContext
班级ContextLoader
中的班级SpringHttpContextLoaderListener
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException {
SpringObjectFactory fact = BaseSpringObjectFactory.getInstance();
return fact.getApplicationContext();
}
这会将自定义创建的上下文设置为父上下文,并且可用于所有子上下文
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句