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

今天,我将我当前正在从事的项目之一迁移到了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类结合使用。 您可能已经知道,我正在自己的配置中使用此功能:

@Configuration
@PropertySource("classpath:/datasource.properties")
public class DefaultDataSourceConfig implements DataSourceConfig {

    @Autowired
    private Environment env;

    @Override
    @Bean
    public 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上查找我发现了一个全新的@PropertySources注释,是一个容器@PropertySource注解。 那就是我针对Java兼容性问题的解决方案:在我的配置类上使用@PropertySources注释,如下所示:

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

    @Autowired
    private 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值