SSM学习笔记之XML方式的Spring Bean管理

##Spring工厂类

     

 

 

一般用ApplicationContext

FileSystemXmlApplicationContext:系统路径找配置文件

ClassPathXmlApplicationContext:项目内

 

##Spring Bean管理之xml方式

    三种方式:

   使用构造器(默认无参数)

   使用静态工厂

   使用实例化工厂

   1.使用构造器(默认)

      在项目中创建Demo2文件夹,创建类Bean1

      

public class Bean1 {
    public Bean1(){
        System.out.println("bean1被实例化了");
    }
}

      在applicationContex.xml配置:

<bean id="bean1" class="com.imooc.ioc.demo2.Bean1">
</bean>

2.使用静态工厂

   创建类Bean2和BeanFactory:

public class Bean2Factory {
    public static Bean2 createBean2(){
        System.out.println("bean2工厂执行了");
        return new Bean2();
    }
}

在applicationContext中配置:

<bean id="bean2" class="com.imooc.ioc.demo2.Bean2Factory" factory-method="createBean2">
</bean>

3.使用实例化工厂

  创建Bean3和Bean3Factory

public class Bean3Factory {
    public Bean3 createBean3(){
        System.out.println("Bean3的工厂被执行了");
        return new Bean3();
    }
}

在applicationContext中配置:

<!--第三种:实例化工厂的方式-->
<bean id="bean3Factory" class="com.imooc.ioc.demo2.Bean3Factory" ></bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"></bean>

 

name和id:

  id在IOC容器中是唯一的

  如果名称中含有特殊字符,就要用name

Bean的作用域:

      

Bean的生命周期:

在ApplicationContext.xml的bean中配置init-method和destroy-method。

在Bean被载入到容器的时候会调用init,在Bean从容器中删除是会调用destroy(仅仅在scope=singleton生效)。

完整的生命周期

其中第五步和第八步和aop开发相关。

##Spring之DI(依赖注入):

1.构造器方式:

   创建测试类User:

 public class User {
    private String name;
    private Integer age;

     public User(String name, Integer age) {
         this.name = name;
         this.age = age;
     }

     @Override
     public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
     }
 }

     在applicationContex.xml中配置:

   <bean class="com.imooc.ioc.Demo4.User" id="user">
      <constructor-arg name="name" value="张三"/>
      <constructor-arg name="age" value="23"/>
   </bean>

 2.set方法注入

    创建Person类和Cat类,并加入setter和getter和toString

  在applicationContex.xml中配置:

</bean>
    <bean class="com.imooc.ioc.Demo4.Person" id="person">
        <property name="name" value="李四"/>
        <property name="age" value="24"/>
        <property name="cat" ref="cat"/>   //将cat引用类型注入到Person中
    </bean>
    <bean id="cat" class="com.imooc.ioc.Demo4.Cat">
        <property name="name" value="ketty"/>
    </bean>

3.P名称空间的注入:

   在beans中插入:

xmlns:p="http://www.springframework.org/schema/p"

  注入:

<bean class="com.imooc.ioc.Demo4.Person" id="person" p:name="大黄" p:age="23" p:cat-ref="cat"/> //Cat是引用类型所以注入要用cat-ref
<bean id="cat" class="com.imooc.ioc.Demo4.Cat" p:name="小黄"/>

4.SpEL注入

#{语法}

创建Product和categoty类:

注入:

<!--SpEL的属性注入-->
<bean id="category" class="com.imooc.ioc.Demo4.Category">
    <property name="name" value="#{'服装'}"/>
</bean>

<bean id="product" class="com.imooc.ioc.Demo4.Product">
    <property name="name" value="#{'男装'}"/>
    <property name="price" value="#{199.00}"/>
    <property name="category" value="#{category}"/>
</bean>

5.复杂属性注入

<!--集合类型的属性注入-->
<bean id="collectionBean" class="com.imooc.ioc.Demo5.CollectionBean">
    <!--数组类型-->
    <property name="arrs">
        <list>
            <value>bbb</value>
            <value>abb</value>
            <value>aab</value>
        </list>
    </property>
    <!--list类型-->
    <property name="list">
        <list>
            <value>bbb</value>
            <value>abb</value>
            <value>aab</value>
        </list>
    </property>
    <!--Set集合的属性注入-->
    <property name="set">
        <set>
            <value>a</value>
            <value>abb</value>
            <value>aab</value>
        </set>
    </property>
    <!--Map集合的属性注入-->
    <property name="map">
        <map>
            <entry key="a" value="1"/>
            <entry key="b" value="2"/>
            <entry key="c" value="3"/>
        </map>
    </property>
    <!--Properties的属性注入-->
    <property name="properties">
        <props>
            <prop key="username">root</prop>
            <prop key="password">root</prop>
        </props>
    </property>
</bean>

    

 

 

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值