2016-06-25 6 views
3

私はJavaのWebスクリプトにBeanを挿入する方法を知っています。アルフレッド注入豆

<bean id="webscript.ru.mycompany.maypackage.myclass.get" 
    class="ru.mycompany.maypackage.myclass" 
    parent="webscript">    
    <property name="properties"> 
      <ref bean="global-properties"/> 
    </property> 
</bean> 

しかし、それは、カスタムJavaクラスのために働いていない、変数プロパティ nullである:(

<bean id="myclass" 
    class="ru.mycompany.maypackage.myclass">     
    <property name="properties"> 
      <ref bean="global-properties"/> 
    </property> 
</bean> 

public class myclass 
{ 
    private Properties properties; 
    public myclass() 
    { 

    } 

    public void setProperties(Properties properties) 
    { 
     this.properties = properties;  
    } 
} 

私はあなたが必要となります

+1

myclassインスタンスはどのように呼び出すのですか?おそらく、あなたはmyclassを "新しい"演算子で呼び出すでしょう。 –

+0

はい、新しいmyclass()。mymethod() –

答えて

0

何をする必要があります。それは非常に簡単です?を実装するにはApplicationContextAwareだからあなたは

public class myclass implements ApplicationContextAware 

かのようにあなたは、オートワイヤリングをする必要があります:

@Value("${custom.url}") 
protected String url; 
+0

ありがとうございますが、MyClassはレポ側ですが、共有側ではありません –

+0

このコードはレポの側です。 –

1

あなたが他のBean(あなたがそれを使用する場所)あなたはMyClassのクラスのグローバル・プロパティを注入と同じようにMyClassの豆を注入する必要があります。 Spring IOCは "new"演算子を使用してインスタンスを作成する場合、myclassについては何も知らない

関連する問題