spring IOC配置 bean属性的配置 scope属性的配置

13 篇文章 0 订阅

spring IOC配置 bean属性的配置

bean属性 就是bean标签
作用:定义spring中的资源,用bean标签定义的资源就会受到spring的控制。
格式

<beans>
         <bean/>
</beans>

bean标签的属性

<bean id="beanId" name="beanName1,beanName2" class="com.itheima.service.impl.UserServiceImpl"></bean>

id:bean的名称,通过id值获取bean
class:bean的类型
name:bean的名称,可以通过bean的name属性获取bean,
name属性一次可以设置多个值,中间用 , 号分割
用于给bean起别名,供多人使用

public class UserApp {
    public static void main(String[] args) {
        // 加载配置文件
     ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
     //获取资源
        UserService userService = (UserService)ctx.getBean("userService");
        //调用userService接口的save
        userService.save();
    }
}

ctx.getBean(“userService”);
在里写id值就是通过id获取这里的class属性
在里写name值就是通过name获取这里的class属性
getBean(“beanName1”); 或者是getBean(“beanName2”);

如果getBean(“beanName3”);
这里的参数是beanName3,
但在bean标签中,id与name的值都没有beanName3名称
这样就会在控制台报出
NO bean named,错误,意思是它先去找id的值,与id值不一致,
就会去找name的值,两个属性都没有这个值就会报出这个错误。

scope属性的配置

这是在bean标签中的 scope属性
作用是:定义bean的作用范围
格式:

scope的取值有六个
singleton:设定创建出的对象保存在spring容器中,是一个单例的对象
prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象
request,session,application,websocket:设定创建出的对象放五在web容器对应的位置

<!--bean的scope属性-->
    <bean id="userService" scope="singleton" class="com.itheima.service.impl.UserServiceImpl"/>

这么写创建出的对象是单列对象

// 加载配置文件
 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 //获取资源
    UserService userService1 = (UserService)ctx.getBean("userService");
    UserService userService2 = (UserService)ctx.getBean("userService");
    UserService userService3 = (UserService)ctx.getBean("userService");
    //调用userService接口的save
    //userService.save();
    System.out.println(userService1);
    System.out.println(userService2);
    System.out.println(userService3);
    System.out.println(userService1==userService2);

这么写在控制台输入的信息

com.itheima.service.impl.UserServiceImpl@604ed9f0
com.itheima.service.impl.UserServiceImpl@604ed9f0
com.itheima.service.impl.UserServiceImpl@604ed9f0
true

表明对象的地址都相同,都是同一个对象

<!--bean的scope属性-->
    <bean id="userService" scope="prototype" class="com.itheima.service.impl.UserServiceImpl"/>

这么写创建出的对象是非单列对象

// 加载配置文件
 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 //获取资源
    UserService userService1 = (UserService)ctx.getBean("userService");
    UserService userService2 = (UserService)ctx.getBean("userService");
    UserService userService3 = (UserService)ctx.getBean("userService");
    //调用userService接口的save
    //userService.save();
    System.out.println(userService1);
    System.out.println(userService2);
    System.out.println(userService3);
    System.out.println(userService1==userService2);

这么写在控制台输入的信息

com.itheima.service.impl.UserServiceImpl@3e6fa38a
com.itheima.service.impl.UserServiceImpl@66a3ffec
com.itheima.service.impl.UserServiceImpl@77caeb3e
false

表明对象的地址都不相同,不是同一个对象

scope属性是用来控制创建出来的对象是单列还是非单列的
singleton对象是单列 创建出来的对象地址相同
prototype对象是非单列 创建出来的对象地址不同

scope属性的默认值是singleton
不用非单列 就不用写scope属性

单列的是在加载spring容器时创建,非单列的是在获取bean的时候创建对象的。bean是spring内的一种容器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

普希托夫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值