2010-12-29 9 views
0

Spring FrameworkでJava 1.5で開発されたWebアプリケーションがあります。アプリケーションには、多数の情報が再グループ化され、ユーザーがいくつかのステータスを変更できる単純なページである「ダッシュボード」が含まれています。管理者は、これら3つのダッシュボードのデータベースにログシステムを追加したいと考えています。各ダッシュボードには異なる情報がありますが、ログは日付とユーザーのログインによってトレースされる必要があります。スプリングで戦略パターンを効率的に実装する方法は?

interface DashboardLog { 
    void createLog(String login, Date now); 
} 

// Implementation for one dashboard 
class PrintDashboardLog implements DashboardLog { 
    Integer docId; 
    String status; 

    void createLog(String login, Date now){ 
    // Some code 
    } 
} 

class DashboardsManager { 
    DashboardLog logger; 
    String login; 
    Date now; 

    void createLog(){ 
    logger.log(login,now); 
    } 
} 

class UpdateDocAction{ 
    DashboardsManager dbManager; 

    void updateSomeField(){ 
     // Some action 
     // Now it's time to log 
     dbManagers.setLogger = new PrintDashboardLog(docId, status); 
     dbManagers.createLog(); 
    } 
} 

Appcontext.xml::私はそのための依存関係を使用していないよ。このソリューションでは

<bean id="dashboardManagers" class="...DashboardManagers" /> 

私が何をしたいのですがどのような

は、このような種類のStrategyパターンを実装することです注入。このようにするのは「正しい」(良い練習、パフォーマンス、...)ですか?私はDIを使うより良い方法がありますか?

注:コンストラクタやgetter/setterのような基本的なものは書きませんでした。

+0

userIdのみを渡し、データベースによって日付が設定されていることを検討してください。これは、「プログラマーschenanigans」の可能性を制限します。 – DwB

答えて

1

完全にあなたが持っているような戦略パターンを採用することが「正しい」が、あなたが春を使用しているという事実を考慮しているが - Springフレームワークが提供するDependency Injectionメカニズムを採用すること方が良いだろう - かもしれませんあなたのフレームワークがコアの強みの1つとして提供しなければならないものを使用します。

+0

その理由は、戦略パターンが私のニーズに完全にマッチすると思うにもかかわらず、私は自分自身に正しいことを質問しています。 – Anth0

+0

weelll - あなたが選択したかどうかに関係なく、問題はありません。 SpringのDI機能を使用します。パフォーマンスやロジックがわからない場合、あなたのアプローチには本当に害はありません。大丈夫だよ。私はあなたが少なくともSpring MVCの機能を利用していると仮定しています。そうしなければ、Springを使用すべきかどうかを尋ねるべきです。 – anirvan

2

解決策は、updateSomeField()を呼び出すたびにPrintDashboardLogの新しいインスタンスを作成します。これは不必要な時間/メモリ/ GC努力を要するかもしれない。また、設計の観点からは、各ダッシュボードに1つのDashboardLogが存在し、各呼び出しごとに新しいDashboardLogがない場合は意味があります。

私は、ロギングが典型的な用途の1つであるという側面を使用することをお勧めします。次のようなものがあります。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 

    <bean id="loggingAspect" class="com.yourcompany.yourapplication.aspects.DashboardLogAspect" /> 

    <aop:aspectj-autoproxy> 
     <aop:include name="loggingAspect" /> 
    </aop:aspectj-autoproxy> 

</beans>  


package com.yourcompany.yourapplication.aspects; 

import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 

@Aspect 
public class DashboardLogAspect { 

    @Around("execution(* com.yourcompany.yourapplication..*Action+.*(..)) && target(target)") 
    public Object logActionCall(ProceedingJoinPoint pjp, Object target) throws Throwable { 

     long before = System.nanoTime(); 

     Object returnValue = pjp.proceed(); 

     long after = System.nanoTime(); 
     long durationNs = after - before; 

     String logMsg = target.getClass() + "." + pjp.getSignature().toShortString() + " (" + durationNs + " ns)"; 

     // TODO: store the log message in your database 
     System.out.println(logMsg); 

     return returnValue; 
    }    
} 

これは、「アクション」で終わる名前のアプリケーションクラスへのすべての呼び出しを記録します。また、各コールが完了するまでの時間も加算されます。特定のメソッド名パターンのAroundアドバイスを微調整することもできます。 the AspectJ programming guide

1

それぞれの「ダッシュボード」にコントローラがある場合は、コントローラからのログを呼び出さないようにしてください。


public interface DashboardLog 
{ 
    void createLog(...); 
} 

public class DashboardUno 
implements DashboardLog 
{ 
    ... 
    public void createLog(...) 
    { ... } 
} 

@Controller 
@RequestMapping("/blah/schmarr") 
public class BlahController 
{ 
    ... 
    @RequestMapping(value = "/xxx") 
    public String someMeaningfulName(...) 
    { 
     DashboardUno elEsUno; 
     ... get the dashboard object ... 
     elEsUno.createLog(...); 
     ... 
    } 
}
関連する問題