代码之家  ›  专栏  ›  技术社区  ›  Aliuk

如何在Spring5中使用环境创建<bean>标记

  •  0
  • Aliuk  · 技术社区  · 7 年前

    在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"/>
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Aliuk    7 年前

    最后,我发现我可以在MyBean中实现EnvironmentAware,以便在使用XML配置时获得环境。