servletcontextlistener - How to create servlet-context.xml & servlet-context-dispatcher.xml files for spring -


in spring spring controller have written @postconstruct configured function, problem is involked 2 times when run project.below web.xml , servlet-context.xml files.

web.xml :

        <?xml version="1.0" encoding="utf-8"?>     <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1">     <display-name>savemoneyoauth</display-name>          <servlet>             <servlet-name>myproject</servlet-name>             <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>         <init-param>             <param-name>contextconfiglocation</param-name>             <param-value>/web-inf/servlet-context.xml</param-value>         </init-param>                     <load-on-startup>1</load-on-startup>             </servlet>               <servlet-mapping>                     <servlet-name>myproject</servlet-name>                     <url-pattern>/</url-pattern>             </servlet-mapping>                     <listener>                     <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>             </listener>              <context-param>                     <param-name>contextconfiglocation</param-name>                     <param-value>                   /web-inf/servlet-context.xml,                   /web-inf/spring-security.xml             </param-value>             </context-param>               <!-- spring security -->              <filter>                     <filter-name>springsecurityfilterchain</filter-name>                     <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class>             </filter>              <filter-mapping>                     <filter-name>springsecurityfilterchain</filter-name>                     <url-pattern>/*</url-pattern>             <dispatcher>request</dispatcher>                     <dispatcher>error</dispatcher>             </filter-mapping>     </web-app> 

servlet-context.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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"             xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd     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-4.1.xsd     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">             <context:annotation-config />         <context:component-scan base-package="com.example.myproject" />             <mvc:annotation-driven>             <mvc:message-converters register-defaults="false">                 <bean class="org.springframework.http.converter.stringhttpmessageconverter" />                 <bean class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter" />             </mvc:message-converters>             </mvc:annotation-driven>              <bean id="multipartresolver"         class="org.springframework.web.multipart.commons.commonsmultipartresolver">            <!-- 1 of properties available; maximum file size in bytes -->         <property name="maxuploadsize" value="1000000000" />             </bean>                <bean id="mydatasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">         <property name="driverclassname" value="com.mysql.jdbc.driver"/>         <property name="url" value="jdbc:mysql://localhost:3306/saveit"/>         <property name="username" value="root"/>         <property name="password" value="password"/>         <property name="validationquery" value="select 1"/>     </bean>      <!-- hibernate session factory -->     <bean id="mysessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean">         <property name="datasource" ref="mydatasource"/>         <property name="packagestoscan">         <array>             <value>com.example.myproject</value>         </array>         </property>         <property name="hibernateproperties">         <value>             hibernate.dialect=org.hibernate.dialect.mysqldialect         </value>         </property>     </bean>     <!-- hibernate transaction manager -->     <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager">         <property name="sessionfactory" ref="mysessionfactory"/>     </bean>      <!-- activates annotation based transaction management -->     <tx:annotation-driven transaction-manager="transactionmanager"/>     </beans> 

i found problem because have configured servlet-context.xml both contextloaderlistener , dispatcherservlet. avoid problem have split xml file sperate these two.so changed dispatcherservlet below in web.xml

        <servlet>         <servlet-name>savemoneyoauth</servlet-name>         <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>     <init-param>         <param-name>contextconfiglocation</param-name>         <param-value>/web-inf/servlet-context-dispatcher.xml</param-value>     </init-param>                 <load-on-startup>1</load-on-startup>         </servlet> 

but how can split file codes servlet-context.xml , servlet-context-dispatcher.xml? should write in both file?please me.

what load in via contextloaderlistener, should root webapplicationcontext

the root webapplicationcontext should contain infrastructure beans should shared between other contexts , servlet instances. these inherited beans can overridden in servlet-specific scope, , can define new scope-specific beans local given servlet instance.

beans load via dispatcherservlet application specific servlet context , inherit beans defined in root webapplication context.

so ideally application specific beans such session factory, transaction manager , other relevant infrastructure beans should go in root webapplication context. beans such controllers , view resolvers should go in servlet context

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-servlet


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -