Spring and classpath* load configuration files

Source: Internet
Author: User
The profile xml/properties is distributed among the individual jar packages (keeping the application relatively independent).
The problem arises from: the original project directly pulled over, packaging, deployment, start on the error, Applicationcontext.xml can not find. It is clear that the configuration file in the jar package is not read.

Java Code Workarounds:
1,classpath:applicationcontext.xml Change to Classpath*:applicationcontext.xml
This is the same thing. Effect: Classpath*:/applicationcontext.xml

2, refer to other configuration files in Applicationcontext.xml:
If you write this in the same jar package,
Java code <import resource= "Classpath:/applicationcontext-action.xml"/>
To import the configuration files from other jar packages, write this
Java code <import resource= "Classpath*:/applicationcontext-service.xml"/>

As simple as this, repack, deploy, start successfully.
Open http://127.0.0.1:8080/portal/index.htm in the browser
Success

3,classpath two ways to use:
Classpath*:applicationcontext.xml will be added to this project and all applicationcontext.xml files in the jar package root directory, which are used across the jar package dependencies

Classpath:applicationContext.xml is only added to the Applicationcontext.xml file in the root directory of this project and is recommended when it is not dependent on the configuration files of other jar packages to avoid conflicts.


Note:

Find a common spring-based configuration:

<bean class= "Org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean ">
                <property name=" Resources ">
                    <array>
                        <value>classpath*:/jobs/*.xml</value >
                        <!--EE 7 specification---
                        <value>classpath*:/META-INF/batch-jobs/*.xml</value>
                    </array>
                </property>
            </bean>


This style of writing is actually based on the spring resource class, which can be used in Java

<pre style= "Background-color: #2b2b2b; color: #a9b7c6; font-family: ' Menlo '; font-size:14pt;" ><span style= "color: #cc7832;" >new </span>defaultresourceloader (). GetResource (location)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.