0

いくつかの EJB (@Stateless アノテーションが付けられている) があり、それらを呼び出したときにロードされます (つまり、アプリケーション サーバーの起動時に積極的にロードされません)。

それらのいくつかには、カスタム メソッド アノテーションが含まれています。これを @Foo と呼びましょう。

アプリケーション サーバー (AS) の起動時にそれらすべてをスキャンし、@Foo で注釈が付けられているものを見つける方法を見つけたいと考えています。

これまでのところ、AS ブートで呼び出されるライフサイクル リスナーを web.xml に登録しました。

  • PS#1: @PostConstruct は、EJB が最初に呼び出されたときに呼び出されます。これは、AP の起動後に実行される可能性があります。
  • PS#2: EJB が JNDI に登録されたときにスローされるイベントはありますか?
  • PS#3: パッケージの下のすべてのクラスをスキャンするように構成すると、Spring は同様のことを行うと思います
4

1 に答える 1

0

If the EJB is constructed by Spring, then you can use a Spring bean post processor (this is what Spring uses when it performs it's scan).

@Component
public class FooFinder implements BeanPostProcessor {

    List<Method> fooMethods = new ArrayList<>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        for (Method method : targetClass.getDeclaredMethods()){
            for (Annotation annotation : AnnotationUtils.getAnnotations(method)){
                if (annotation instanceof Foo){
                    fooMethods.add(method);
                }
            }
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

Also be warned... be careful about dependency injection into your BeanPostProcessor. You may create problems with the Spring dependency graph, as the BeanPostProcessors must be created first. If you need to register the method with some other bean, make the BeanPostProcessor ApplicationContextAware or BeanFactoryAware, and get the bean that way.

于 2013-08-22T16:15:01.753 に答える