Sping中Bean的简单认识(一)

    技术2022-05-19  22

    Bean的简单认识:

     

             spring配置文件中,使用bean元素来装配一个Bean,它的常用属性有一下九种

    1、  id:指它该Bean的唯一标识。

    2、  class:指定该Bean的全限定名。

    3、  name:为该Bean指定一到多个别名。多个别名可以用空格、分隔。

    4、  autowire:指定该Bean的属性的装配方式(后面会详细介绍)

    5、  scope:指定该Bean的存在范围(同上)

    6、  init-method:指定该Bean的初始化方法

    7、  destroy-method:指定该Bean的销毁方法

    8、  abstract:指定该Bean是否为抽象的,如果是抽象的,则spring不在为其创建实例。

    9、  parent:指定该Bean的父类标识或别名。

     

    装配Bean的各种类型的属性值

    1、  简单类型属性值的装配

    Bean中通常有一些简单的类型属性,比如Stringint这些简单的类型都可以通过bean元素的子元素property来设置

     

    比如:

                       <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

       

        <bean id="hello" class="cn.csdn.service.GreetingServiceImpl" init-method="init">

           <property name="say" value="hello"/>

        </bean>

        <bean id="test" class="cn.csdn.service.Test"></bean>

    </beans>

     

    2、  引用其他Bean的装配

    如果Bean的某个属性是复杂的类型,既它将引用自其他的Bean。那么在Spring配置文件中可以使用property元素的ref属性来引用

     

    比如:

     

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     

     

             <!-- 创建一个DAObean id是唯一的标识 class指明类路径 propertybean的属性 -->

            <bean id="greetingDAOImpl" class="cn.csdn.dao.GreetingDAOImpl">

            <property name="say">

               <value>hello</value>

            </property>

           </bean>

       

       

           <!-- 创建一个Servicebean  ref引用对象-->

           <bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">

           <property name="greetingDAOImpl" ref="greetingDAOImpl"></property>

           </bean>

    </beans>

     

    还有一种不常用的方法是在property元素中嵌入一个bean元素来指定所引用的Bean

     

    比如:

             <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     

     

             <!-- 创建一个DAObean id是唯一的标识 class指明类路径 propertybean的属性 -->

            <bean id="greetingDAOImpl" class="cn.csdn.dao.GreetingDAOImpl">

            <property name="say">

               <value>hello</value>

            </property>

           </bean>

       

       

           <!-- 创建一个Servicebean  ref引用对象-->

           <bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">

           <property name="greetingDAOImpl">

               <bean class=”cn.csdn.dao.GreetingDAOImpl”></bean>

    </property>

           </bean>

    </beans>

    这种装配方式的确定是用户无法在其他地方重用这个内嵌的Bean

     

     

    未完待续……………………………………(请继续关注GoodWell的东东)


    最新回复(0)