Spring学习之DI(二)

来源:互联网 发布:mac液体唇膏三文鱼粉 编辑:程序博客网 时间:2024/06/11 02:19

在上一篇里边我们知道了Spring可以帮我们将一个对象注入到另外一个对象之中,那我们应该怎么做呢?我们可以通过XML配置的方式或者注解的方式来实现对象的依赖注入,在Spring中我们叫做组件的装配。下面是一个简单的Spring配置文件bean.xml,这个文件把一种操作用户的方式赋给一个PersonServiceBean。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="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"><bean id="personDao" class="com.why.dao.impl.PersonDaoBean"></bean> <bean id="personService" class="com.why.service.impl.PersonServiceBean"> <property name="personDao" ref="personDao"></property> </bean></beans>

然后我们需要启动Spring容器来实现组件之间的装配,实例化Spring容器有两种方式。

方法一:

在类路径下寻找配置文件来实例化容器

ApplicationContextctx=newClassPathXmlApplicationContext(newString[]{"beans.xml"});

方法二:

在文件系统路径下

配置文件来实例化容器

ApplicationContextctx=newFileSystemXmlApplicationContext(newString[]{“d:\\beans.xml“});

Spring的配置文件可以指定多个,可以通过String数组传入。


spring容器启动后,因为spring容器可以管理bean对象的创建,销毁等生命周期,所以我们只需从容器直接获取Bean对象就行,而不用编写一句代码来创建bean对象。从容器获取bean对象的代码如下:

ApplicationContextctx = newClassPathXmlApplicationContext(“beans.xml”);

personService = (personService)ctx.getBean("personService");

到这里我们可以通过调用personService的方法来实现用户的保存。以上就是Spring依赖注入的简单应用


spring中bean的实例化、作用域以及指定bean的初始化方法(在spring调用bean的构造方法之后调用)和销毁方法(在spring容器调用close方法时调用)省略不说。


Spring中注入依赖对象有三种方式。

使用构造器注入:

<bean id="personDao" class="com.why.dao.impl.PersonDaoBean"></bean>     <bean id="personservice" class="com.why.service.impl.PersonServiceBean" lazy-init="true">                <constructor-arg index="0" type="com.why.dao.PersonDao" ref="personDao"></constructor-arg>    //其他类型bean         <constructor-arg index="1" value="why"></constructor-arg>        //基本类型bean         <constructor-arg index="2" value="88"></constructor-arg>               </bean></bean>

使用属性setter方法注入:

<bean id="personDao" class="com.why.dao.impl.PersonDaoBean"></bean>     <bean id="personservice" class="com.why.service.impl.PersonServiceBean" lazy-init="true">         <property name="personDao" ref="personDao"></property>         <property name="name" value="why"></property>         <property name="id" value="88"></property>              </bean></bean>

使用Field注入(这种方式需要使用注解,前面的一种方式也可以使用注解,因为依赖注入的注解既可以用在field上也可以用在方法上),这种方式如下所示。

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

   @Autowired

   privatePersonDao  personDao;//用于字段上

    @Autowired

    public voidsetOrderDao(OrderDaoorderDao) {//用于属性的setter方法上

        this.orderDao =orderDao;

    }

@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:

   @Autowired 

   @Qualifier("personDaoBean")

    privatePersonDao personDao;

@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resourcename属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。

    @Resource(name=“personDaoBean”)

    privatePersonDao personDao;//用于字段上

注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时,@Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。因为使用了注解,所以需要在配置文件中加入context命名空间并且加入<context:annotation-config/>这个配置项


前面的例子我们都是使用XMLbean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些这组件采用xmlbean定义来配置显然会增加配置文件的体积查找及维护起来也不太方便。spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了@Component@Service@Controller@Repository注解的类,并把这些类纳入进spring容器中管理它的作用和在xml文件中使用bean节点配置组件是一样的要使用自动扫描机制,我们需要打开以下配置信息:

<beansxmlns="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-package="cn.why"/>

</beans>

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

@Service用于标注业务层组件、 @Controller用于标注控制层组件(如struts中的action)、@Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。


注:本文大量参考了黎活明老师的Spring2.5的视频




0 0
原创粉丝点击