有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在子注释上下文中使用spring属性源?

我正在尝试使用带注释的配置类复制与XML配置一起工作的东西。我遇到的问题是,在子上下文中定义的属性源是不可访问的

工作的xml看起来像

父上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="proxyChannelQueue" class="java.util.concurrent.ArrayBlockingQueue">
        <constructor-arg value="10"/>
    </bean>

</beans>

子上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="proxy-host.properties"/>

    <bean class="org.eclipse.jetty.server.Server"
          p:handler-ref="proxyHostHandler"
            init-method="start">
        <constructor-arg value="${proxyHostPort}"/>
    </bean>

    <bean id="proxyHostHandler" class="com.sjl.web.ProxyHostHandler"
            p:proxyChannelQueue-ref="proxyChannelQueue"/>
</beans>

启动代码:

    ClassPathXmlApplicationContext parentContext = new ClassPathXmlApplicationContext("test/parent-context.xml");
    ClassPathXmlApplicationContext childContext = new ClassPathXmlApplicationContext(new String[] {"test/child-context.xml"}, parentContext);

我尝试使用配置类来实现这一点,结果如下所示

父上下文:

@Configuration
public class ParentConfiguration {
    @Bean(name = "proxyChannelQueue")
    public BlockingQueue<ProxyChannel> getProxyChannelQueue() {
        return new ArrayBlockingQueue<ProxyChannel>(10);
    }
}

子上下文:

@Configuration
@PropertySource("classpath:proxy-host.properties")
public class ChildContext {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChildContext.class);
    @Autowired
    private Environment environment;

    @Resource(name = "proxyChannelQueue")
    private BlockingQueue<ProxyChannel> proxyChannelQueue;

    public static void main(String[] args) {
        new HierarchicalAnnotationConfigApplicationContext(ChildContext.class);
    }

    @Bean
    public Server getJettyServer() throws Exception {
        int proxyHostPort = environment.getProperty("proxyHostPort", Integer.class);
        Server server = new Server(proxyHostPort);
        server.setHandler(getHandler());
        server.start();

        return server;
    }

    @Bean
    public Handler getHandler() {
        ProxyHostHandler proxyHostHandler = new ProxyHostHandler();
        proxyHostHandler.setProxyChannelQueue(proxyChannelQueue);
        return proxyHostHandler;
    }
}

启动代码:

AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext(ParentConfiguration.class);
AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext();
childContext.setParent(parentContext);
childContext.register(ChildContext.class);
childContext.refresh();

尝试在ChildContext getJettyServer方法中检索proxyHostPort时,我得到一个空指针。检查环境变量表明它只包含2个属性源(systemProperties和systemEnvironment),而不是我期望的3个

如果我将它们作为单个组合上下文运行,则相同的配置也会起作用。例如:

AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext(ParentConfiguration.class, ChildContext.class);

然而,我想要使用父上下文提供的隔离

干杯, 彼得


共 (0) 个答案