Background
The spring development process often encounters the need to inject special values into member variables, such as normal values, files, URLs, configuration information, system variables, and so on. Spring mainly uses annotation @value to inject the corresponding value into the variable.
Commonly used injection types have the following:
1. Inject ordinary string.
2. Inject operating System properties.
3. Injection expression operation result.
4. Inject the properties of the other bean.
5. Inject the contents of the file.
6. Inject the website information.
7. Inject the property file. Sample preparation
Because the example needs to read the file and the content of the Web page, we introduce an IO package for easy reading:
<dependency>
<groupId>commons-io</groupId>
<artifactid>commons-io</ artifactid>
<version>2.4</version>
</dependency>
Create a folder under Rescoures with the name Ch2.value.
Create a Test.text under the folder, the content is random, our content is "test file".
Create a test.properties below the folder, which reads:
Book.author = Feige
book.name = Spring
Test Bean
Create a new class to test and declare it as a bean.
@Service public
class Demoservice {
@Value ("I am other attribute")
private String anothervalue;
Public String Getanothervalue () {return
anothervalue;
}
public void Setanothervalue (String anothervalue) {
this.anothervalue = anothervalue;
}
}
Configuration class
@Configuration @ComponentScan ("Ch2.value") @PropertySource ("Classpath:ch2/value/test.properties") public class
Config {@Value ("I am an ordinary string") private string nornal;
@Value ("#{systemenvironment[' Os.name ']}") Private String osname;
@Value ("#{t (Java.lang.Math). Random () *1000.0}") private double randomnumber;
@Value ("#{demoservice.anothervalue}") Private String anothervalue;
@Value ("Classpath:ch2/value/test.txt") private Resource testfile;
@Value ("http://www.baidu.com") private Resource Testurl;
@Value ("${book.name}") Private String bookname;
@Autowired Private environment environment;
public void Outsource () {System.out.println (nornal);
System.out.println (Osname);
System.out.println (Randomnumber);
System.out.println (Anothervalue);
try {System.out.println (ioutils.tostring (Testfile.getinputstream ())); System.out.println (Ioutils.tostring (Testurl.getinputstream ()));
}catch (Exception e) {e.printstacktrace ();
} System.out.println (BookName);
System.out.println (Environment.getproperty ("Book.author")); }
}
Running the sample
public class Main {public
static void main (String []args) {
Annotationconfigapplicationcontext context = new Anno Tationconfigapplicationcontext (config.class);
Config config = context.getbean (config.class);
Config.outsource ();
}
Directory structure
Run results
I'm an ordinary string
null
47.47599424058235
I'm the other property
test file
Spring
14:11:10.719 [main] DEBUG Org.springframework.core.env.propertysourcespropertyresolver-found key ' Book.author ' in [Class path resource [ch2/ Value/test.properties]] with type [String]
Feige
Summary @Configuration of knowledge points
The class, declared by the configuration annotation, is equivalent to an XML configuration file in spring that introduces the configuration class by instantiating a Annotationconfigapplicationcontext object:
Annotationconfigapplicationcontext context = new Annotationconfigapplicationcontext (config.class);
Can the Annotationconfigapplicationcontext construct method pass in multiple classes declared by configuration. The answer is yes.
This example uses the Annotationconfigapplicationcontext method to construct the following:
Public Annotationconfigapplicationcontext (Class ... annotatedclasses) {this
();
This.register (annotatedclasses);
This.refresh ();
}
@ComponentScan
Componentscan annotations can pass in a package name that represents all the classes in the name of the package, loading the class with the annotation declaration into the spring container, example @componentscan ("Ch2.value") Scans all classes under the package Ch2.value and loads the class of the annotation declaration into the spring container. @PropertySource
Propertysource annotations can be passed into a file or folder that loads the contents of all the. properties files under a file or folder into spring's configuration items for use with the value annotation. @Value Normal string
@Value ("I am an ordinary string")
private string nornal;
Operating System Properties
@Value ("#{systemenvironment[' Os.name ']}")
private String osname;
The operating system property is a static global variable systemenvironment deposit, which can be used to obtain the properties of the operating system. An expression value
@Value ("#{t (Java.lang.Math). Random () *1000.0}")
private double randomnumber;
The object of an expression must be wrapped by T () to execute. Properties of other Beans
@Value ("#{demoservice.anothervalue}")
private String anothervalue;
Demoservice is a Bean object, Anothervalue is one of its properties, which can be injected into the properties of the @value declaration by @value ("#{demoservice.anothervalue}"). Injecting file Resources
@Value ("Classpath:ch2/value/test.txt")
private Resource testfile;
Receive this file through resource. Inject Web Resources
@Value ("http://www.baidu.com")
private Resource Testurl;
Receive this resource through resource. Injection Configuration Properties
@Value ("${book.name}")
private String bookname;
Injection configuration Properties via ${}, note that it is not the #, this is not the same as other, and in spring 4 you need to use the Property-placeholder tag to register the currently injected configuration before you can use, see.