Spring框架入手学习(三)

前言

 继续上一篇(二)的内容,主要讲bean的生命周期,以及属性注入的几种方式。


正文

1. Bean生命周期

1.1初始化和销毁

目标方法执行前后执行后,将进行初始化或销毁。

<bean id="" class="" init-method="初始化方法名称"  destroy-method="销毁的方法名称">

目标类:

public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {
        System.out.println("e_lifecycle add user");
    }

    public void myInit(){
        System.out.println("初始化");
    }
    public void myDestroy(){
        System.out.println("销毁");
    }

}

 先定义接口UserService,然后写实现类UserServiceImpl,实现类里面加入初始化跟销毁方法,可以将需要的代码写入到这里,一旦启动就会加载初始化方法

spring配置:

<!--  
        init-method 用于配置初始化方法,准备数据等
        destroy-method 用于配置销毁方法,清理资源等
    -->
    <bean id="userServiceId" class="com.itheima.e_lifecycle.UserServiceImpl" 
        init-method="myInit" destroy-method="myDestroy" ></bean>

测试:

@Test
    public void demo02() throws Exception{
        //spring 工厂
        String xmlPath = "com/itheima/e_lifecycle/beans.xml";
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserService userService = (UserService) applicationContext.getBean("userServiceId");
        userService.addUser();

        //要求:1.容器必须close,销毁方法执行; 2.必须是单例的
//applicationContext.getClass().getMethod("close").invoke(applicationContext);
        // * 此方法接口中没有定义,实现类提供
        applicationContext.close();

    }

 只有在代码中加入applicationContext.close()方法,才能执行销毁代码,无法执行自动销毁。

2.属性依赖注入

  • 依赖注入方式:手动装配 和 自动装配
  • 手动装配:一般进行配置信息都采用手动
    基于xml装配:构造方法、setter方法
    基于注解装配:
  • 自动装配:struts和spring 整合可以自动装配
    byType:按类型装配
    byName:按名称装配
    constructor构造装配,
    auto: 不确定装配。

2.1 构造方法

目标类:

public class User {

    private Integer uid;
    private String username;
    private Integer age;

    public User(Integer uid, String username) {
        super();
        this.uid = uid;
        this.username = username;
    }

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

Spring配置 bean.xml:
方式1:

<!-- 构造方法注入 
        * <constructor-arg> 用于配置构造方法一个参数argument
            name :参数的名称
            value:设置普通数据
            ref:引用数据,一般是另一个bean id值

            index :参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。
            type :确定参数类型
        例如:使用名称name
            <constructor-arg name="username" value="jack"></constructor-arg>
            <constructor-arg name="age" value="18"></constructor-arg>
        例如2:【类型type 和  索引 index
            <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
            <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
    -->
    <bean id="userId" class="com.itheima.f_xml.a_constructor.User" >
        <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
        <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
    </bean>

Spring配置 bean.xml:
方式二:setter方法

<!-- setter方法注入 
        * 普通数据 
            <property name="" value="值">
            等效
            <property name="">
                <value>值
        * 引用数据
            <property name="" ref="另一个bean">
            等效
            <property name="">
                <ref bean="另一个bean"/>

    -->
    <bean id="personId" class="com.itheima.f_xml.b_setter.Person">
        <property name="pname" value="阳志"></property>
        <property name="age">
            <value>1234</value>
        </property>

        <property name="homeAddr" ref="homeAddrId"></property>
        <property name="companyAddr">
            <ref bean="companyAddrId"/>
        </property>
    </bean>

    <bean id="homeAddrId" class="com.itheima.f_xml.b_setter.Address">
        <property name="addr" value="阜南"></property>
        <property name="tel" value="911"></property>
    </bean>
    <bean id="companyAddrId" class="com.itheima.f_xml.b_setter.Address">
        <property name="addr" value="北京八宝山"></property>
        <property name="tel" value="120"></property>
    </bean>

Spring配置 bean.xml:
P命令空间:
对“setter方法注入”进行简化,替换,而是在
bean p:属性名=”普通值” p:属性名-ref=”引用值”
p命名空间使用前提,必须添加命名空间

<bean id="personId" class="com.itheima.f_xml.c_p.Person" 
        p:pname="禹太璞" p:age="22" 
        p:homeAddr-ref="homeAddrId" p:companyAddr-ref="companyAddrId">
    </bean>

    <bean id="homeAddrId" class="com.itheima.f_xml.c_p.Address"
        p:addr="DG" p:tel="东莞">
    </bean>
    <bean id="companyAddrId" class="com.itheima.f_xml.c_p.Address"
        p:addr="DG" p:tel="岛国">
    </bean>

Spring配置 bean.xml:
SpEL[了解]
对进行统一编程,所有的内容都使用value
property name=”” value=”#{表达式}”
#{123}、#{‘jack’} : 数字、字符串
#{beanId} :另一个bean引用
#{beanId.propName} :操作数据
#{beanId.toString()} :执行方法
#{T(类).字段|方法} :静态方法或字段

<!-- 
        <property name="cname" value="#{'jack'}"></property>
        <property name="cname" value="#{customerId.cname.toUpperCase()}"></property>
            通过另一个bean,获得属性,调用的方法
        <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
            ?.  如果对象不为null,将调用方法
    -->
    <bean id="customerId" class="com.itheima.f_xml.d_spel.Customer" >
        <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
        <property name="pi" value="#{T(java.lang.Math).PI}"></property>
    </bean>

Spring配置 bean.xml:
集合注入

<!-- 
        集合的注入都是给<property>添加子标签
            数组:<array>
            List:<list>
            Set:<set>
            Map:<map> ,map存放k/v 键值对,使用<entry>描述
            Properties:<props>  <prop key=""></prop>  【】

        普通数据:<value>
        引用数据:<ref>
    -->
    <bean id="collDataId" class="com.itheima.f_xml.e_coll.CollData" >
        <property name="arrayData">
            <array>
                <value>DS</value>
                <value>DZD</value>
                <value>屌丝</value>
                <value>屌中屌</value>
            </array>
        </property>

        <property name="listData">
            <list>
                <value>于嵩楠</value>
                <value>曾卫</value>
                <value>杨煜</value>
                <value>曾小贤</value>
            </list>
        </property>

        <property name="setData">
            <set>
                <value>停封</value>
                <value>薄纸</value>
                <value>关系</value>
            </set>
        </property>

        <property name="mapData">
            <map>
                <entry key="jack" value="杰克"></entry>
                <entry>
                    <key><value>rose</value></key>
                    <value>肉丝</value>
                </entry>
            </map>
        </property>

        <property name="propsData">
            <props>
                <prop key="高富帅"></prop>
                <prop key="白富美"></prop>
                <prop key="男屌丝"></prop>
            </props>
        </property>
    </bean>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值