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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

HOW-TO:在Spring 4和Java 7中使用@PropertySource批注

發(fā)布時間:2023/12/3 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HOW-TO:在Spring 4和Java 7中使用@PropertySource批注 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天,我將我當前正在從事的項目之一遷移到了Spring 4.0。 由于它是我用來學習和演示Spring功能的非常簡單的Web應用程序,因此只需要更新項目的POM文件并更改Spring版本。 我將項目部署到Tomcat 7服務器,顯然該應用程序未啟動。 我在IntelliJ控制臺中看到此消息: Failed to load bean class: pl.codeleak.t.config.RootConfig; nested exception is org.springframework.core.NestedIOException: Unable to collect imports; nested exception is java.lang.ClassNotFoundException: java.lang.annotation.Repeatable Failed to load bean class: pl.codeleak.t.config.RootConfig; nested exception is org.springframework.core.NestedIOException: Unable to collect imports; nested exception is java.lang.ClassNotFoundException: java.lang.annotation.Repeatable Failed to load bean class: pl.codeleak.t.config.RootConfig; nested exception is org.springframework.core.NestedIOException: Unable to collect imports; nested exception is java.lang.ClassNotFoundException: java.lang.annotation.Repeatable 。 什么……?

java.lang.annotation.Repeatable注釋是用于標記注釋以供Java 8中多次使用的元注釋(但我在項目中使用Java 7)。 例如:

@Repeatable(Schedules.class) public @interface Schedule { ... }@Schedule(dayOfMonth="last") @Schedule(dayOfWeek="Fri", hour="23") public void doPeriodicCleanup() { ... }

此處對此進行了很好的描述: http : //docs.oracle.com/javase/tutorial/java/annotations/repeating.html 。

Spring 4在其@PropertySource批注中利用了此功能。 提醒您,@ PropertySource批注提供了一種將名稱/值屬性對的源添加到Spring的Environment的機制 ,它與@Configuration類結合使用。 您可能已經(jīng)知道,我正在自己的配置中使用此功能:

@Configuration @PropertySource("classpath:/datasource.properties") public class DefaultDataSourceConfig implements DataSourceConfig {@Autowiredprivate Environment env;@Override@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName(env.getRequiredProperty("dataSource.driverClassName"));dataSource.setUrl(env.getRequiredProperty("dataSource.url"));dataSource.setUsername(env.getRequiredProperty("dataSource.username"));dataSource.setPassword(env.getRequiredProperty("dataSource.password"));return dataSource;} }

我首先想到的是,Spring不再與低于8的Java兼容。 不可能。 雖然這樣做GitHub上查找我發(fā)現(xiàn)了一個全新的@PropertySources注釋,是一個容器@PropertySource注解。 那就是我針對Java兼容性問題的解決方案:在我的配置類上使用@PropertySources注釋,如下所示:

@Configuration @PropertySources(value = {@PropertySource("classpath:/datasource.properties")}) public class DefaultDataSourceConfig implements DataSourceConfig {@Autowiredprivate Environment env;}

就是這樣! 進行此更改后,我的應用程序開始運行,我可以看到它運行正常!

編輯 :請參閱: https : //jira.springsource.org/browse/SPR-11086

參考:操作方法:在Springle 4中,通過我們的JCG合作伙伴 Rafal Borowiec在Javale 7中使用@PropertySource批注在 Codeleak.pl博客上。

翻譯自: https://www.javacodegeeks.com/2013/11/how-to-using-propertysource-annotation-in-spring-4-with-java-7.html

總結

以上是生活随笔為你收集整理的HOW-TO:在Spring 4和Java 7中使用@PropertySource批注的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。