Giriş
Bean'e kurucu parametre vermek için constructor-arg tag'i kullanılır. Şu tip nesneler geçilebilir.
Şöyle yaparız.
Bean'e kurucu parametre vermek için constructor-arg tag'i kullanılır. Şu tip nesneler geçilebilir.
List – <list/>
Set – <set/>
Map – <map/>
Properties – <props/>Şöyle yaparız.
<bean id="X" class="A.X">
  <constructor-arg value="Foo"/>
</bean>Elimizde şöyle bir sınıf olsun
public class PCCollection implements MarketPC {
  private Collection<PersonalComputer> pcCollection;
  public PCCollection(Collection<PersonalComputer> pcCollection) {
    this.pcCollection = pcCollection;
  }
}
Şöyle yaparız.
<bean id="marketPC" class="PCCollection">
  <constructor-arg name="pcCollection">
    <list>
      <ref bean="customPC"/>
      <bean class="MyCustomPC" scope="prototype">
        <constructor-arg name="mother" ref="msiMother"/>
        <constructor-arg name="processor" ref="asusProcessor"/>
        <constructor-arg name="storage" ref="gigabyteStorage"/>
        <constructor-arg name="ram" ref="gigabyteRam"/>
        <constructor-arg name="video" ref="asusVideo"/>
      </bean>
    </list>
  </constructor-arg>
</bean>Elimizde şöyle bir kod olsun.
public class MyClass {
  public MyClass(Map<String, String> componentName)
}<bean id="MyBean" class="org.MyClass">
  <constructor-arg name="componentName">
    <util:map>
      <entry key="T1" value-ref="A"/>
      <entry key="T2" value-ref="B"/>
      <entry key="T3" value-ref="C"/>
    </util:map>
</bean>Elimizde şöyle bir sınıf olsun
public class PCCollection implements MarketPC {
  private Collection<PersonalComputer> pcCollection;
  public PCCollection(Collection<PersonalComputer> pcCollection) {
    this.pcCollection = pcCollection;
  }
}public class MyCustomPC implements PersonalComputer {
  private MotherBoard mother;
  private Processor processor;
  private Ram ram;
  private StorageDevice storage;
  private VideoCard video;
}<bean id="marketPCASUS" class="PCCollection">
    <constructor-arg name="pcCollection"
    value = "#{marketPC.pcCollection.?[video eq 'asusVideo']}">
    </constructor-arg>
</bean>Elimizde hem value hem de bir başka bean alan bir sınıf olsun.
public Location(Address address, String pincode) {
...
}<bean id="location" class="com.ibm.spring.Location">
  <constructor-arg name="pincode" value="110976"></constructor-arg>
  <constructor-arg ref="address"></constructor-arg>
</bean>
 
Hiç yorum yok:
Yorum Gönder