Read Properties file under Spring

Source: Internet
Author: User

  Because the XML file in spring is configured with the

<bean id= "Validator" class= "Org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" >
<property name= "Providerclass" value= "Org.hibernate.validator.HibernateValidator"/>
<!--not set by default is Validationmessages.properties---classpath
<property name= "Validationmessagesource" ref= "Validatemessagesource"/>
</bean>

  In this case, the result of the read failure occurs when the properties file is read by the normal program.

For example:

Properties prop =new Properties (); InputStream in = Object.Class.getresourceasstream ("/validatemessage.properties"); try {   prop.load (in); Param2 = Prop.getproperty ("InitYears2"). Trim (); }catch (IOException e) {
Todo
}  

  Therefore, the properties file needs to be loaded using the Org.springframework.core.io.support.PropertiesLoaderUtil class provided by spring

For example:

Resource Resource = new Classpathresource ("/validatemessages.properties"); Properties Properties = new properties (); try {
Properties = propertiesloaderutils.loadproperties (Resource);
} catch (IOException e) {
Todo
}

   This will enable you to successfully read the properties file and get the desired value.

Read Properties file under Spring

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.