2016-08-19 124 views
14

我有一个Spring Boot应用程序,并且在其中一个类中,我尝试使用@Value从application.properties文件引用属性。但是,该属性没有得到解决。我看过类似的帖子,并尝试遵循这些建议,但这并没有帮助。该类是:Spring Boot @Value Properties

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class PrintProperty { 

    @Value("${file.directory}") 
    private String fileDirectory; 

    public void print() { 
    System.out.println(fileDirectory); 
    } 
} 

我有application.properties属性file.directory。我还有其他领域。

+0

向我们显示您收到的错误,请检查我的答案,它应该工作。 – Jesse

回答

0

由于您使用的是Spring引导,我认为它目前不起作用的唯一方法是因为您尚未添加@PropertySource注释。

请试试看,并让我知道您的反馈。谢谢!

@Configuration 
@ComponentScan 
@PropertySource("classpath:/application.properties") 
@EnableAutoConfiguration 
public class PrintProperty { 

    @Value("${file.directory}") 
    private String fileDirectory; 

    public void print() { 
    System.out.println(fileDirectory); 
    } 
} 
+2

因为这是弹簧启动,所以还有其他的方法。用@SpringBootApplication注解主类足够了 – rajadilipkolli

+0

@ user6641655你解决了这个问题吗?让我们知道您对此的反馈? – kukkuz

+0

我尝试了两种方式,但没有奏效。 – user6641655

1

来读取application.properties我们只是需要标注我们的主要类@SpringBootApplication的价值观和你在哪里使用了@Component或多种它读取类。以下是我已阅读application.properties中的值的示例,并且在调用Web服务时它工作正常。如果您按原样部署相同的代码并尝试访问http://localhost:8080/hello,则您将获得存储在关键消息的application.properties中的值。

package com.example; 

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

@SpringBootApplication 
@RestController 
public class DemoApplication { 

    @Value("${message}") 
    private String message; 

    public static void main(String[] args) { 
     SpringApplication.run(DemoApplication.class, args); 
    } 

    @RequestMapping("/hello") 
    String home() { 
     return message; 
    } 

} 

尝试,让我知道

+0

我试过了,但没有奏效。这些属性在我的主类中使用@SpringBootApplication注释解决,但不在此类中。 – user6641655

+0

尝试在您的课堂中添加@Component注释,并让我知道发生了什么 – rajadilipkolli

+0

我试过了@Component,但没有奏效。 – user6641655

2

确保您的application.properties文件是在src /主/资源/ application.properties。有一条路要走。然后加入@PostConstruct如下

@ComponentScan 
public class PrintProperty { 

    @Value("${file.directory}") 
    private String fileDirectory; 

    @PostConstruct 
    public void print() { 
    System.out.println(fileDirectory); 
    } 
} 
0

尝试修改以下 -

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class PrintProperty { 

    @Autowired 
    Environment environment; 

    public void print() { 
    System.out.println(environment.getProperty("file.directory")); 
    } 
} 

你可以摆脱@ComponentScan@EnableAutoConfiguration如果这些已经存在于你SpringbootApplication类。

至于为什么@Value不起作用,不确定!

7

我有和你一样的问题。这是我的错误代码。

@Component 
public class GetExprsAndEnvId { 
    @Value("hello") 
    private String Mysecret; 


    public GetExprsAndEnvId() { 
     System.out.println("construct"); 
    } 


    public void print(){ 
     System.out.println(this.Mysecret); 
    } 


    public String getMysecret() { 
     return Mysecret; 
    } 

    public void setMysecret(String mysecret) { 
     Mysecret = mysecret; 
    } 
} 

这是这样的没有问题,但 我们需要使用这样的:

@Autowired 
private GetExprsAndEnvId getExprsAndEnvId; 

不喜欢这样的:

getExprsAndEnvId = new GetExprsAndEnvId(); 
+0

谢谢!我遇到了这个问题,这解决了这个问题。奇怪的情况是Spring检查属性是否存在(当它没有在属性文件中定义时它会抛出一个错误),但它没有用它来填充变量。现在确实如此。 – mcv

+0

解决了我的问题,任何想法为什么发生这种情况? – gaurav5430

3

我倒是想提一提,那我使用的是1.4.0版本的弹簧启动版本,自此版本以来只能编写:

@Component 
public class MongoConnection { 

@Value("${spring.data.mongodb.host}") 
private String mongoHost; 

@Value("${spring.data.mongodb.port}") 
private int mongoPort; 

@Value("${spring.data.mongodb.database}") 
private String mongoDB; 
} 

然后在需要时注入类。

+0

为什么不直接在你的答案中写代码? – Cyril

0

您尚未在OP中包含包裹声明,但可能您的@SpringBootApplication@ComponentScan都不在扫描您的@Component

@ComponentScan的Javadoc状态:

要么basePackageClassesbasePackages(或其别名value)可以指定以定义特定的包进行扫描 。如果特定软件包 未定义,则将从声明此批注的类 的软件包中进行扫描。

ISTR在这之前浪费了很多时间,发现最简单的方法就是将我的应用程序类简单地移动到我应用程序包树中的最高包。

最近我遇到了一个问题,就是属性在值插入完成之前被读取。杰西的回答很有帮助,因为@PostConstruct似乎是最早可以读取插入值的值,当然你应该让Spring调用它。