16

With Spring 3 distribution there was a project folder which was packaged in the distribution . This project folder had sample applicationContext.xml file which can be used . However when I have downloaded Spring 4 distribution from here it does not come with a project folder and I am not able to find the sample applicationContext.xml. Where can I find the example applicationContext.xml file.

erhun
  • 3,549
  • 2
  • 35
  • 44
Anshul
  • 267
  • 1
  • 4
  • 13

1 Answers1

17

You can use this for further information check here.

Below sample contains configuration as follows:

  • component-scan base com.foo which thinking as your root folder.
  • Basic dataSource definition.
  • Property place holder file(database.properties)
  • Message Source for localization support.

sample ApplicationContext.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:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:property-placeholder location="classpath:/database.properties" />
    <context:component-scan base-package="com.foo" />


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="5" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
    </bean>

</beans>
erhun
  • 3,549
  • 2
  • 35
  • 44
  • BTW ,We dont need ApplicationConetxt in Spring 4 .Annotations are there to do all job.I face thi issue and then removed this file "ApplicationConetxt" and it worked. – Ankur Srivastava Feb 09 '17 at 21:41
  • Yes this is not new feature of Spring but it is hard to find a working applicationContext.xml in all blogs. – erhun Feb 11 '17 at 12:09
  • 3
    Also, many Spring projects are written using xml files like the applicationContext.xml so learning this legacy format is good when encountering these. – Mushy Sep 11 '17 at 15:35