tomcat启动时没有把spring配置文件定义的bean加载进来

tomcat启动时没有把spring配置文件定义的bean加载进来

最近在把自己的之前搭建的那个通用的ssh框架,配置成dubbo+zookeeper这样的分布式架构。搭建的思路就是把项目的service和controller分离出来。两个项目之间的服务就通过zookeeper注册和订阅的方式来完成。service层的搭建还是比较顺利的。在加入了zookeeper和dubbo的相关配置和分离了controller层的配置后接口服务能够自动的注册到dubbo。
tomcat启动时没有把spring配置文件定义的bean加载进来_第1张图片

但是在搭建controller层的项目时,在相关的配置完成后,服务通过tomcat启动,一开始是发现项目能够订阅到service层的服务的,以为就没有错了,但是后面我在测试项目的整合的redis的时候发现controller层的容器没有成功初始化redis配置文件中定义的bean对象。
tomcat启动时没有把spring配置文件定义的bean加载进来_第2张图片
(服务是已经订阅到的)

我在服务启动后把容器管理的bean对象信息都打印出来发现我spring整合redis配置文件定义的bean都没有在里面。

 [DUBBO] Refer dubbo service com.lung.application.inter.TestService from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=dubbo-project-manager-consumer&check=false&default.delay=-1&default.retries=0&default.timeout=1200000&delay=-1&dubbo=2.5.3&interface=com.lung.application.inter.TestService&methods=queryAreaInfo,getResult&pid=29313&revision=1.0-SNAPSHOT&side=consumer×tamp=1539567979182, dubbo version: 2.5.3, current host: 172.17.0.1
[INFO ] 2018-10-15 09:46:19,274 method:com.lung.common.utils.ManagerBeansName.init(ManagerBeansName.java:25)
beanDefinitionCount29
roleController
testController
superController
managerBeansName
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0
mvcCorsConfigurations
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
mvcHandlerMappingIntrospector
mvcContentNegotiationManager
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
org.springframework.format.support.FormattingConversionServiceFactoryBean#0
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
mvcUriComponentsContributor
org.springframework.web.servlet.handler.MappedInterceptor#0
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0
fastJsonHttpMessageConverter
org.springframework.web.servlet.view.InternalResourceViewResolver#0
multipartResolver
[INFO ] 2018-10-15 09:46:19,541 method:org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:362)
Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
[INFO ] 2018-10-15 09:46:19,628 method:org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.reg

这是我的springMVC配置文件




    
    
    
        
    
    
    
    
    
    
        
            
            
            
            
            
        
    

    
    
    
    
        
            
                text/html;charset=UTF-8
                application/json
            
        
        
            
                
                DisableCircularReferenceDetect
                
                QuoteFieldNames
                
                WriteNullNumberAsZero
                
                WriteNullListAsEmpty
                
                WriteNullStringAsEmpty
                
                WriteNullBooleanAsFalse
                
                WriteDateUseDateFormat
            
        
    

    
    
        
        
    

    
    
        
        
        
    

    


