我正在尝试获取 Spring 活动配置文件JPA
实体监听器使用System.getproperty("spring.profiles.active")
。但它总是返回空配置文件。但是我已经检查了服务器并且配置文件配置正确。
我尝试使用环境获取 Spring 活动配置文件,但在侦听器中,我无法@Autowired
环境也。
@PostUpdate
public void methodInvoked afterUpdate(Example example){
String activeProfile = System.getproperty("spring.profiles.active");
}
请任何指导!
你应该使用Environment https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Environment.htmlbean,同时注入它,如中所述这个答案 https://stackoverflow.com/questions/12155632/injecting-a-spring-dependency-into-a-jpa-entitylistener. SpringBeanAutowiringSupport https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html如果您正在构建 Web 应用程序,则可以使用:
@Autowired
private Environment env;
@PostUpdate
public void methodInvoked afterUpdate(Example example) {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
String[] activeProfile = env.getActiveProfiles();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)