1, Bean's Scope
Scope describes how to create a Spring container is Bean instance, achieved by @Scope ( "xxxx") notes
singleton: Bean is only one example of a container Spring, Spring is the default configuration, all sharing a container instance
default to comment @Service Bean and other defaults are single cases, if it is a multi-server deployment, load balancing exist, the same Bean exist in each server.
prototype: Each call Bean to create a new instance of
the prototype model, injection or other Bean acquired by getBean, will create an instance of
request: to every http request to create a new Bean instance
session: each http session to create a new Bean instance
2, Spring EL and resources to call
expression language
use can be achieved EL inject resources, resources that is simple to understand initialization
1 import org.apache.commons.io.IOUtils; 2 import org.springframework.beans.factory.annotation.Autowired; 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.ComponentScan; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.PropertySource; 8 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 9 import org.springframework.core.env.Environment; 10 import org.springframework.core.io.Resource; 11 12 @Configuration 13 @ComponentScan("com.ning.ch2.el.*") 14 @PropertySource("classpath:com/ning/ch2/el/el.properties") 15 public class ElConfig { 16 //注入普通字符 17 @Value("this is spring boot") 18 private String normal; 19 20 //注入操作系统属性 21 @Value("#{systemProperties['os.name']}") 22 privateOSNAME String; 23 is 24 // injection operation result of the expression 25 @Value ( "# {T (java.lang.Math) .random ()} 100.0 *" ) 26 is Private Double randomNumber; 27 28 // injection properties of other Bean 29 // class name for an lowercase funService beginning private property name getter methods needed 30 @Value ( "funService.name # {}" ) 31 is private String fromFunServiceName; 32 33 is // injection profile 34 // has been designated file location , or does not require complex configuration PropertySourcesPlaceholderConfigurer @PropertySource annotation of Bean 35 @Value ( "CLASSPATH: COM / Ning / CH2 / EL / el.properties" ) 36 Private the Resource testFile; 37 [ 38 is // injection URL content 39 @Value ( "https://www.baidu.com/" ) 40 Private the Resource testUrl; 41 is 42 is // @Value ( " HTTPS: //www.cnblogs. COM / shipengzhi / Articles / 2099693.html ") 43 is // Private the Resource bolgUrl; 44 is 45 // injection attribute file 46 // need @PropertySource location of the file and the configuration PropertySourcesPlaceholderConfigurer Bean 47 @Value ("} $ {book.name " ) 48 Private String bookName; 49 50 //注入的配置文件可以从Environment获取 51 @Autowired 52 private Environment environment; 53 54 @Bean 55 public static PropertySourcesPlaceholderConfigurer propertyConfigure() { 56 return new PropertySourcesPlaceholderConfigurer(); 57 } 58 59 public void outputResource() { 60 System.out.println("start..."); 61 System.out.println(normal); 62 System.out.println(osName); 63 System.out.println(randomNumber); 64 System.out.println(fromFunServiceName); 65 try { 66 System.out.println(IOUtils.toString(testFile.getInputStream())); 67 System.out.println(IOUtils.toString(testUrl.getInputStream())); 68 //System.out.println(IOUtils.toString(bolgUrl.getInputStream())); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 73 System.out.println(bookName); 74 //需要@PropertySource指定配置文件位置 75 System.out.println(environment.getProperty("book.anthor")); 76 System.out.println("end..."); 77 } 78 }