将spring存储库注入静态方法

时间:2017-10-24 06:00:51

标签: spring spring-data-jpa

我生成一个实现接口CheckInterface的实例列表 使用反射,但其中一些实例需要使用JPA存储库 ,因为它们是反射新的,所以Spring不会为它们注入存储库。 那么如何将存储库注入这些实例

或者我如何将这些类委托给Spring作为@Bean并迭代它们?

        <LinearLayout
            android:id="@+id/lDates"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/card_margins"
            android:layout_marginEnd="@dimen/card_margins"
            android:layout_below="@id/lnrButtons"
            android:orientation="horizontal">

            <android.support.v7.widget.CardView
                android:id="@+id/cardSectionA"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:cardElevation="0dp"
                android:layout_marginEnd="@dimen/card_margins"
                android:layout_marginBottom="@dimen/card_margins"
                app:cardCornerRadius="4dp"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/textLabelA"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/sample_value"/>

            </android.support.v7.widget.CardView> 

            <android.support.v7.widget.CardView
                android:id="@+id/cardSectionB"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:cardElevation="0dp"
                android:layout_marginEnd="@dimen/card_margins"
                android:layout_marginBottom="@dimen/card_margins"
                app:cardCornerRadius="4dp"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/textLabelB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sample_value"/>

            </android.support.v7.widget.CardView>                          
        </LinearLayout>      
  

当我从ValidationRegister.itemHashMap获取LPHYCZ的实例时   claimDataEntityRepository将为null

1 个答案:

答案 0 :(得分:0)

只需将您的Reflections reflections设置为Spring Bean并将其注入'ValidationRegister',然后使用它初始化您的HashMap您的hashmap仍然可以是静态的,如果您希望它由多个{共享{1}}实例,但注入不会发生在静态字段上。