e11438
e11438

Reputation: 894

Name of the Spring XML file in Spring MVC

I'm new to Spring MVC and when i'm going through tutorials in different tutorials they name spring XML file in different names. As examples "spring-web-servlet.xml", "dispatcher-servlet.xml", "spring-dispatcher-servlet.xml". Can somebody explain to me why is that.

Upvotes: 4

Views: 7097

Answers (5)

revanth nanban
revanth nanban

Reputation: 1

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:message"/>
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/bms1"/>
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

<tx:annotation-driven />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- <bean id="bookDao" class="com.hcl.dao.BookDaoImpl"> <property name="sessionFactory" 
    ref="sessionFactory" /> </bean> -->

<!-- <bean id="bookService" class="com.hcl.service.BookServiceImpl"> <property 
    name="bookDao" ref="bookDao"></property> </bean> -->

Upvotes: -1

revanth nanban
revanth nanban

Reputation: 1

<!-- <mvc:resources mapping="/resources/**" location="/resources/" /> -->

<context:component-scan base-package="com.cts.bankmanagement" />

Upvotes: 0

Jagadeesh Keerthi
Jagadeesh Keerthi

Reputation: 71

In this case, we need to add the listener also. Please correct me if I am wrong. org.springframework.web.context.ContextLoaderListener

Otherwise we can mention the custom context config file location through an init param to the DispatcherServlet for "contextConfigLocation".

Upvotes: 1

Master Slave
Master Slave

Reputation: 28519

What barunsthakur has answered is a means to change the default location of the spring configuration file by using the context param contextConfigLocation. If this param is not specified, spring mvc expects the following

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application

Consider the following DispatcherServlet Servlet configuration (in the web.xml file):

<web-app>
    <servlet>
        <servlet-name>golfing</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>golfing</servlet-name>
        <url-pattern>/golfing/*</url-pattern>
    </servlet-mapping>
</web-app>

With the above Servlet configuration in place, you will need to have a file called /WEB-INF/golfing-servlet.xml in your application

Your projects most likely use default configurtion and in this case you must pair the name of your configuration file, with the servlet-name of your DispatcherServlet

you can read more in the docs here

Upvotes: 8

barunsthakur
barunsthakur

Reputation: 1216

The name of spring xml file doesn't matter. You can name it anything(something semantic will be good) and need configure the web.xml using same name. for e.g if the filename is spring-dispatcher-servlet.xml the add this entry in web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:META-INF/spring-dispatcher-servlet.xml
    </param-value>
</context-param>

Upvotes: 3

Related Questions