Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find the example applicationContext.xml file

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.

like image 481
Anshul Avatar asked Feb 09 '15 16:02

Anshul


People also ask

What is applicationContext XML file?

Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application.

Where do I put applicationContext xml?

It needs to be in the classpath. You can put the original editable instance anywhere (e.g. a config directory off the root) but then you will need to have your build management tool (e.g. Ant or Maven ) copy it into the classpath for the runtime.

Where is applicationContext xml in Spring boot?

src/main/resources/applicationContext.xml: properties in src/main/resources too.

How do I find application context?

You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.


1 Answers

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>
like image 155
erhun Avatar answered Sep 21 '22 12:09

erhun