Spring MVC reads configuration files through @PropertySource and @value

Source: Internet
Author: User

In this article, we will use the spring @propertysource and @value two annotations to read the values from the configuration file properties. First, a snippet of Java code:

@Component @propertysource (value= {"Classpath:common.properties", "Classpath:abc.properties"}) Public classconfigs {@Value ("${connect.api.apikeyid}")     PublicString Apikeyid; @Value ("${connect.api.secretapikey}")     PublicString Secretapikey;  PublicString Getapikeyid () {returnApikeyid; }     PublicString Getsecretapikey () {returnSecretapikey; }}

Let us specifically analyze:

1, @Component Note that this is an ordinary bean, component scanning will be scanned and injected into the bean container; We can automate the assembly in other places that reference this type. @Autowired This annotation indicates that the bean is automatically assembled. Like what:

@Controller  Public class HomeController {    @Autowired    private  configs configs;}

2, @PropertySource annotations are used to specify the path of the configuration file to read to read these configuration files, you can specify more than one configuration file;

3, @Value ("${connect.api.apikeyid}") is used to read the value of the property Key=connect.api.apikeyid and assigns the value to the property apikeyid;

4. Obtain the property value by providing a Get method, such as:

@Controller  Public class HomeController {    @Autowired    private  configs configs;         Private void throws Exception {        configs.getcardkey (), Consts.charset_utf8));}    }

Summarize:

@[email protected][email protected]== Powerful + convenient + efficient

Spring MVC reads configuration files through @PropertySource and @value

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.