开发者

How do I use an bean exported from a directory below?

Think for example that I have a Directory A that holds A.spring and Directory B. Inside Directory B is B.spring that defines a bean say "WANTED_BEAN". Also in Directory B is a main.spring that exports "WANTED_BEAN". How do I reuse this exported bean in A.spring?

The hierarchy below explains it much better.

  • Directory A
    • A.spring (want to use bean here开发者_如何转开发)
    • Directory B
      • main.spring (bean exported here)
      • B.spring (bean defined here)


While you're talking about directories I expect what you're saying is that you have a few different spring context definition XML files located in those directories.

Spring has a concept of parent/child ApplicationContexts but merely locating the context configuration in a directory structure does not imply that Spring will look at it that way.

You don't say how you're constructing your ApplicationContext so it's bit difficult to make comment, but:

Composing XML-based configuration metadata explains how to import one XML configuration file from another.

If, on the other hand, you're creating an ApplicationContext within your application with paths supplied when you configure it, you should look at the reference for Instantiating a container and probably also ClassPathXmlApplicationContext documentation.

You're should be either doing

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"A/A.spring", "B/B.spring", "B/main.spring"});

or

<beans>
    <import resource="A/A.spring"/>
    <import resource="B/B.spring"/>
    <import resource="B/main.spring"/>
</beans>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