@value Annotations in Spring

Source: Internet
Author: User

There are many ways to find @value in the project

The role of @value injection:

The external values are injected dynamically into the bean by @value.

1, can inject ordinary string

2. Inject operating System Properties

3. Inject expression results

4. Inject Other bean properties

5. Inject File Resources

6. Inject URL Resources

@value ("Nomalchacter")

Private String char;//Inject ordinary characters

@value ("#{sytemproperties[' Os.name '}")

Private String systempropertiesname;//injected into the operating system

@value ("#{t (Java.lang.Math). Random () *100.0}")

Private double randomnumber;//injection expression result

@Value ("#{bean.name}")

Private String Fromanotherbean; Inject other bean properties: Inject the properties of the Beaninject object name

@Value ("Classpath:com/1.txt")

Private Resource resourcefile; Inject File Resources

@Value ("http://www.gogle.com")

Private Resource Testurl; Inject URL Resource

The value of the property file is injected into the Bean property value by @value ("${app.name}") syntax.

@Value ("${person.name}")

private String name;

@Value ("${person.name.firstname}")

Private String firstname;//injected with name FirstName


There are #{} and ¥{} in the above usage, what is the difference between them?

#{..} Primarily used to execute Spel expressions and assign content to properties

${..} Used primarily to load values from an external properties file

#{..} and ${...} Can be mixed, but must #{} outside, ${} in it



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.