すべての予備検索でいずれかの方法を示すために何かが戻ってこないので、次のことも可能かどうかを調べたいと思います。Spring AOPで特定のアノテーションをインターセプト
私は、Beanメソッドを検証するために、HibernateのValidatorアノテーションを使用したい、と私は、Hibernateのサブセットで注釈を付けた方法を傍受する一部 AOPフレームワーク(春、AOPアライアンス、AspectJの、など)を使用したいですバリデータアノテーション(@NotNull
、@NotEmpty
、@Email
など)。私は、AOPアドバイスを遭遇したときに実行したいと思っています。
これは可能ですか?もしそうなら、私はコードがどのように機能するかを視覚化するのに苦労しています。そして
public class SomeBean
{
private String data;
// Hibernate Validator annotation specifying that "data" cannot be an empty
// string.
@NotEmpty
public String getData() { ... } // etc.
}
、その豆を使用していくつかのコード:
まず、Hibernateのバリデータを用いてビーン:例として春AOPのMethodInterceptor
インターフェースを使用して
public void someMethod()
{
SomeBean oBean = new SomeBean();
// Validation should fail because we specified that "data" cannot be empty.
oBean.setData("");
}
次にであるとAOPのアドバイスHibernate Validatorに注釈を付けたメソッドが見つかったときに実行されました。
だから、public class ValidationInterceptor implements MethodInterceptor
{
public Object invoke(MethodInvocation invocation)
{
// Here's where we would use Hibernate's validator classes.
// My code example here is wrong, but it gets the point across.
Class targetClass = invocation.getClass(); // Should give me SomeBean.class
ClassValidator<targetClass> oValidator= new ClassValidator<targetClass>();
// Here I need to get a reference to the instance of the offending
// SomeBean object whose data has been set to empty...not sure how!
SomeBean oOffendingBean = getTheBadBeanSomehow();
InvalidValue[] badVals = oValidator.getInvalidValues(oOffendingBean);
}
}
だけでなく、私はコンフィギュレーションは、私がしたいのHibernate Validatorアノテーションを傍受するようになり、私は完全に実装する方法を把握していないだけでなく、どのような春AOP(ポイントカット定義、など)に窒息しています実際のアドバイス(例えば、上記のコメントの中でアドバイスの中から問題のあるSomeBean
をインスタンス化する方法)がありますが、この解決策が可能かどうか、春かどうかはわかりません。
正しい方向にいくつかの穏やかな "ナッジ"をお寄せいただきありがとうございます!
こんにちはグンナー - 私は今、すべての3つのリンクをチェックアウトしていた入力に感謝を。私は特定のフレームワークの一部ではない - ちょうど、適切なツールに興味を持って仕事をする!再度、感謝します – IAmYourFaja