Spring入门教程笔记2 - bean的依赖注入及作用域

7 篇文章 0 订阅

Spring入门教程笔记2 - bean的定义及作用域

简介

我们今天来认识一下Spring中最重要的一个知识点 bean, bean是spring核心关键点,一般常用于存在XML文件内进行定义,以及依赖注入,话不多说我们直接开始。

bean

接下来我们开始创建bean

创建一个UserBean.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 class="cn.marinda.resturants.User" name="userBean" >
    </bean>
    
</beans>

接下来开始创建一个User类


public class User {
    private String name;
    private int age;
    public User()
    {

    }

    public User(String name,int age)
    {
        this.name = name;
        this.age = age;
    }

    public void print()
    {
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}

作用域

可以通过作用域来设置是否每次返回一个新的bean实例或者只返回一个bean

列举一下Spring的bean作用域

  • singleton
  • prototype
  • request
  • session
  • global

接下来的学习我们着重介绍前两种

singleton

是默认的作用域,在没有scope设置前,是用来返回单个bean实例

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 class="cn.marinda.resturants.User" name="userBean"  scope="singleton">

    </bean>

</beans>

main方法

    public static void main(String[] args) {
        //创建ClassPathXmlApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
        User user = (User)context.getBean("userBean");
        user.setAge(14);
        user.print();

        User user2 = (User)context.getBean("userBean");
        user2.print();
    }

结果

姓名:null
年龄:14
姓名:null
年龄:14

prototype

设置之后每次会返回一个新的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 class="cn.marinda.resturants.User" name="userBean"  scope="prototype">

    </bean>

</beans>

main方法

    public static void main(String[] args) {
        //创建ClassPathXmlApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
        User user = (User)context.getBean("userBean");
        user.setAge(14);
        user.print();

        User user2 = (User)context.getBean("userBean");
        user2.print();
    }

结果

姓名:null
年龄:14
姓名:null
年龄:0

依赖注入

spring依赖注入有设值函数注入,和构造函数注入

  • property
  • constructor-arg

还是以前面User类以及UserBean.xml为例

代码实例

property

UserBean.xml

    <bean class="cn.marinda.resturants.User" name="userBean"  scope="prototype">
        <property name="name" value="小明"></property>
        <property name="age" value="16"></property>
    </bean>

结果

姓名:小明
年龄:14
姓名:小明
年龄:16

constructor-arg

接下来看看 constructor-arg 依赖注入方式
UserBean.xml

    <bean class="cn.marinda.resturants.User" name="userBean"  scope="prototype">
        <constructor-arg name="age" value="16"></constructor-arg>
        <constructor-arg name="name" value="小红"></constructor-arg>
    </bean>

结果

姓名:小红
年龄:16
姓名:小红
年龄:16

结束语

  • spring学习笔记就到这里了,如有不足欢迎补充
  • 谢谢观看
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值