如何使用 Spring 中的环境创建 <bean> 标签 5
How to create a <bean> tag with the Environment in Spring 5
在 Spring 中,您可以使用注释执行以下操作:
@Configuration
@PropertySource(value = "classpath:props.properties")
public class MyConfiguration {
@Autowired
Environment env;
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean;
myBean.setEnv(env);
}
}
是否可以从XML注入环境?
我想要类似于:
<context:property-placeholder location="classpath:props.properties"/>
<bean id="env" class="org.springframework.core.env.Environment"/>
<bean id="myBean" class="MyBean" p:env-ref="env"/>
但我不知道我是否可以从 XML 中声明的 myBean bean 中的 env bean 引用获取属性。
最后,我发现我可以在 MyBean 中实现 EnvironmentAware 以在使用 XML 配置时获取环境。
在 Spring 中,您可以使用注释执行以下操作:
@Configuration
@PropertySource(value = "classpath:props.properties")
public class MyConfiguration {
@Autowired
Environment env;
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean;
myBean.setEnv(env);
}
}
是否可以从XML注入环境? 我想要类似于:
<context:property-placeholder location="classpath:props.properties"/>
<bean id="env" class="org.springframework.core.env.Environment"/>
<bean id="myBean" class="MyBean" p:env-ref="env"/>
但我不知道我是否可以从 XML 中声明的 myBean bean 中的 env bean 引用获取属性。
最后,我发现我可以在 MyBean 中实现 EnvironmentAware 以在使用 XML 配置时获取环境。