2017-08-31 4 views
1

コードから一定の移動するための最良の方法は何ですか? (つまり、Spring起動application.properties)春 - - リファクタリング構成に一定の移動エンティティ(application.properties)

操作は現在Postクラスにカプセル化されていますが、HOTPOST_LIKE_TRESHOLDをクラスから移動しても問題はありませんが、どこかでincreaseLikeCounterメソッドを保持したいと考えています。

+0

投稿が暑いと判断するべきではありません。このルールをエンコードする別のエンティティが存在する必要があります。 –

+0

しかし、Postが決定しなければならない状況についてはどうでしょうか?設定に移動したい値を使用する必要がありますが、througメソッドのパラメータを渡したくないのですか? 質問は、ビジネスルールではなく、技術的な可能性についてです。 エンティティ(バネで管理されていない)でコンフィグレーション可能なプロパティを使用する最も良い方法は、メソッドパラメータで渡さないでください。 – msiakis

+0

まあ、技術的な可能性を議論しているならば、Springのオブジェクトについてはautowireで、それに一意の名前を付けることができます。 –

答えて

2

変更ポストへ:

@Entity 
public abstract class Post { 

    ... 

    protected abstract Integer getHotpostLikeTreshold();  
    ... 

    public long increaseLikeCounter() { 
     likesCount++; 

     if (likesCount >= getHotpostLikeTreshold()) { 
     this.setStatus(HOT); 
     } 

     return likesCount; 
    } 
    ... 

} 

は、その後、例えば、クラスを拡張する:あなたのapplication.propertiesで

public class SimplePost extends Post { 

@Value("${simplePost.hotpostLikeTreshold}") 
private Integer hotpostLikeTreshold; 

@Override 
protected Integer getHotpostLikeTreshold(){ 
    return hotpostLikeTreshold; 
} 
... 

追加

simplePost.hotpostLikeTreshold = 6 

EDIT:

use a Service and getters setters: 

@Service 
public class SimplePostService{ 

    // wire your property in 
    @Value("${simplePost.hotpostLikeTreshold}") 
    private Integer hotpostLikeTreshold; 

    public Post savePost(...){ 
     Post post = new SimplePost(); // you create your post with 'new' 
     ... 
     // set your property 
     post.setHotpostLikeTreshold(this.hotpostLikeTreshold); 
     ... 
     // save your entity 
     SimplePostDAO.save(post); 
    } 

} 
+0

ここではJPA @Entityオブジェクトについて議論しています。彼らは春の豆ではありません。 「新しい」演算子(new SimplePost())を使うか、EntityManagerを使ってアクセスすることで作成しています。あなたのソウルティオンはどのように機能しますか? – msiakis

+0

@msiakisこの場合、その設定をSpringがautowiredできる場所に外部化する必要があります。 –

関連する問題