日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring4中的@Value的使用(学习笔记)

發布時間:2024/9/27 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring4中的@Value的使用(学习笔记) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、@Value用途介紹

Spring主要在注解@Value的參數中使用表達式
@Value的使用主要有以下幾種情況
(1)注入普通字符
(2)注入操作系統屬性
(3)注入表達式運算結果
(4)注入其他Bean的屬性
(5)注入文件內容
(6)注入網址內容
(7)注入屬性文件

2、案例編寫之項目目錄結構

3、pom.xml文件內容

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.wisely</groupId><artifactId>highlight_spring4</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.8</java.version><spring-framework.version>4.1.5.RELEASE</spring-framework.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-framework.version}</version></dependency><!-- spring aop支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring-framework.version}</version></dependency><!-- aspectj支持 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.6</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.5</version></dependency><!-- Spring test 支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring-framework.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.3</version></dependency><dependency><groupId>javax.annotation</groupId><artifactId>jsr250-api</artifactId><version>1.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring-framework.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build></project>

4、test.txt的內容

測試文件

5、test.properties的內容

book.author=wangyunfei book.name=spring boot

6、DemoService的內容

package com.wisely.highlight_spring4.ch2.el;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;@Service public class DemoService {@Value("其它類的屬性")private String another;public String getAnother() {return another;}public void setAnother(String another) {this.another = another;} }

7、ElConfig的內容

package com.wisely.highlight_spring4.ch2.el;import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.env.Environment; import org.springframework.core.io.Resource;@Configuration @ComponentScan("com.wisely.highlight_spring4.ch2.el") @PropertySource("classpath:test.properties") public class ElConfig {@Value("I Love You!") //1private String normal;@Value("#{systemProperties['os.name']}") //2private String osName;@Value("#{ T(java.lang.Math).random() * 100.0 }") //3private double randomNumber;@Value("#{demoService.another}") //4private String fromAnother;@Value("classpath:test.txt") //5private Resource testFile;@Value("http://www.baidu.com") //6private Resource testUrl;@Value("${book.name}") //7private String bookName;@Autowiredprivate Environment environment; //7@Bean //7public static PropertySourcesPlaceholderConfigurer propertyConfigure() {return new PropertySourcesPlaceholderConfigurer();}public void outputResource() {try {System.out.println(normal);System.out.println(osName);System.out.println(randomNumber);System.out.println(fromAnother);System.out.println(IOUtils.toString(testFile.getInputStream()));System.out.println(IOUtils.toString(testUrl.getInputStream()));System.out.println(bookName);System.out.println(environment.getProperty("book.author"));} catch (Exception e) {e.printStackTrace();}} }

8、Main的內容

package com.wisely.highlight_spring4.ch2.el;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args) {AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext(ElConfig.class);ElConfig resourceService = context.getBean(ElConfig.class);resourceService.outputResource();context.close();} }

9、最終運行的結果

總結

以上是生活随笔為你收集整理的Spring4中的@Value的使用(学习笔记)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。