Environment configuration for @value in Java

Source: Internet
Author: User

@value

At this stage I think everyone is not unfamiliar with annotations, @value usage is to get the configuration file information in the background, so as to facilitate the modification of some fixed configuration. Do not understand the @value Baidu can be detailed.

There are several steps to configuring the @value.

1, first create a new configuration file, System.properties

Directory structure such as

The contents are as follows

Jdbc.jdbcurl=jdbc:mysql://localhost:3306/commentdemo?useunicode=true&characterencoding

2. Configure the XML file. Is the spring configuration file, add the following content

<!--@value Annotations -<BeanID= "Configproperties"class= "Org.springframework.beans.factory.config.PropertiesFactoryBean">    < Propertyname= "Locations">        <List>            <value>Web-inf/config/system.properties</value>        </List>    </ Property></Bean><BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">    < Propertyname= "Properties"ref= "Configproperties" /></Bean>

3. Use in Controller

  @Controller @requestmapping ( "Usermanager" )   Class  Usercontroller extends   basecontroller{@Value ( "#{configproperties[' Jdbc.jdbcurl '}"  
    
     private 
      String Jdbcurl;        @Resource  private   Userdao Userdaoimpl; @RequestMapping (Value  = "/showlist"    String Showlist (model model) {SYSTEM.OUT.PRINTLN (Jdbcurl);     return  "index" 

4. See the results below

Environment configuration for @value in Java

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.