02第二章Spring Bean管理进阶(import、生命周期、list map 数组 properties注入数据 、注解开发)

一、bean标签和import标签

1.bean标签中的id属性和name属性

  • id属性 唯一标识 Ioc容器中id属性不可重复 否则报错

  • name属性 spring4.0版本后 与id属性一样

<bean id="person" class="com.xqh.pojo.Person" ></bean> 
<bean name="person" class="com.xqh.pojo.Person" ></bean>

2.Bean的作用范围和生声周期

bean标签中 scope属性 控制Bean的作用范围

  • singleton: 单例对象,被标识为单例的对象在Spring容器中只会存在一个实体。(注册容器默认值scope=singleton略写)
  • prototype: 多利原型,被标识为多利的对象,每次需要获取对象时才会创建,每次创建的都是新的对象。
  • request:Web环境下,对象与Request生命周期一致。
  • Session环境下,对象与Session生命周期一致。
<bean id="person" class="com.xqh.pojo.Person" scope="prototype"></bean> 

3.import标签

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    
    <import resource="com/xqh/connecteDataBase/applicationContext-conMysql.xml"/><!-- 数据库连接-->

    <import resource="com/xqh/pojo/book/applicationContext-book.xml"/>
    <import resource="com/xqh/pojo/student/applicationContext-student.xml"/>
    <import resource="com/xqh/pojo/service/applicationContext-service.xml"/>
    <import resource="com/xqh/pojo/type/applicationContext-data.xml"/>

</beans>

二、复杂类型注入

    private String str;
    private int[] intArr; //数组

    private List<String> strlist;//list集合
    private Map<String,String> strMap;//map集合
    private Properties properties;//数据库连接信息
	
	//文章此处省略get()、set()方法(在类中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="arr" class="com.xqh.pojo.type.Data">
        <property name="intArr" >   <!-- name属性值为 类中set方法的名字 -->
            <list> <!-- list标签 数组、list注入 -->
                <value>1</value>
                <value>2</value>
                <value>3</value>
                <value>4</value>
            </list>
        </property>
    </bean>

    <bean id="list" class="com.xqh.pojo.type.Data">
        <property name="strlist" >
            <list> <!-- list标签 数组、list注入 -->
                <value>xqh</value>
                <value>yuyan</value>
                <value>120q</value>
                <value>120q</value>
            </list>
        </property>
    </bean>

    <bean id="map" class="com.xqh.pojo.type.Data">
        <property name="strMap" >
            <map>    <!-- map  注入-->
                <entry key="xqh" value="18"/>
                <entry key="yuyan" value="18"/>
                <entry key="acer" value="18"/>
            </map>
        </property>
    </bean>
    
        <bean id="conMysql" class="com.xqh.connecteDataBase.ConMysql">
        <property name="properties">	<!-- properties 注入 -->
            <props>
                <prop key="driver" >com.mysql.jdbc.Driver</prop>
                <prop key="url">jdbc:mysql://localhost:3306</prop>
                <prop key="userName">root</prop>
                <prop key="password">root123</prop>
            </props>
        </property>
    </bean>
</beans>
/*
测试类 复杂类型   数组、list集合、map集合注入
 */
@Test
public void type01(){
    Data data = (Data) SpringUtil.app.getBean("arr");//数组[]
    for (int i : data.getIntArr()) {
        System.out.println(i);
    }

    Data list = (Data)SpringUtil.app.getBean("list");//lsit
    for (String s : list.getStrlist()) {
        System.out.println(s);
    }

    Data map = (Data)SpringUtil.app.getBean("map");//map
    for (Map.Entry<String, String> m : map.getStrMap().entrySet()) {
        System.out.println("key:"+m.getKey()+"\t"+"value:"+m.getValue());
    }

    //数据库连接<!-- properties 注入 -->
    ConMysql conMysql = (ConMysql)SpringUtil.app.getBean("conMysql");
    for (Map.Entry<Object, Object> m : conMysql.getProperties().entrySet()) {
        System.out.println("key:"+"\t"+m.getKey()+"\t\t"+"value:"+"\t"+m.getValue());
    }
}

三、通过注解方式配置Bean对象

  • @Component讲对象注册到容器中 (默认id名=类名小写)
  • @Value值类型注入
  • @Autowired引用类型注入
  • @Scope设置对象作用域
  • @PostConstruct 设置初始化方法init()
  • @PreDestroy 设置销毁前方法destroy()
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.xqh"></context:component-scan> <!-- 需要扫描包来开启注解! -->
</beans>
package com.xqh.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//@Component(value = "person") 自定义bean的id值
@Component
@Scope("singleton")
public class Person {
    @Value("xqh")
    private String name;
    @Value("18")
    private String age;

    @Autowired//自动引用类
    private Car car;
    
    @PostConstruct
    public void init(){
        System.out.printLn("初始化方法被调用");
    }
    
    @PreDestroy
    public void destroy(){
        System.out.printLn("销毁方法被调用");
    }
    
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", car='" + car + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值