Android 中怎么利用Spring实现依赖注入

发布时间:2021-06-28 15:11:32 作者:Leah
来源:亿速云 阅读:227

Android 中怎么利用Spring实现依赖注入,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1.手工装配依赖对象

手工装配依赖对象,在这种方式中又有两种编程方式

在xml配置文件中,通过在bean节点下配置

在java代码中使用@Autowired或@Resource注解方式进行装配

依赖注入--手工装配--XML方式

通过setter方法注入依赖

<bean>元素的< property >子元素指明了使用它们的set方法来注入。可以注入任何东西,从基本类型到集合类,甚至是应用系统的bean。

通过setter方法注入依赖

简单bean配置

配置bean的简单属性,基本数据类型和String。

<beanidbeanid="personService" class="com.test.bean.impl.PersonServiceImpl"> <!-- 基本类型,string类型 --> <propertynamepropertyname="age"value="20"></property> <propertynamepropertyname="name" value="张无忌"></property>                         </bean>

通过setter方法注入依赖

引用其它bean

<beanidbeanid="person"class="com.test.bean.Person" /> <beanidbeanid="personService"   class="com.test.bean.impl.PersonServiceImpl"> <!-- 引用类型 --> <propertynamepropertyname="person" ref="person" /> </bean>

内部bean

<beanidbeanid="personService"class="com.test.bean.impl.PersonServiceImpl"> <!-- 内部bean注入 --> <propertynamepropertyname="personClass"> <beanclassbeanclass="com.test.bean.PersonClass" /> </propert> </bean>

这种方式的缺点是你无法在其它地方重用这个personClass实例,原因是它是专门为personService而用。

装配集合

若bean的属性是集合类型,按如下处理:

A、装配List和数组:

<!-- 装配list --> <propertynamepropertyname="lists">   <list>     <value>list1</value>     <value>list2</value>     <refbeanrefbean="person"/>   </list> </property> <!--装配数组 --> <property name="obj">   <list>     <value>obj1</value>     <value>obj2</value>     <refbeanrefbean="person"/>   </list> </property>

B、 装配set:

<!--装配set --> <property name="sets">    <set>     <value>set1</value>     <value>set2</value>     <refbeanrefbean="person"/>   </set> </property>

set使用方法和list一样,不同的是对象被装配到set中,而list是装配到List或数组中装配。

装配集合

C、装配map:

<!-- 装配map--> <propertynamepropertyname="maps"> <map> <entrykeyentrykey="01"> <value>map01</value> </entry> <entrykeyentrykey="02"> <value>map02</value> </entry> </map> </property>

map中的<entry>的数值和<list>以及<set>的一样,可以使任何有效的属性元素,需要注意的是key值必须是String的。

D、装配Properties:

<!--装配Properties  --> <property name="props"> <props> <prop key="01">prop1</prop> <prop key="02">prop2</prop> </props> </property>

E、设置null:

<!--装配null --> <property name="listnull"> <null/> </property>

通过参数的顺序:

<constructor-argindexconstructor-argindex="0"> <value>张三</value> </constructor-arg> <constructor-argindexconstructor-argindex="1"> <value>56</value> </constructor-arg>

通过构造函数注入依赖

<!--通过参数的类型 --> <constructor-argtypeconstructor-argtype="java.lang.Integer">    <value>56</value> </constructor-arg> <constructor-argtypeconstructor-argtype="java.lang.String">    <value>张三</value> </constructor-arg>

依赖注入--手工装配&mdash;注解方式

在java代码中使用@Autowired或@Resource注解方式进行装配的前提条件是。

1、引入context命名空间 需要在xml配置文件中配置以下信息:

<beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> </beans>

2、在配置文件中添加context:annotation-config标签

<context:annotation-config/>

这个配置隐式注册了多个对注释进行解析处理的处理器

AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,   PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor

注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar

在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。

@Autowired

privatePersonDao  personDao;//用于字段上

@Autowired

publicvoid setPersonDao(PersonDaopersonDao) { //用于属性的set方法上         this.personDao = personDao;  }

@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。

@Autowired(required=false)     privatePersonDao  personDao;//用于字段上  @Autowired(request=false)     public voidsetPersonDao(PersonDaopersonDao) {  //用于属性的set方法上         this.personDao = personDao;  }

如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:

@Autowired@Qualifier("personDao")     privatePersonDao  personDao;//用于字段上  @Autowired  publicvoidsetPersonDao(@Qualifier("personDao") PersonDao personDao) {//用于属性的set方法上        this.personDao= personDao;  }

@Qualifier注解也能够被指定为构造器的参数或者方法的参数:

@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上.

@Resource注解默认按名称装配。

名称可以通过@Resource的name属性指定,如果没有指定name属性,

当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象

当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。

@Resource(name="personDao")      privatePersonDaopersonDao;//用于字段上  @Resource(name="personDao")  publicvoidsetPersonDao(PersonDao personDao) {//用于属性的set方法上      this.personDao = personDao;  }

后一种相当于xml配置文件中的

<propertynamepropertyname=“personDao"ref="personDao" />

注意:如果没有指定name属性,并且按照默认的名称找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。

2.自动装配依赖对象

对于自动装配,大家了解一下就可以了,实在不推荐大家使用。例子:

<beanidbeanid=“foo”class=“...Foo” autowire=“autowire type”>

autowire属性取值如下

byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null。

byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null。

constructor与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

autodetect :首先尝试使用constructor来自动装配,然后使用byType方式。不确定性的处理与constructor方式和byType方式一致。

通过在classpath自动扫描方式把组件纳入spring容器中管理

前面的例子我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。

spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。

要使用自动扫描机制,我们需要打开以下配置信息:

1、引入context命名空间 需要在xml配置文件中配置以下信息:

<beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scanbase-packagecontext:component-scanbase-package="cn.itcast"/> </beans>

2、在配置文件中添加context:component-scan标签

<context:component-scanbase-packagecontext:component-scanbase-package="cn.itcast"/>

其中base-package为需要扫描的包(含子包)。

注:

1、在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。

2、功能介绍

@Service用于标注业务层组件、

@Controller用于标注控制层组件(如struts中的action)、

@Repository用于标注数据访问组件,即DAO组件。

而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

//Dao层  importorg.springframework.stereotype.Repository;  importcom.test.dao.PersonDao;    @Repository("personDao")     publicclassPersonDaoBean implements PersonDao {  }
//业务层  importjavax.annotation.Resource;  importorg.springframework.stereotype.Service;  importcom.test.dao.PersonDao;  importcom.test.service.PersonService;   @Service("personService")     publicclassPersonServiceBean implements PersonService {   @Resource(name="personDao")     privatePersonDao personDao;  }

看完上述内容,你们掌握Android 中怎么利用Spring实现依赖注入的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Android开发者怎么进一步学习Android
  2. 如何在Android环境下支持Spring框架下得AOP,DI,Aspect等

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android spring

上一篇:Android 8.0版本更新无法自动安装怎么办

下一篇:Android 中的目录结构有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》