然后这是web.xml文件




    Project Web Application

    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        contextConfigLocation
        classpath*:spring/spring-introduce.xml
    

    
    
        springDispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath*:/spring/springmvc.xml
        
        1
    
    
        springDispatcherServlet
        /
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        CharacterEncodingFilter
        /*
    

    
    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        HiddenHttpMethodFilter
        /*
    
    
        HttpPutFormContentFilter
        org.springframework.web.filter.HttpPutFormContentFilter
    
    
        HttpPutFormContentFilter
        /*
    


通过测试发现redis的bean对象没有,报一个空指针异常。
tomcat启动时没有把spring配置文件定义的bean加载进来_第3张图片
tomcat启动时没有把spring配置文件定义的bean加载进来_第4张图片

在这里先给出解决方案:(其实这个算是我配置的问题了,自己基础不好包含而且粗心导致的。我想项目地址commonManageProject)(mybatis-plus-version分支)
根本原因就是我在web.xml配置springDispatcherServlet的时候只是把springMVC的配置文件指定了,spring的配置文件没有指定。所以导致了tomcat启动后,只加载了pringmvc配置文件指定的bean。(因为分布式配置后,很多原来定义在springMVC配置文件bean都定义在了spring配置文件中)

这个问题在这里我卡住了很久都没有解决,一开始也是认为是我自己的配置问题,尝试着从几个方面找原因。
1、通过单元测试看看容器中redis对象是否存在:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring/spring-introduce.xml",
        "classpath*:/spring/springmvc.xml"})
public class TestRedis {

    @Autowired
    BaseRedisDao baseRedisDao;

    @Test
    public void TestRedis() {
        try {
            boolean set = baseRedisDao.set("baseRedisKey", "baseRedisvalue");
            System.out.println(set);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

发现对象是存在的

[INFO ] 2018-10-15 10:03:57,861 method:com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:423)
 [DUBBO] Refer dubbo service com.lung.application.inter.TestService from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=dubbo-project-manager-consumer&check=false&default.delay=-1&default.retries=0&default.timeout=1200000&delay=-1&dubbo=2.5.3&interface=com.lung.application.inter.TestService&methods=queryAreaInfo,getResult&pid=30327&side=consumer×tamp=1539569037813, dubbo version: 2.5.3, current host: 172.17.0.1
[INFO ] 2018-10-15 10:03:57,868 method:com.lung.common.utils.ManagerBeansName.init(ManagerBeansName.java:25)
beanDefinitionCount39
poolConfig
jedisConnectionFactory
redisTemplate
baseRedisDao
com.lung.common.utils.PropertyPlaceholder#0
config
dubbo-project-manager-consumer
com.alibaba.dubbo.config.RegistryConfig
roleServiceImpl
testServiceImpl
roleController
testController
superController
managerBeansName
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0
mvcCorsConfigurations
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
mvcHandlerMappingIntrospector
mvcContentNegotiationManager
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
org.springframework.format.support.FormattingConversionServiceFactoryBean#0
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
mvcUriComponentsContributor
org.springframework.web.servlet.handler.MappedInterceptor#0
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0
fastJsonHttpMessageConverter
org.springframework.web.servlet.view.InternalResourceViewResolver#0
multipartResolver
[INFO ] 2018-10-15 10:03:58,045 method:org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:362)
Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'

2、通过ClassPathXmlApplicationContext查看spring容器是否把redis的相关对象给初始化了:


public class TestSpring {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("classpath:/spring/spring-introduce.xml");
        int beanDefinitionCount = classPathXmlApplicationContext.getBeanDefinitionCount();
        System.out.println(beanDefinitionCount);
        String[] beanDefinitionNames = classPathXmlApplicationContext.getBeanDefinitionNames();
        for (String str :
                beanDefinitionNames) {
            System.out.println(str);
        }
    }
}

发现也是正常的

[INFO ] 2018-10-15 10:06:13,113 method:org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:317)
Loading XML bean definitions from URL [file:/home/lung/IdeaProjects/commonManageProject/commons-manager/commons-manager-web/target/classes/dubbo/dubbo-consumer.xml]
[INFO ] 2018-10-15 10:06:13,214 method:
using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter
10
poolConfig
jedisConnectionFactory
redisTemplate
baseRedisDao
com.lung.common.utils.PropertyPlaceholder#0
config
dubbo-project-manager-consumer
com.alibaba.dubbo.config.RegistryConfig
roleServiceImpl
testServiceImpl
[INFO ] 2018-10-15 10:06:14,425 method:com.alibaba.dubbo.config.AbstractConfig$1.run(AbstractConfig.java:450)

无奈之下,我重新去复习一下springMVC的相关基础了,后面发现自己好像在配置分布式服务,把spring的配置做了更加细分的解耦后,我在web.xml配置servlet时只声明的配置文件是不行的,这个时候需要把其他spring配置文件也指定才行。
所以:

 
    
        springDispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath*:/spring/springmvc.xml,classpath*:/spring/applicationContext-*.xml
        
        1
    
    
        springDispatcherServlet
        /
    

修改成上面后测试接口就正常了:
tomcat启动时没有把spring配置文件定义的bean加载进来_第5张图片

谨记!!!

你可能感兴趣的:(Spring)