@@ -23,7 +23,7 @@ public class AndroidSpringMvc {
2323 private static AndroidSpringMvc instance = new AndroidSpringMvc ();
2424 private HashMap <Class <?>, Object > beanMap = new HashMap ();
2525
26- public static void init ( Configuration configuration ) {
26+ public static void init (Configuration configuration ) {
2727 try {
2828 BeanContainer .init (configuration );
2929 } catch (Exception e ) {
@@ -55,39 +55,70 @@ private void injectObject(Object object) throws Exception {
5555 if (fieldAnnotation != null ) {
5656 Class <?> fieldType = field .getType ();
5757 Object injectInstance = null ;
58- //遍历实体配置
59- ArrayList <Class <?>> classArrayList = BeanContainer .getBeans ();
60- for (int i = 0 ; i < classArrayList .size (); i ++) {
61- if (fieldType .isAssignableFrom (classArrayList .get (i ))) {
62- Class <?> cls = classArrayList .get (i );
63- Service serviceAnnotation = cls .getAnnotation (Service .class );
64- Controller controllerAnnotation = cls .getAnnotation (Controller .class );
65- Dao daoAnnotation = cls .getAnnotation (Dao .class );
66- if (cls .getAnnotation (Service .class ) != null ) {//注入Service
67- String autowiredAnnotationName = fieldAnnotation .name ();
68- String serviceAnnotationName = serviceAnnotation .name ();
69- //可以注入的条件是名字相同 或者 autowiredAnnotationName无标记
70- if ((!TextUtils .isEmpty (autowiredAnnotationName ) && autowiredAnnotationName .equals (serviceAnnotationName )
71- || (TextUtils .isEmpty (autowiredAnnotationName )))) {
72- injectInstance = serviceAnnotation .singleInstance () ? injectAsSingleInstance (cls ) : injectAsNewInstance (cls );
73-
74- field .setAccessible (true );
75- field .set (object , injectInstance );
58+ String autowiredAnnotationName = fieldAnnotation .name ();
59+
60+ String beanName = fieldType .getName ();
61+ if (!TextUtils .isEmpty (autowiredAnnotationName )) {
62+ beanName = fieldType .getName () + "_" + autowiredAnnotationName ;
63+ }
64+
65+ Class <?> beanCls = BeanContainer .getBean (beanName );
66+ if (beanCls == null ) {
67+ //遍历实体配置
68+ ArrayList <Class <?>> classArrayList = BeanContainer .getAllBeans ();
69+ for (int i = 0 ; i < classArrayList .size (); i ++) {
70+ if (fieldType .isAssignableFrom (classArrayList .get (i ))) {//找到实现类
71+ Class <?> cls = classArrayList .get (i );
72+ Service serviceAnnotation = cls .getAnnotation (Service .class );
73+ Controller controllerAnnotation = cls .getAnnotation (Controller .class );
74+ Dao daoAnnotation = cls .getAnnotation (Dao .class );
75+ if (cls .getAnnotation (Service .class ) != null ) {//注入Service
76+ String serviceAnnotationName = serviceAnnotation .name ();
77+ //可以注入的条件是名字相同 或者 autowiredAnnotationName无标记
78+ if ((!TextUtils .isEmpty (autowiredAnnotationName ) && autowiredAnnotationName .equals (serviceAnnotationName )
79+ || (TextUtils .isEmpty (autowiredAnnotationName )))) {
80+ beanCls = cls ;
81+ break ;
82+ }
83+ } else if (controllerAnnotation != null ) {//注入Controller
84+ beanCls = cls ;
85+ break ;
86+ } else if (daoAnnotation != null ) {//注入Dao
87+ beanCls = cls ;
7688 break ;
7789 }
78- } else if (controllerAnnotation != null ) {//注入Controller
79- injectInstance = controllerAnnotation .singleInstance () ? injectAsSingleInstance (cls ) : injectAsNewInstance (cls );
80- field .setAccessible (true );
81- field .set (object , injectInstance );
82- break ;
83- } else if (daoAnnotation != null ) {//注入Dao
84- injectInstance = daoAnnotation .singleInstance () ? injectAsSingleInstance (cls ) : injectAsNewInstance (cls );
90+ }
91+ }
92+
93+ if (beanCls != null ) {
94+ BeanContainer .addBean (beanName , beanCls );
95+ }
96+ }
97+
98+ if (beanCls != null ) {
99+ Service serviceAnnotation = beanCls .getAnnotation (Service .class );
100+ Controller controllerAnnotation = beanCls .getAnnotation (Controller .class );
101+ Dao daoAnnotation = beanCls .getAnnotation (Dao .class );
102+ if (serviceAnnotation != null ) {//注入Service
103+ String serviceAnnotationName = serviceAnnotation .name ();
104+ //可以注入的条件是名字相同 或者 autowiredAnnotationName无标记
105+ if ((!TextUtils .isEmpty (autowiredAnnotationName ) && autowiredAnnotationName .equals (serviceAnnotationName )
106+ || (TextUtils .isEmpty (autowiredAnnotationName )))) {
107+ injectInstance = serviceAnnotation .singleInstance () ? injectAsSingleInstance (beanCls ) : injectAsNewInstance (beanCls );
85108 field .setAccessible (true );
86109 field .set (object , injectInstance );
87- break ;
88110 }
111+ } else if (controllerAnnotation != null ) {//注入Controller
112+ injectInstance = controllerAnnotation .singleInstance () ? injectAsSingleInstance (beanCls ) : injectAsNewInstance (beanCls );
113+ field .setAccessible (true );
114+ field .set (object , injectInstance );
115+ } else if (daoAnnotation != null ) {//注入Dao
116+ injectInstance = daoAnnotation .singleInstance () ? injectAsSingleInstance (beanCls ) : injectAsNewInstance (beanCls );
117+ field .setAccessible (true );
118+ field .set (object , injectInstance );
89119 }
90120 }
121+
91122 if (injectInstance == null ) {
92123 try {
93124 injectInstance = fieldType .newInstance ();
@@ -98,10 +129,7 @@ private void injectObject(Object object) throws Exception {
98129 throw new RuntimeException (fieldType + " cannot Autowired inject" );
99130 }
100131 }
101-
102132 }
103-
104- Log .i ("mvc" ,field .getName ()+"" );
105133 }
106134
107135 }
0 commit comments