JavaWeb学习笔记-spring-04-ioc-bean装载

Bean装载

//id:bean名称
//class:bean类名
//id也可以使用name,id命名有规范,name没有
//id与name可以定义多个,使用逗号,分号或空格分隔
//不允许出现两个相同id的bean,可以出现相同name的bean,后者会覆盖前者,所有应尽量使用id
//id与name未指定,自动使用全限定类名作为Bean名称
<bean id="foo" class="com.smart.bean.Foo"/>

如果出现多个相同匿名bean

<bean class="com.smart.Car"/>
<bean class="com.smart.Car"/>
<bean class="com.smart.Car"/>
getBean("com.smart.Car")//第一个bean
getBean("com.smart.Car#1")//第二个bean
getBean("com.smart.Car#2")//第三个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.xsd"> 
    <bean id="car" class="com.smart.Car"></bean>
</beans>

依赖注入

属性Setter注入

public class Car2 {
    //javabean属性必须满足变量前面两个字母大小写一致,否则spring会出错
    private int maxSpeed;
    private String brand;
    private double price;

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
<?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.xsd">
    <bean id="car" class="com.smart.Car2">
        <property name="maxSpeed">
            <value>200</value>
        </property>
        <property name="brand">
            <value>红旗</value>
        </property>
        <property name="price">
            <value>200000</value>
        </property>
    </bean>

</beans>

构造器注入

public class User{
    private Stirng name;
    public User(String name){
        this.name = name;
    }
}

配置Bean

<bean name="user" class="xxx.User">
    <constructor-arg>
        <value>无语</value>
    </constructor-arg>
    <constructor-arg>
        <value>30</value>
    </constructor-arg>
    <constructor-arg>
        <value></value>
    </constructor-arg>
</bean>

construct-arg属性
index
type

<bean id="car" class="com.smart.Car">
    <!--索引从0开始-->
    <constructor-arg index="0" value="xxx"/>
    <constructor-arg index="1" value="xxx"/>
    <constructor-arg index="2" value="xxx"/>
</bena>
<bena id="car" class="com.smart.Car">
    <constructor-arg index="0" type="java.lang.String">
        <value>xxx</value>
    </constructor-arg>
    <constructor-arg index="1" type="java.lang.String">
        <value>xxx</value>
    </constructor-arg>
    <constructor-arg index="2" type="int">
        <value>200</value>
    </constructor-arg>
</bean>

循环依赖








引用其他bean




注入参数详解

字面值

<bena id="car" class="com.smart.Car">
    <property name="maxSpeed">
        <value>200</value>
    </property>
    <property name="brand">
        <!-- <![CDATA[]]处理特殊字符串,防止破坏xml结构>-->
        <value>![CDATA[红旗&CA72]]</value>
    </property>
</bean>

引入其他bean

<!--car bean-->
<bean id="car" class="com.smart.Car"/>
<bean id="boss" class="com.smart.Boss">
    <property name="car">
        <ref bean="car"></ref>
    </property>
</bean>
  • ref
    • bean //引用同一容器或父容器的bean
    • local //引用同一配置文件中的bean
    • parent //引用父容器中的bean
<!--在父容器中定义car-->
<bean id="car" class="com.smart.Car">
    <property name="brand" value="xxx"/>
    <property name="maxSpeed" value="200"/>
    <property name="price" value="2000"/>
</bean>
<!--定义和父容器car相同id的bean-->
<bean id="car" class="com.smart.Car">
    <property name="brand" value="xxx">
    <property name="maxSpeed" value="200"/>
    <property name="price" value="2000"/>
</bean>
<bean id="boss" class="com.smart.Boss">
    <property name="car">
        <!--引用父容器car,使用<ref bean="car"/>将引用本容器的car-->
        <ref parent="car"/>
    </propery>
</bean>
//父容器
ClassPathXmlApplicationContext pFactory = new ClassPathXmlApplicationContext(new String[]{"com/smart/beans.xml"});
//指定pFactory为该容器父容器
ApplicationContext factory = new ClassPathXmlApplicationContext(new String[]{com/smart/beans2.xml},pFactory);
Boss boss = (Boss)factory.getBean("boss");
System.out.println(boss.getCar().toString());

集合类型属性

list
<bean id="boss" class="com.smart.Boss">
    <property name="favorites">
        <list>
            <!--可以通过value注入字面值,也可以通过ref注入bean-->
            <value>xxx</value>
            <value>xxx</value>
            <value>xxx</value>
        </list>
    </property>
</bean>
map
<bean id="boss" class="com.smart.Boss">
    <property name="jobs">
        <map>
            <entity>
                <key><value>xxx</value></key>
                <value>xxx</value>
            </entity>
            <entity>
                <key><value>xxx</value></key>
                <value>xxx</value>
            </entity>
        </map>
    </property>
</bean>
<!--map键和值是对象-->
<entity>
    <key><ref bena="keyBean"></key>
    <ref bena="valueBean"/>
</entity>
properties
<!--properties只能使用字符串,没有value子元素标签-->
<bean id="boss" class="com.smart.Boss">
    <property name="mails">
        <props>
            <prop key="jobMail">john-office@smart.com</prop>
            <prop key="lifeMail">john-life@smart.com</prop>
        </props>
    </property>
</bean>

通过util命名空间配置集合类型bean

xmlns:util="http://www.springframework.org/schema/util"
xsi:scheamLocation="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-.1.xsd"
<!--list类型-->
<util:list id="favoriteList1" list-class="java.util.LinkedList"
    <value>xxx</value>
    <value>xxx</value>
    <value>xxx</value>
</uil:list>
<!--set类型,可以使用set-class指定set实现类-->
<util:set id="favoriteSet1">
    <value>xxx</value>
    <value>xxx</value>
    <value>xxx</value>  
</util:set>
<!--map类型,可以使用map-class指定map实现类-->
<util:map id="emails">
    <entry key="AM" value="xxx"/>
    <entry key="PM" value="xxx">
</util:map>

p元素简化配置

<xmlns:p="http://www.springframework.org/schema/p">
<bean id="car" class="com.smart.Car"
    p:brand="xxx"
    p:maxSpeed="xxx"
    p:price="xxx"/>
p:<属性名>="xxx"
p:<属性名>_ref="xxx"

自动装配

元素自动装配类型属性autowire=”<自动装配类型>”

自动装配类型|说明

byName|根据名称自动匹配
byType|根据类型自动匹配
constructor|与byType类似,只针对构造函数注入
autodetect|根据bean自省机制决定采用byType还是constructor

中的default-autowire属性可以配置全局自动匹配,默认为no,表示不自动装配,其他可选配置为byName,byType,constructor,autodetect
中定义的自动装配策略会被自动装配策略覆盖

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值