java - Multiple servlets with different context in one Spring application -
i have spring application, has have 2 servlets (dispatchermain
, dispatchercatalog
) working different databases via hibernate (finance
, finances_global
).
first, in web.xml
define:
- parent context config location (
application-config.xml
) - servlets , own:
- context config locations (
dispatchermain-servlet.xml
,dispatchercatalog-servlet.xml
) - hibernate datasources config files (
databaseconfigmain.xml
,databaseconfigcatalog.xml
).
- context config locations (
second, in each of servlets' config file, create bean mainconfig
reads resource bundle - separate each servlet (mainservlet.properties
, catalogservlet.properties
).
but turns out that configuration files never read, deployment failes. in log file, have lots of java.lang.runtimeexception: can't read property "server.redis"
, caused by: java.lang.nullpointerexception @ klab.backend.utils.mainconfig.get(mainconfig.java:351)
.
what doing wrong?
here configs.
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 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 dispatcherservlet application contexts (child contexts) --> <!-- configurations 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 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
:
sql.settings.limit = 1000 jdbc.debug = true server.redis = 192.168.1.56 db.default = jdbc:postgresql://192.168.1.56/finance_global?user=admin&password=ddeo7qcpoade5iqo; 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}
Comments
Post a Comment