Spring EL表達式
Spring EL-Spring表達式語言,支持在xml和注解中使用表達式,類似於在jsp的EL表達式語言。
Spring 開發中經常涉及調用各種資源的情況,包含普通文件、網址、配置文件、系統環境變量等,我們可以使用Spring的表達式語言實現資源的注入。
Spring主要在注解@Value的參數中使用表達式語言。
- 注入普通字符
- 注入操作系統屬性
- 注入表達式運算結果
- 注入其他Bean的屬性
- 注入文件內容
- 注入網址內容
- 注入屬性文件
示例
- 在類路徑下新建一個text.txt文件,內容隨意,在類路徑下新建一個test.propertes文件,內容如下:
book.author=xxx
book.name= spring boot
- 編寫一個需要注入的Bean
@Service
public class DemoService {
@Value("其他類中的屬性") // 此處為注入普通字符串
private String another;
public String getAnother(){
return another;
}
public void setAnother(String anther){
this.another = anther;
}
}
- 配置類
@Configuration
@ComponentScan("xxx.xx.xx")
@PropertySource("classpath:test.properties")
public class ELConfig {
@Value("I Love You !") // 1
private String normal;
@Value("#{systemProperties['os.name']}") // 2
private String osName;
@Value("#{T(java.lang.Math).random() * 100.0}") // 3
private double randomNumber;
@Value("#{demoService.another}") // 4
private String fromAnother;
@Value("classpath:test.txt") // 5
private Resource testFile;
@Value("http://www.baidu.com") // 6
private Resource testUrl;
@Value("${book.name}")
private String bookName;
@Bean // 7
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
}
...
setter and getter function
...
}
代碼解釋
-
注入普通字符串
-
注入操作系統屬性
-
注入表達式結果
-
注入其他Bean屬性
-
注入文件資源,文件資源可以是classpath路徑下的也可以是系統文件目錄下的
-
注入網址資源
-
注入配置文件中的資源,配置文件使用@PropertySource注解在類上標注
注入配置件需要使用@PropertySource指定文件地址,若使用@Value注入,則要配置一個PropertySourcesPlaceholderConfigurer的Bean,@Value("${book.name}")使用的是"$"而不是"#"
Profile
Profile為不同環境下使用不同的配置提供了支持(開發環境和生成環境)
- 通過設定Environment的ActiveProfiles類設定當前context需要使用的配置。在開發中使用@Profile注解類或者方法,達到不同情況下選擇實例化不同的Bean。
- 通過設定JVM的spring.profiles.active參數來設計配置環境。
- Web項目設置在Servlet的context parameter中。
<-- Servlet 2.5及以下 -->
<serlvet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</init-param>
</servlet>
// servlet 3.0 以上
public class WebInit implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
container.setInitParameter("spring.profiles.default", "dev");
}
}
示例
- 示例Bean
public class DemoBean {
private String content;
public DemoBean(String content){
this.content = content;
}
public String getContent(){
return content;
}
}
- Profile配置
@Configuration
public class ProfileConfig {
@Bean
@Profile("dev") // 1
public DemoBean devDemoBean(){
return new DemoBean("from development profile");
}
@Bean
@Profile("prod") // 2
public DemoBean prodDemoBean() {
return new DemoBean("from production profile");
}
}
代碼解釋
-
profile為dev時示例化devDemoBean
-
profile為prod時實例化為prodDemoBean
-
運行
public class Main{
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod"); // 1
context.register(ProfileConfig.class); // 2
context.refresh(); // 3
DemoBean demoBean = context.getBean(DemoBean.class);
System.out.println(demoBean.getContext());
context.close();
}
}
說明
- 先將活動的Profile設置為prod
- 后置注冊Bean配置類,不然會報Bean未定義的錯誤
- 刷新容器