spring属性注入

注入方式

set方法注入这里写图片描述
构造函数注入这里写图片描述
p名称空间注入这里写图片描述
spel注入(#表示理解为引用)这里写图片描述

练习代码:

    <!-- 第二天 set注入 index:索引 ref:引用对象 type:参数类型-->
    <bean name="person" class="com.spring.bean.Person">
        <constructor-arg name="name" value="12" index="0" type="java.lang.Integer"></constructor-arg>
        <constructor-arg name="car" ref="car" index="1" ></constructor-arg>
    </bean>
    <bean name="car" class="com.spring.bean.Car">
        <property name="name" value="兰博基尼"></property>
        <property name="color" value="绿色"></property>
    </bean>
    <!-- p命名空间 -->
    <bean name="person1" class="com.spring.bean.Person" p:name="Alan2" p:age="18" p:car-ref="car"></bean>
    <!-- spel springEL表达式注入 -->
    <bean name="person2"  class="com.spring.bean.Person">
        <property name="name" value="#{person1.name}"></property>
        <property name="age" value="#{person1.age}"></property>
        <property name="car" ref="car"></property>
    </bean>

复杂类型注入

数组这里写图片描述这里写图片描述
List这里写图片描述这里写图片描述
Map这里写图片描述
Properties这里写图片描述

练习代码:
applicationContext.xml

    <!-- 复杂类型注入 -->
    <bean name="colleactionsBean" class="com.spring.bean.ColleactionsBean">
        <!-- 集合中只有一个值 -->
        <!-- <property name="arraylist" value="hello"></property> -->
        <!-- 集合中有多个值 -->
        <property name="array">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>alanList1</value>
                <value>alanList2</value>
                <value>alanList3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="alanMap" value="12"></entry>
                <entry key="alan1Map" value="13"></entry>
                <entry key="alan2Map" value="14"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="prop1">p1</prop>
                <prop key="prop2">p2</prop>
                <prop key="prop3">p3</prop>
            </props>
        </property>
    </bean>

ColleactionsBean.java

package com.spring.bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class ColleactionsBean {
    private int[] array;
    private List list;
    private Map map;
    private Properties properties;
    public int[] getArray() {
        return array;
    }
    public void setArray(int[] array) {
        this.array = array;
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public Map getMap() {
        return map;
    }

    @Override
    public String toString() {
        return "ColleactionsBean [array=" + Arrays.toString(array) + ", list=" + list + ", map=" + map + ", properties="
                + properties + "]";
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

}

TestDemo.java

package com.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.bean.User;

public class TestDemo {
    private  ClassPathXmlApplicationContext context;
    @Test
    public void testDemo() throws InterruptedException{
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        /*scope=prototype的javaBean在创建容器时候不会被创建*/
        User bean = (User)context.getBean("user");
        //User bean1 = (User) context.getBean("user");
        //System.out.println(bean==bean1);
        bean.setName("DreamZuora");
        bean.setAge(18);
        System.out.println(bean);
        /*单例对象可以销毁,多例对象不会被销毁*/
        context.close();
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值