简体   繁体   中英

Spring: How to make class a bean if one constructor-arg is a ref bean but another is not?

I have a class

public class MakeMeBean {
  @Autowired private IAmBean var1;
  private IAmNOTBean var2;

  public MakeMeBean() {}

  public MakeMeBean(IAmNOTBean var) {
    this.var2 = var;
  }
}

I want to make this class as a bean so I make a wireup.xml as

<bean id="make-me-bean" class="com.blah.blah.MakeMeBean">
  <constructor-arg index="0" ref=<PUT REFERENCE BEAN HERE>
  <constructor-arg index="1" <I don't want to put anything>
</bean>

Question
a.) How can I make a bean in which one instance variable is a bean and another not? I don't want to inject var2(another bean in wireup.xml )

b.) <PUT REFERENCE BEAN HERE> is a bean imported from jar file, how can I make reference to this bean in wireup.xml

You can't just have some beans in context that you created and another half that spring created (at least not that simple), if you want to manage the instances over spring, spring should have the objects on its context. Of course you have the possibility to instantiate the objects in the context, and after the instantiation you could invoke some setters to set some properties.

In order to use another to user another bean, that I suppose comes from another Spring context, the other spring context needs to be imported in the first one. In order to import a context file you can use:

<import resource="resourcePath" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM