开发者社区> chinpang> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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

简介: tomcat启动时没有把spring配置文件定义的bean加载进来 最近在把自己的之前搭建的那个通用的ssh框架,配置成dubbo+zookeeper这样的分布式架构。
+关注继续查看

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

最近在把自己的之前搭建的那个通用的ssh框架,配置成dubbo+zookeeper这样的分布式架构。搭建的思路就是把项目的service和controller分离出来。两个项目之间的服务就通过zookeeper注册和订阅的方式来完成。service层的搭建还是比较顺利的。在加入了zookeeper和dubbo的相关配置和分离了controller层的配置后接口服务能够自动的注册到dubbo。


img_2886a4de66734f8062af9564270fe8cc.png
2018-10-15 09-41-26屏幕截图.png

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


img_59e9b5dc20bcf61dd1bdabfad3828238.png
2018-10-15 09-45-17屏幕截图.png

(服务是已经订阅到的)

我在服务启动后把容器管理的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&timestamp=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配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--<context:property-placeholder ignore-unresolvable="true" location="classpath*:config.properties"/>-->
    <!--1. 扫描 -->
    <context:component-scan
            base-package="com.lung.application,com.lung.common.controller,com.lung.common.utils">
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>-->
    </context:component-scan>
    <!--2.两个标准配置 -->
    <!--将springmvc不能处理的请求交给tomcat -->
    <mvc:default-servlet-handler/>
    <!--能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            <ref bean="fastJsonHttpMessageConverter"/>
            <!--<ref bean="jsonMessageConverter"/>-->
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--jackson配置json为空不输出-->
    <!--<bean id="jsonMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="objectMapper">
            <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                &lt;!&ndash;设置不输出null字段&ndash;&gt;
                <property name="serializationInclusion" value="NON_NULL"/>
            </bean>
        </property>
    </bean>-->
    <!-- 使用fastJson来支持JSON数据格式 -->
    <bean id="fastJsonHttpMessageConverter"
          class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>application/json</value>
            </list>
        </property>
        <property name="features">
            <list>
                <!-- 避免循环引用 -->
                <value>DisableCircularReferenceDetect</value>
                <!-- 输出key时是否使用双引号 -->
                <value>QuoteFieldNames</value>
                <!-- 数值字段如果为null,输出为0,而非null -->
                <value>WriteNullNumberAsZero</value>
                <!-- List字段如果为null,输出为[],而非null -->
                <value>WriteNullListAsEmpty</value>
                <!-- 字符类型字段如果为null,输出为"",而非null -->
                <value>WriteNullStringAsEmpty</value>
                <!-- Boolean字段如果为null,输出为false,而非null -->
                <value>WriteNullBooleanAsFalse</value>
                <!-- Date的日期转换器 -->
                <value>WriteDateUseDateFormat</value>
            </list>
        </property>
    </bean>

    <!--3.配置视图解析器,方便页面返回-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--file upload settings-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="10485760000"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

    <!--拦截器配置-->

</beans>

然后这是web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
          version="3.0">

    <display-name>Project Web Application</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--1.Spring容器applicationContext.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/spring-introduce.xml</param-value>
    </context-param>

    <!--2.springmvc的前端控制器 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--3.编码过滤 解决post乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

通过测试发现redis的bean对象没有,报一个空指针异常。


img_c1deb42ff8ccf60c0ce73c4adabff978.png
2018-10-15 09-53-19屏幕截图.png

img_8cc23604056ec59484d7a116e93ecf54.png
2018-10-15 09-53-42屏幕截图.png

在这里先给出解决方案:(其实这个算是我配置的问题了,自己基础不好包含而且粗心导致的。我的项目地址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&timestamp=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配置文件也指定才行。
所以:

 <!--2.springmvc的前端控制器 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/spring/springmvc.xml,classpath*:/spring/applicationContext-*.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

修改成上面后测试接口就正常了:


img_82dce8acdbf97be6317c19a07a48c9e5.png
2018-10-15 10-12-21屏幕截图.png

谨记!!!

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
tomcat启动时报错ports are invalid,默认端口不要为-1
tomcat启动时报错ports are invalid,默认端口不要为-1
0 0
mac下Tomcat的安装,启动和关闭脚本配置,端口号更改
mac下Tomcat的安装,启动和关闭脚本配置,端口号更改
0 0
IDEA的Web项目启动Tomcat出现404错误
IDEA的Web项目启动Tomcat出现404错误
0 0
IDEA中SpringBoot工程运行报Tomcat启动异常(org.apache.catalina.LifecycleException)问题解决
IDEA中SpringBoot工程运行报Tomcat启动异常(org.apache.catalina.LifecycleException)问题解决
0 0
创建tomcat的服务,删除服务,通过服务来启动项目
创建tomcat的服务,删除服务,通过服务来启动项目
0 0
IEAD启动Tomcat控制台乱码问题
IEAD启动Tomcat控制台乱码问题
0 0
mac下载tomcat 8启动后无法使用
mac下载tomcat 8启动后无法使用
0 0
tomcat启动异常缓慢
tomcat启动异常缓慢
0 0
启动 Tomcat 应用服务器端口 8080 被占用排查思路及解决方式
启动 Tomcat 应用服务器端口 8080 被占用排查思路及解决方式
0 0
Tomcat 启动和关闭| 学习笔记
快速学习 Tomcat 启动和关闭,介绍了 Tomcat 启动和关闭系统机制, 以及在实际应用过程中如何使用。
0 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题)
立即下载
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战
立即下载
Java Spring Boot开发实战系列课程(第17讲):Spring Boot 2.0实战Docker容器Linux
立即下载