我有一个Spring应用程序,它必须有两个servlet(dispatcherMain
和dispatcherCatalog
)通过Hibernate(finance
和finances_global
)处理不同的数据库。
首先,在web.xml
我定义:
application-config.xml
)dispatcherMain-servlet.xml
,dispatcherCatalog-servlet.xml
)databaseConfigMain.xml
,databaseConfigCatalog.xml
)。 第二次,在每个servlet的配置文件中,我创建了一个bean MainConfig
,它读取资源包 - 为每个servlet(mainServlet.properties
和{{1}分开})。
但事实证明,配置文件永远不会被读取,因此部署失败。在日志文件中,我有很多catalogServlet.properties
和java.lang.RuntimeException: Can't read property "server.redis"
。
我做错了什么?
这是我的配置。
Caused by: java.lang.NullPointerException at klab.backend.utils.MainConfig.get(MainConfig.java:351)
:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
...
<!-- Configurations for the root application context (parent context) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-config.xml
</param-value>
</context-param>
<!-- Configurations for the DispatcherServlet application contexts (child contexts) -->
<!-- Configurations for servlet "dispatcherMain" -->
<servlet>
<servlet-name>dispatcherMain</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcherMain-servlet.xml
/WEB-INF/databaseConfigMain.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherMain</servlet-name>
<url-pattern>/rest/api/finance/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateFilterForFinanceMain</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilterForFinanceMain</filter-name>
<url-pattern>/rest/api/finance/*</url-pattern>
</filter-mapping>
<!-- Configurations for servlet "dispatcherCatalog" -->
<servlet>
<servlet-name>dispatcherCatalog</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcherCatalog-servlet.xml
/WEB-INF/databaseConfigCatalog.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherCatalog</servlet-name>
<url-pattern>/rest/api/finance-global/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateFilterForFinanceCatalog</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilterForFinanceCatalog</filter-name>
<url-pattern>/rest/api/finance-global/*</url-pattern>
</filter-mapping>
</web-app>
:
application-config.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="klab.finance.system"/>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
<property name="validator" ref="validator"/>
</bean>
<bean name="CorsFilter" class="klab.backend.filter.CorsFilter" />
<bean id="swagger2Config" class="klab.finance.config.SwaggerConfig"/>
</beans>
:
dispatcherMain-servlet.xml
<?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"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="klab.backend.utils.MainConfig" id="mainConfigMain">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations">
<list>
<value>/WEB-INF/mainServlet.properties</value>
<value>/WEB-INF/build.properties</value>
</list>
</property>
</bean>
<bean class="klab.backend.controller.base.DefaultController" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="writeAcceptCharset" value="true"/>
</bean>
<bean class="klab.backend.utils.json.JacksonView2HttpMessageConverter">
<property name="objectMapper">
<bean class="klab.backend.utils.json.KJsonMapper">
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="writeAcceptCharset" value="true"/>
</bean>
<bean class="klab.backend.utils.json.JacksonView2HttpMessageConverter">
<property name="objectMapper">
<bean class="klab.backend.utils.json.KJsonMapper">
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="klab.finance.main"/>
</beans>
:
dispatcherCatalog-servlet.xml
<?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"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="klab.backend.utils.MainConfig" id="mainConfig">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations">
<list>
<value>/WEB-INF/catalogServlet.properties</value>
<value>/WEB-INF/build.properties</value>
</list>
</property>
</bean>
<bean class="klab.backend.controller.base.DefaultController" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="writeAcceptCharset" value="true"/>
</bean>
<bean class="klab.backend.utils.json.JacksonView2HttpMessageConverter">
<property name="objectMapper">
<bean class="klab.backend.utils.json.KJsonMapper">
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="klab.finance.catalog"/>
</beans>
:
databaseConfigMain.xml
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="sessionFactory1" name="sessionFactory1" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>klab.finance.main.entity</value>
<value>klab.backend.entity.finance.main</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL81Dialect</prop>
<prop key="hibernate.show_sql" >${jdbc.debug}</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="jdbc.batch_size">20</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="klab.backend.utils.hibernate.RoutingDataSource">
<property name="poolProperties">
<props>
<prop key="max_size">3</prop>
<prop key="min_size">1</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory1"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
:
databaseConfigCatalog.xml
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="sessionFactory" name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>klab.backend.entity.finance.catalog</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL81Dialect</prop>
<prop key="hibernate.show_sql" >${jdbc.debug}</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="jdbc.batch_size">20</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="klab.backend.utils.hibernate.RoutingDataSource">
<property name="poolProperties">
<props>
<prop key="max_size">3</prop>
<prop key="min_size">1</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
:
mainServlet.properties
sql.settings.limit = 1000
jdbc.debug = true
server.redis = 192.168.1.56
db.default = jdbc:postgresql://192.168.1.56/finances?user=admin&password=aFdD4aEcf5j7Cia;
callback.default = http://192.168.1.57/rest/api
auto.restart=1
context.count=3
rabbit.host = 192.168.1.56
rabbit.port = 5672
rabbit.user = guest
rabbit.password = guest
monitoring.query.size = 10
monitoring.enabled = 1
application.version = ${build.number}
:
catalogServlet.properties