色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

詳解Spring中的@PropertySource注解使用

瀏覽:2日期:2023-12-13 15:32:02

@PropertySource注解是Spring用于加載配置文件,默認支持.properties與.xml兩種配置文件。@PropertySource屬性如下:

name:默認為空,不指定Spring自動生成 value:配置文件 ignoreResourceNotFound:沒有找到配置文件是否忽略,默認false,4.0版本加入 encoding:配置文件編碼格式,默認UTF-8 4.3版本才加入 factory:配置文件解析工廠,默認:PropertySourceFactory.class 4.3版本才加入,如果是之前的版本就需要手動注入配置文件解析Bean

接下來就使用@PropertySource來加載.properties與.xml配置文件。這里模擬連接MySQL數據庫。首先添加依賴:

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.6.RELEASE</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.6.RELEASE</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version></dependency>

準備屬性配置文件jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306jdbc.userName=rootjdbc.password=xiaohu

創建屬性實體類來加載配置文件JdbcProperties

@Data@Repository@PropertySource(value = 'classpath:jdbc.properties')public class JdbcProperties { @Value('${jdbc.driver}') private String driver; @Value('${jdbc.url}') private String url; @Value('${jdbc.userName}') private String userName; @Value('${jdbc.password}') private String password;}

創建JDBC配置類JdbcConfig

@Componentpublic class JdbcConfig { @Bean public DataSource dataSource(JdbcProperties jdbcProperties){System.out.println('打印獲取到的配置信息:'+jdbcProperties);DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName(jdbcProperties.getDriver());dataSource.setUrl(jdbcProperties.getUrl());dataSource.setUsername(jdbcProperties.getUserName());dataSource.setPassword(jdbcProperties.getPassword());return dataSource; }}

創建Spring配置類SpringConfiguration

@Configurationpublic class SpringConfiguration {}

創建測試類測試讀取配置文件

public class PropertySourceTest { public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext('config');DataSource dataSource = context.getBean('dataSource',DataSource.class);System.out.println(dataSource); }}

查看輸出結果:

打印獲取到的配置信息:JdbcProperties(driver=com.mysql.cj.jdbc.Driver, url=jdbc:mysql://127.0.0.1:3306, userName=root, password=xiaohu)org.springframework.jdbc.datasource.DriverManagerDataSource@58695725

從結果可以看出,我們的properties中的配置已經成功讀取到,并且DataSource也從Spring容器中獲取到。上面介紹注解的屬性時,factory是4.3版本才加入的,那么如果4.3版本之前要解析配置文件又應該怎么處理呢?,這個時候就需要手動將解析配置文件的Bean注入到Spring容器中了,用法很簡單,在SpringConfiguration類中添加如下代碼即可:

@Beanpublic PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){ return new PropertySourcesPlaceholderConfigurer();}

具體測試結果,就自行測試了。上面例子介紹了properties的使用,下面我們將配置文件換成xml文件。配置如下:

<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'><properties> <entry key='jdbc.driver'>com.mysql.cj.jdbc.Driver</entry> <entry key='jdbc.url'>jdbc:mysql://127.0.0.1:3306/test</entry> <entry key='jdbc.userName'>root</entry> <entry key='jdbc.password'>xiaohu</entry></properties>

然后將JdbcProperties類上的注解的配置文件換成xml文件。

@PropertySource(value = 'classpath:jdbc.properties')

其他不用調整,執行測試類,輸出的結果一樣。因為上面介紹到@PropertySource默認支持properties與xml的配置文件。我們可以查看PropertySourceFactory的默認實現DefaultPropertySourceFactory源碼

public class DefaultPropertySourceFactory implements PropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));}}

然后進入ResourcePropertySource類,源碼這里使用了一個三元運算符,如果name為空,就使用默認Spring默認生成的name。

