从application.properties文件中读取属性在Java中不起作用。

huangapple go评论112阅读模式
英文:

Reading property from application.properties not working in java

问题

我正在尝试在@Embeddable pojo类中运行这个**@Value**,但该值始终为false。所有application.properties文件都已正确填写。可能是因为它不是服务/控制器/配置而无法实现吗?

@Embeddable
public class WebDestination implements Serializable {
    
    @Value("${property.example}")
    private boolean example;

    public boolean isExample() {
        return example;
    }

    public void setExample(boolean example) {
        this.example = example;
    }
}
英文:

I am trying to get this @Value running in an @Embeddable pojo class, but the value ist always false. All application.properties files are filled correctly. Is it maybe just not possible, because it's no srvice/ controller/ configuration?

@Embeddable
  public class WebDestination implements Serializable {
    
       @Value("${property.example}")
       private boolean example;


	   public boolean isExample() {
	    	return example;
	   }

	   public void setExample(boolean example) {
		   this.example= example;
	   }
}

答案1

得分: 6

你只能在由Spring容器管理的组件中(使用@Service@Controller@Component@Configuration等注解的类)使用@Value("${property.example}")来注入属性值,不能在其他任何地方使用它。

英文:

You can only inject property value using @Value("${property.example}") in components that are managed by Spring container (classes that are annotated with @Service, @Controller, @Component, @Configuration and ...), you can't use it anywhere else.

huangapple
  • 本文由 发表于 2020年7月28日 17:26:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63131013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定