英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论