Spring框架中bean标签的常见配置

Spring框架中bean标签的常见配置

在spring基础使用中明确配置Spring的xml文件中bean标签内id/name和class是必须的,除去这两个还有以下几个

1.Scope配置

配置方法
<bean name="myStudent" class="com.example.Student" scope="singleton" ></bean>

  • singleton : 单例【默认值】。表示此对象在Spring容器中仅会存在唯一的一个实例。
  • prototype : 每实例。表示从容器中取出对象时,总会创建一个新的对象出来。
  • request (web环境下): 同一个请求中,拿到的bean是同一个。 【实际开发中放入请求域】
  • session(web环境下): 同一个会话中,拿到的bean是同一个。【实际开发中放入session域】
  • globalSession(portlet环境下)

特点:

  • 当bean的范围是 singleton 时, 此bean的对象是在Spring容器启动时创建出来。
  • 当bean的范围是 prototype时 ,是从容器中获取时才会创建bean的实例。

2.生命周期方法的配置

  • 形式都是无参的方法。
  • 通过bean标签的 init-method 和 destroy-method 属性来分别指定方法名。

配置文件:

<bean name="myStudent" class="com.example.Student" scope="singleton" init-method="aaa" destroy-method="bbb"></bean>

实类代码:

public class Student {
    private String name;
    private Integer age;
    
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void aaa(){
        System.out.println("实例创建");
    }
    public void bbb(){
        System.out.println("实例销毁");
    }

}

测试类代码:
原始的ApplicationContext没有close()方法,需要使用 ClassPathXmlApplicationContext

public class Demo1 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =null;
        context = new ClassPathXmlApplicationContext("com/example/demo1.xml");
        Student myStudent = context.getBean("myStudent", Student.class);
        
        //这里要正常关闭spring容器,才可以显示销毁方法
        context.close();

    }
}

运行显示结果:
在这里插入图片描述

3.Bean的简单依赖注入

简单依赖:基本数据类型及其包装类型、 String类型、其它Bean实例。

3.1通过构造方法参数

调用类代码:

public class Demo2 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = null;
        context = new ClassPathXmlApplicationContext("com/example/demo1.xml");
        Bean1 bean1 = context.getBean("bean1", Bean1.class);
        System.out.println(bean1.getNum());
    }
}

需要传递参数并被spring管理的类

public class Bean1 {
    private Integer num;
    private String string;
    private Boolean aBoolean;
    private OtherBean otherBean;

    public Bean1(Integer num, String string, Boolean aBoolean,OtherBean otherBean) {
        this.num = num;
        this.string = string;
        this.aBoolean = aBoolean;
        this.otherBean = otherBean;
    }

    public Integer getNum() {
        return num;
    }

    public String getString() {
        return string;
    }

    public Boolean getaBoolean() {
        return aBoolean;
    }

    public OtherBean getOtherBean() {
        return otherBean;
    }
}

OtherBean类

public class OtherBean {
    public OtherBean() {
        System.out.println("OtherBean类被创建");
    }
}

xml配置

<?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 name="otherBean" class="com.example.di.OtherBean" ></bean>
    
    <bean name="bean1" class="com.example.di.Bean1">
    	//参数传递
        <constructor-arg name="num" value="100"></constructor-arg>
        <constructor-arg name="string" value="sds"></constructor-arg>
        <constructor-arg name="aBoolean" value="true"></constructor-arg>
        //这里需要传递的是类时需要使用ref,且该类被spring管理
        <constructor-arg name="otherBean" ref="otherBean"></constructor-arg>
    </bean>
</beans>

显示结果:
在这里插入图片描述
xml参数传递简化:
xml beans标签中多了xmlns:c="http://www.springframework.org/schema/c"

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="otherBean" class="com.example.di.OtherBean" ></bean>

    <bean name="bean1" class="com.example.di.Bean1" c:num="100" c:string="sds" c:aBoolean="true" c:otherBean-ref="otherBean"></bean>
</beans>

最终显示结果一致

3.2 setter方法传递参数

