3、Spring EL和Bean的Scope

1、Bean的Scope
  Scope描述Spring容器是如何新建Bean实例的,通过@Scope("xxxx")注解实现
  singleton:一个Spring容器只有一个Bean实例,为Spring的默认配置,全容器共享一个实例
    默认注解为@Service等Bean默认都为单例的,如果是多服务器部署,存在负载均衡,同一个Bean会存在于每台服务器。
  prototype:每次调用新建一个Bean实例
    为原型模式,注入其他Bean或通过getBean获取,都会创建一个实例
  request:给每一个http request新建一个Bean实例
  session:给每一个http session新建一个Bean实例
2、Spring EL和资源调用
  表达式语言
  使用EL可以实现资源的注入,简单理解即为资源的初始化

 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     private String osName;
23     
24     //注入表达式运算结果
25     @Value("#{ T(java.lang.Math).random() * 100.0}")
26     private double randomNumber;
27     
28     //注入其他Bean的属性
29     //类名funService需小写开头,私有属性name需要有getter方法
30     @Value("#{funService.name}")
31     private String fromFunServiceName;
32     
33     //注入配置文件
34     //已经指定了文件地址,不需要配合@PropertySource注解或配置PropertySourcesPlaceholderConfigurer的Bean
35     @Value("classpath:com/ning/ch2/el/el.properties")
36     private Resource testFile;
37     
38     //注入网址内容
39     @Value("https://www.baidu.com/")
40     private Resource testUrl;
41     
42     //@Value("https://www.cnblogs.com/shipengzhi/articles/2099693.html")
43     //private Resource bolgUrl;
44     
45     //注入属性文件
46     //需要@PropertySource指定文件位置且配置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 }

猜你喜欢

转载自www.cnblogs.com/6xiong/p/12005574.html