tomcat启动时没有把spring配置文件定义的bean加载进来
最近在把自己的之前搭建的那个通用的ssh框架,配置成dubbo+zookeeper这样的分布式架构。搭建的思路就是把项目的service和controller分离出来。两个项目之间的服务就通过zookeeper注册和订阅的方式来完成。service层的搭建还是比较顺利的。在加入了zookeeper和dubbo的相关配置和分离了controller层的配置后接口服务能够自动的注册到dubbo。
但是在搭建controller层的项目时,在相关的配置完成后,服务通过tomcat启动,一开始是发现项目能够订阅到service层的服务的,以为就没有错了,但是后面我在测试项目的整合的redis的时候发现controller层的容器没有成功初始化redis配置文件中定义的bean对象。
(服务是已经订阅到的)
我在服务启动后把容器管理的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配置文件
<?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">
<!–设置不输出null字段–>
<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对象没有,报一个空指针异常。
在这里先给出解决方案:(其实这个算是我配置的问题了,自己基础不好包含而且粗心导致的。我的项目地址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配置文件也指定才行。
所以:
<!--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>
修改成上面后测试接口就正常了:
谨记!!!