IOC XML的Bean管理

IOC

管理Bean(xml)

注入数组,List,Map,Set

<?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="shuzu" class="com.hncj.beans.ShuZu">
            <property name="names">
                <array>
                    <value>小王</value>
                    <value>小黄</value>
                    <value>小李</value>
                    <value>小江</value>
                </array>
             </property>
        </bean>

    <!--     有LIst属性的注入    -->
    <bean id="list" class="com.hncj.beans.LIst">
        <property name="names">
            <list>
                <value>小王</value>
                <value>小黄</value>
                <value>小李</value>
                <value>小江</value>
            </list>
        </property>
    </bean>

    <!--     有Map属性的注入    -->
    <bean id="map" class="com.hncj.beans.Maps">
        <property name="name_age">
            <map>
                <entry key="小王" value="12"></entry>
                <entry key="小黄" value="22"></entry>
                <entry key="小李" value="32"></entry>
                <entry key="小江" value="42"></entry>
            </map>
        </property>
    </bean>

    <!--     有Set属性的注入    -->
    <bean id="set" class="com.hncj.beans.Sets">
        <property name="sets">
            <set>
                <value>小王</value>
                <value>小黄</value>
                <value>小李</value>
                <value>小江</value>
            </set>
        </property>
    </bean>

</beans>

对象注入LIst<其他对象>

<?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">

    <!-- student中嵌套list的course集合  -->
    <bean id="student" class="com.hncj.bean2.Student">
        <property name="course">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
                <ref bean="course3"></ref>
                <ref bean="course4"></ref>
            </list>
        </property>
    </bean>
    <!-- 创建多个course对象 -->
    <bean id="course1" class="com.hncj.bean2.Course">
        <property name="cname" value="Spring5课程"></property>
    </bean>
    <bean id="course2" class="com.hncj.bean2.Course">
        <property name="cname" value="Mybatis课程"></property>
    </bean>
    <bean id="course3" class="com.hncj.bean2.Course">
        <property name="cname" value="Spring MVC课程"></property>
    </bean>
    <bean id="course4" class="com.hncj.bean2.Course">
        <property name="cname" value="SpringBoot课程"></property>
    </bean>
</beans>

IOC操作Bean管理(FactoryBean)

  • Spring 分为两类Bean ,普通Bean和FactoryBean
    -普通Bean : 在XMl中定义的Bean类就是其返回类型
    -工厂Bean : 在XMl定义的Bean类可以和其发挥类型不一样

工厂Bean

  • 创建类,实现接口FactoryBean
public class MyBean implements FactoryBean<String> {
    // 定义返回的bean
    @Override
    public String getObject() throws Exception {
        return "Hello FactoryBean";
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}
<?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="mybean" class="com.hncj.bean3.MyBean"></bean>
</beans>
    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        String myBean = context.getBean("mybean", String.class);
        System.out.println(myBean);
    }

IOC操作Bean的作用域

  • 在Spring里,设置创建Bean的实例是单实例还是多实例
  • 在Spring中,创建的Bean实例默认是单实例(实例的地址值相同)
  • 在Spring的配置文件bean标签里有scope用于设置是单实例还是多实例
    1. 默认值 singleton 表示是单实例,Spring在加载配置文件时就创建
    <?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="mybean" class="com.hncj.bean3.MyBean" scope="singleton"></bean>
    </beans>
    
    1. prototype 表示是多实例,在context.getBean()才会去创建对象
    <?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="mybean" class="com.hncj.bean3.MyBean" scope="prototype"></bean>
    </beans>
    

IOC操作Bean的生命周期

  • 从对象创建到对象销毁的过程
bean的声明周期
  • 1.通过构造器创建Bean实例(无参构造器)
  • 2.为bean的属性设置值和对其他bean的引用(调用set方法)
  • 3.调用bean的初始化的方法(需要进行配置初始化方法)
  • 4.bean可以使用了(对象获取到了)
  • 5.当容器关闭时,调用bean的销毁的方法(需要进行配置销毁方法)
public class Person {
    private String name;

    public Person() {
        System.out.println("无参数构造器");
    }

    public Person(String name) {
        this.name = name;
    }

    public void setName(String name) {
        System.out.println("使用Set()设置对象属性");
        this.name = name;
    }

    // 创建初始化方法
    public void inintMethod(){
        System.out.println("初始化方法");
    }

    // 创建销毁方法
    public void destoryMethod(){
        System.out.println("销毁方法");
    }

    // 对象方法
    public void user(){
        System.out.println("使用对象的方法");
    }
}
<?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="person" class="com.hncj.bean4.Person" init-method="inintMethod" destroy-method="destoryMethod">
            <property name="name" value="刘倩"></property>
        </bean>
</beans>
    @Test
    public void test4(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        Person person = context.getBean("person", Person.class);
        person.user();
        // 销毁容器,也销毁了实例. 注意 ClassPathXmlApplicationContext 才有close方法
        context.close();
    }
无参数构造器
使用Set()设置对象属性
初始化方法
使用对象的方法
销毁方法

XML自动装配

  • 根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入
<?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标签属性 autowire 配置自动装配,可选参数有 byName根据名称自动注入 ByType根据类型自动注入-->
    <bean id="emp" class="com.hncj.bean5.Emp" autowire="byType">
<!--        <property name="dpt" ref="dpt"></property>-->
    </bean>
    <bean id="dpt" class="com.hncj.bean5.Dpt"></bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临水而愚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值