public ResourcePropertySource(String name, EncodedResource resource) throws IOException {super(name, PropertiesLoaderUtils.loadProperties(resource));this.resourceName = getNameForResource(resource.getResource());}public ResourcePropertySource(EncodedResource resource) throws IOException {super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource));this.resourceName = null;}

這里可以看到調用了PropertiesLoaderUtils.loadProperties方法,進入到源碼

public static Properties loadProperties(EncodedResource resource) throws IOException {Properties props = new Properties();fillProperties(props, resource);return props;}

會調用fillProperties的方法,一直跟到調用最低的fillProperties方法。

static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)throws IOException {InputStream stream = null;Reader reader = null;try {String filename = resource.getResource().getFilename();if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {stream = resource.getInputStream();persister.loadFromXml(props, stream);}else if (resource.requiresReader()) {reader = resource.getReader();persister.load(props, reader);}else {stream = resource.getInputStream();persister.load(props, stream);}}finally {if (stream != null) {stream.close();}if (reader != null) {reader.close();}}}

第一個if判斷文件后綴是否是xml結尾,常量XML_FILE_EXTENSION如下:

private static final String XML_FILE_EXTENSION = '.xml';

除了支持properties與xml的配置文件方式,也支持yml配置文件的方式,不過需要自定義解析工廠,下面來實現怎么解析yml配置文件。引入可以解析yml文件的第三方庫

<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.28</version></dependency>

創建yml解析工廠YamlPropertySourceFactory實現PropertySourceFactory

public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();factoryBean.setResources(resource.getResource());Properties properties = factoryBean.getObject();return name != null ? new PropertiesPropertySource(name, properties) : new PropertiesPropertySource(resource.getResource().getFilename(), properties); }}

然后將JdbcProperties類的@PropertySource換成如下寫法:

@PropertySource(value = 'classpath:jdbc.yml',factory = YamlPropertySourceFactory.class)

執行測試類,輸出結果與上面結果一樣

打印獲取到的配置信息:JdbcProperties(driver=com.mysql.cj.jdbc.Driver, url=jdbc:mysql://127.0.0.1:3306, userName=root, password=xiaohu)org.springframework.jdbc.datasource.DriverManagerDataSource@58695725

證明我們自定義的解析yml配置文件就成功了。

到此這篇關于Spring的@PropertySource注解使用的文章就介紹到這了,更多相關Spring的@PropertySource注解使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 日本国产欧美色综合 | 一区二区三区欧美日韩国产 | 国产在线观看网址在线视频 | 久久免费视频1 | 曰本毛片va看到爽不卡 | 福利网址在线 | 日本高清色本在线www游戏 | 五月激情丁香婷婷综合第九 | 精品久久久久久中文字幕 | 免费的三级毛片 | 国产成人精品福利网站在线 | 欧美一级片免费看 | 日韩乱淫 | 99久久亚洲 | 精品丝袜国产自在线拍亚洲 | 免费一级毛片私人影院a行 免费一级毛片无毒不卡 | 香蕉超级碰碰碰97视频在线观看 | 成熟女人免费一级毛片 | 国产在线精品一区二区 | 丝袜美腿精品一区二区三 | 美国aaaa一级毛片啊 | 成年人免费网站在线观看 | 亚洲欧美激情视频 | 精品欧美一区二区在线观看欧美熟 | 欧美日韩亚洲成色二本道三区 | 久久99热只有视精品6国产 | 极品精品国产超清自在线观看 | 亚洲欧洲国产精品 | 老司机午夜在线视频免费观 | 国产精品中文 | 欧美片能看的一级毛片 | 经典国产乱子伦精品视频 | 日本一级特黄a大片 | 国产99久久精品 | 国内成人自拍视频 | 国产不卡精品一区二区三区 | 久久视频在线播放视频99re6 | 亚洲久久久久久久 | 国产精品私人玩物在线观看 | 亚洲欧美男人天堂 | 91年精品国产福利线观看久久 |