我想在应用程序启动期间(或者在结束时)执行一些代码。我使用@PostConstruct批注,@EventListener(ContextRefreshedEvent.class),实现了InitializingBean,实现了ApplicationListener,我发现了一些资源。它们都在启动时执行我的代码,但是应用程序属性的占位符不会被替换为时刻。所以如果我的类有一个具有@Value(“$ {my.property}”)注解的成员,它将返回“$ {my.property}”,而不是在yaml(或任何地方)中定义的实际值。 如何在更换发生后完成我的代码?在Spring启动时执行方法
0
A
回答
0
您可以实施InitializingBean
,该方法的名称为afterPropertiesSet()
。此方法将在所有属性占位符被替换后调用。
0
@PostConstruct在bean创建时被调用。 Ypu必须检查弹簧是否找到带有属性的文件。
+0
使用评论或编辑 –
0
如果你有一个配置类,@Configuration,那么你可以尝试明确导入您的属性文件中加入以下注释:你的配置类和后
@PropertySource("classpath:your-properties-file.properties")
任何其他非配置资源应加载@Value注释应该可以正常工作。
0
你应该实现ApplicationListener<ContextRefreshedEvent>
这样的:
@Component
public class SpringContextListener implements ApplicationListener<ContextRefreshedEvent> {
@Value("${my.property}")
private String someVal;
/**
* // This logic will be executed after the application has loded
*/
public void onApplicationEvent(ContextRefreshedEvent event) {
// Some logic here
}
}
相关问题
- 1. 在启动时执行Selenium
- 2. 执行PS在启动时
- 3. 使用Celery在启动时执行方法
- 4. 如何让Grails在启动时执行控制器方法?
- 5. 在屏幕启动时运行方法
- 6. 如何在Spring中启动时只执行一次SQL脚本?
- 7. 在Spring启动时运行服务器时在启动模式下启动GWT
- 8. 让GDB自动执行在启动时
- 9. 启动时执行操作
- 10. 启动时执行控制器方法(Grails)
- 11. Spring @Scheduled执行方法在VPS中执行三次
- 12. Spring事务 - 在新事务中记录方法执行时间
- 13. 在CDI启动时执行代码
- 14. 在Tomcat启动时执行JSP
- 15. 在viewcontroller的启动时执行segue
- 16. 在启动时执行m文件
- 17. Django - 在启动时执行代码
- 18. 在xtext启动时执行代码?
- 19. VSIX:在VS启动时执行代码
- 20. 在Emacs启动时执行命令(emacs.el)
- 21. liquibase.bat在执行时未启动
- 22. 仅在Spring应用程序上下文启动时运行一个方法?
- 23. 有没有办法在spring启动时运行madvoc?
- 24. 启动应用程序弹簧启动后在类中执行某些方法
- 25. @Autowired后执行Spring @Bean方法
- 26. Spring AOP - 无法执行方面
- 27. Spring 3.1 @Cacheable - 仍然执行的方法
- 28. 无法在启动时在Raspberry Pi上执行Java程序
- 29. 无法在执行CMD脚本时在Dockerfile中启动Mysql
- 30. 在网络端启动应用程序的执行方法
春天配置将解决放在'@ Value'内的财产的占位符。如果未加载,则表示不正确的属性名称,或者特定的yaml未加载到上下文中。当然你可以使用'@ PostConstuct'来设置会员 – Barath