-1

こんにちは、私はSpring 3を使用しています

私は使っています

applicationContext.getBeansOfType

現在のスコープで既にインスタンス化されている Bean のみを取得する可能性はありますか?

現在のリクエストでまだ使用されていない Bean をインスタンス化したくありません。

4

1 に答える 1

1

そうではないと思いますが、次のように書くことができます。

public static List<Object> getBeansInScope(Class<?> type, AbstractApplicationContext ctx, int scope) {
        List<Object> beans = new ArrayList<Object>();
        String[] names = ctx.getBeanNamesForType(type);
        RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();

        for (String name : names) {
            Object bean = attributes.getAttribute(name, scope);
            if (bean != null) 
                beans.add(bean);
        }

        return beans;
    }

これは、リクエスト スコープとセッション スコープでのみ機能します。

于 2013-03-22T11:45:45.763 に答える