1

私は以下を含むクラスAを持っています

class A
{

private HashSet<Long> at = new HashSet<Long>();

and it has a constructor like this

A()
{
//set is being initialsised here 

                this.at.add(new Long(44));
        this.at.add(new Long(34));
        this.at.add(new Long(54));
        this.at.add(new Long(55));


}

以下は、そのために定義されたSpring xml Beanです...

<bean id="aa" class="com.abc.A">
        <property name="readPermissionGroup">
      <set>
         <value>03</value>
         <value>13</value>
         <value>54</value>
         <value>55</value>
      </set>
   </property>
    </bean>

Bean bbにはクラスAの完全な定義が含まれているため、上記のBean aaをbbに追加する方法を教えてください

4

2 に答える 2

1

クラス B を Spring コンテキストに定義する

<bean id="bb" class="com.abc.B"></bean>

次のように、クラス B に A クラス参照を追加します。

class B
{
    private A beanA;

    //setters getters
}

xml構成でBean AをBean Bに注入します。Bean B の定義を変更する

<bean id="bb" class="com.abc.B">
<property name="beanA" ref="aa" />
</bean>

私の提案は、Bean の定義と注入に xml を使用しないことです。本当に古いので、代わりに注釈を使用してください。

于 2013-09-12T06:28:03.460 に答える
0

@Annotation を使用して bean A をインジェクトでき​​ます。クラス A で @Bean アノテーションを使用します。@Bean アノテーションは bean A を作成します。

class B {
    @Autowired
    private A beanA;
    // setter and getter
}

これがあなたのクラスAです

@Bean
 class A
  {
  private HashSet<Long> at = new HashSet<Long>();
  A()
   {
   //set is being initialsised here 

    this.at.add(new Long(44));
    this.at.add(new Long(34));
    this.at.add(new Long(54));
    this.at.add(new Long(55));
}
于 2013-09-12T08:15:31.173 に答える