@PropertySource("classpath:/com/etoak/importresource/jdbc.properties")无效

来源:4-6 Spring Bean装配之基于Java的容器注解说明——@ImportResource和@Value

慕村8951117

2018-01-19 17:02

gtrfans 可以不使用@ImportResource("classpath:config.xml"),也不必配置xml文件,直接用@PropertySource("classpath:config.properties")就可以获取配置文件的数据。 spring4以上 我用的4.17 为什么 引入无效 输出 uri==>${url} user==>${user} name==>${pass}

写回答 关注

5回答

  • Bruth_Lee
    2018-08-07 11:09:02

    https://mp.csdn.net/postedit

    Bruth_...

    这篇博客写到了的哈,主要是注解使用错误

    2018-08-07 11:09:35

    共 1 条回复 >

  • Tari丶
    2018-05-16 17:01:12

    参考:https://my.oschina.net/u/2326864/blog/1786955

    1。@PropertySource+@Value

    要想使用@Value 用${}占位符注入属性,这个bean是必须的(PropertySourcesPlaceholderConfigurer),    //这个就是占位bean 

    如果不用的话,

    Value("${url}")

    private String url;

    @Value("value") 里面的value就是String url的值

    加上这段代码,会识别${}占位符:

    @Bean

        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

           return new PropertySourcesPlaceholderConfigurer();

        }

    2.@PropertySource+@Value+@Envirment

     另一种方式是不用value直接用Envirment变量直接getProperty('key')  

    @Autowired

    private Envirment envirment;

    使用envirment.getProperty('key') ;就可以取到属性 


  • 我也很绝望_但只能继续学啊
    2018-02-02 13:26:16

    我也遇到了这样的问题,暂时不知道为什么,但是用@PropertySource + Environment 却可以正确读到配置http://img.mukewang.com/5a73f674000191e311550174.jpg

  • 慕村8951117
    2018-01-20 11:06:15

    我就是这样的 没问题啊 可是 @Value("${url}")  取不到数据  他去的是${url} 这玩意  跟没识别一样

  • annnn
    2018-01-19 19:15:49

    classpath:一般是在src下面,比如我的src/com/cl/bean/db.properties

    我用注解的时候就会classpath:com/cl/bean/db.properties

    慕村8951...

    我就是这样的 没问题啊 可是 @Value("${url}") 取不到数据 他去的是${url} 这玩意 跟没识别一样 你给下你的代码 配置吗 谢谢

    2018-01-20 11:06:52

    共 1 条回复 >

Spring入门篇

为您带来IOC和AOP的基本概念及用法,为后续高级课程学习打下基础

268800 学习 · 1026 问题

查看课程

相似问题