未加载Spring上下文

拉胡尔·塔奇拉斯(Rahul Thachilath)

我正在尝试将我的旧项目从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周中。

拉胡尔·塔奇拉斯(Rahul Thachilath)

我发现问题出在SpringHttpContextLoaderListener它覆盖了一种方法createContextLoaderSpring 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

未加载Spring上下文

来自分类Dev

在tomcat上部署后未加载Spring上下文

来自分类Dev

Spring JUnit Test未加载完整的应用程序上下文

来自分类Dev

扩展菜单未加载上下文菜单

来自分类Dev

上下文文件未加载Tomcat

来自分类Dev

Spring @DirtiesContext是否重新加载Spring上下文?

来自分类Dev

Spring递归加载应用程序上下文

来自分类Dev

MVC脚手架未加载模型或上下文

来自分类Dev

@SpringBootTest的问题:上下文未加载,Spock无法连接到@Autowire

来自分类Dev

Windows 10未加载文件夹项目/上下文菜单

来自分类Dev

确定程序集的加载上下文

来自分类Dev

在Atmosphere的MeteorServlet中使用Spring DispatcherServlet时,Spring上下文会加载两次

来自分类Dev

Spring上下文动态变化

来自分类Dev

Spring REST模拟上下文路径

来自分类Dev

Spring Boot和上下文路径

来自分类Dev

了解Spring MVC中的上下文

来自分类Dev

用于DelegatingFilterProxy的单独Spring上下文

来自分类Dev

Spring安全上下文,几个用户

来自分类Dev

找不到Spring Application上下文

来自分类Dev

了解Spring MVC中的上下文

来自分类Dev

Spring上下文动态变化

来自分类Dev

Spring Integration多上下文

来自分类Dev

用于DelegatingFilterProxy的单独Spring上下文

来自分类Dev

Spring安全上下文,几个用户

来自分类Dev

Thymeleaf Spring Mail上下文错误

来自分类Dev

找不到Spring Application上下文

来自分类Dev

上下文之间的Spring Integration通信

来自分类Dev

spring paypal api上下文问题

来自分类Dev

Spring 测试:缺少 servlet 上下文