使用 property 子标签来进行设置
name 属性值 对应的是 setter方法对应的 属性名
如果对应的值是简单类型,就用 value属性。
如果对应的值是其它bean,就用 ref 属性。
demo1.xml配置文件:

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="otherBean" class="com.example.di.OtherBean" ></bean>
    <bean name="bean2" class="com.example.di.Bean2">
        <property name="num" value="789"></property>
        <property name="string" value="sds"></property>
        <property name="aBoolean" value="true"></property>
        <property name="otherBean" ref="otherBean"></property>
    </bean>
</beans>

Bean2类

public class Bean2 {
    private Integer num;
    private String string;
    private Boolean aBoolean;
    private OtherBean otherBean;

    public Integer getNum() {
        return num;
    }

    public String getString() {
        return string;
    }

    public Boolean getaBoolean() {
        return aBoolean;
    }

    public OtherBean getOtherBean() {
        return otherBean;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public void setString(String string) {
        this.string = string;
    }

    public void setaBoolean(Boolean aBoolean) {
        this.aBoolean = aBoolean;
    }

    public void setOtherBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }
}

Demo2类

public class Demo2 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = null;
        context = new ClassPathXmlApplicationContext("com/example/demo1.xml");
        Bean2 bean2 = context.getBean("bean2", Bean2.class);
        System.out.println(bean2.getNum());
    }
}

显示结果:
在这里插入图片描述
spring简化配置方式:
xml中多了
xmlns:p="http://www.springframework.org/schema/p"

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="otherBean" class="com.example.di.OtherBean" ></bean>
 
    <bean name="bean2" class="com.example.di.Bean2" p:num="789" p:string="sds" p:aBoolean="true" p:otherBean-ref="otherBean"></bean>
</beans>

4.Bean的复杂依赖注入

数组、集合(List、Set)、Map、Properties 。
以构造方法为例
想要注入并交给spring管理的类Bean3

public class Bean3 {
    private Integer[] pro1;
    private List pro2;
    private Set pro3;
    private Map<String,String> pro4;
    private Properties pro5;

    public Bean3(Integer[] pro1, List pro2, Set pro3, Map<String, String> pro4, Properties pro5) {
        this.pro1 = pro1;
        this.pro2 = pro2;
        this.pro3 = pro3;
        this.pro4 = pro4;
        this.pro5 = pro5;
    }

    public Integer[] getPro1() {
        return pro1;
    }

    public List getPro2() {
        return pro2;
    }

    public Set getPro3() {
        return pro3;
    }

    public Map<String, String> getPro4() {
        return pro4;
    }

    public Properties getPro5() {
        return pro5;
    }
}

测试类Demo2

public class Demo2 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = null;
        context = new ClassPathXmlApplicationContext("com/example/demo1.xml");
        Bean3 bean3 = context.getBean("bean3", Bean3.class);
        System.out.println(bean3.getPro2());
    }
}

xml配置
复杂类型数据的传入需要使用<constructor-arg>标签
对于不同的数据类型使用不同的标签

  • 数组 <array>
  • 列表 <list>
  • 集合 <set>
  • map <map>&<entry>
  • properties <props>&<prop>
<?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:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="bean3" class="com.example.di.Bean3">
        <constructor-arg name="pro1">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </constructor-arg>
        <constructor-arg name="pro2">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </constructor-arg >
        <constructor-arg name="pro3">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </set>
        </constructor-arg>
        <constructor-arg name="pro4">
            <map>
                <entry key="k1" value="v1"></entry>
                <entry key="k2" value="v2"></entry>
                <entry key="k3" value="v3"></entry>
            </map>
        </constructor-arg>
        <constructor-arg name="pro5">
            <props>
                <prop key="">对应的值</prop>
            </props>
        </constructor-arg>
    </bean>
</beans>

显示结果:
在这里插入图片描述
在map中也可以指定其他类
以setter方法为例,但是map泛型指定必须与配置一致

        <property name="pro4">
            <map>
                <entry key="123" value-ref="bean2"></entry>
            </map>
        </property>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值