在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"/>
最后,我发现我可以在MyBean中实现EnvironmentAware,以便在使用XML配置时获得环境。