2016-10-07 5 views
0

私の質問は簡単です。以下のクラスを考えてみましょう。 @Valueアノテーションを使ってプロパティファイルを使わずに、i = 1、str = "abc"という値を直接配置する必要があります。どうやってするか ?春@valueプロパティファイルなしで値を注入する

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Component 
public class Example { 

    int i; 
    String str; 

    @Autowired 
    AccountService obj; 

    public void setI(int i) { 
     this.i = i; 
    } 
    public void setStr(String str) { 
     this.str = str; 
    } 
    public void setObj(AccountService obj) { 
     this.obj = obj; 
    } 


} 

答えて

1

まあ、単にこれは値を注入するためのセッターを呼び出すXML <property name="" value="" />への書き込みと同じです

@Component 
public class Example { 
    @Value("1") 
    int i; 
    @Value("abc") 
    String str; 

    @Autowired 
    AccountService obj; 

    public void setI(int i) { 
     this.i = i; 
    } 
    public void setStr(String str) { 
     this.str = str; 
    } 
    public void setObj(AccountService obj) { 
     this.obj = obj; 
    } 

} 

ください。

+0

すごく分かりませんでした。私は私の答えを削除します。 +1 –

関連する問題