Spring入门篇之Bean的配置项及作用域

来源:互联网 发布:淘宝c店的运营案例 编辑:程序博客网 时间:2024/06/06 00:30

1.Bean的配置项: 
这里写图片描述

2.Bean的作用域: 
这里写图片描述

例子:

package com.wuyonghu.insert;public class BeanScope {    public void printMes(String meString){        System.out.println("参数的数据是:"+meString);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
<?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"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans                          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                          http://www.springframework.org/schema/context                          http://www.springframework.org/schema/context/spring-context-3.1.xsd                          http://www.springframework.org/schema/mvc                          http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <bean id="beanScope" class="com.wuyonghu.insert.BeanScope" scope="prototype"></bean></beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
@Test    public void testHello4() {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");        BeanScope bean = (BeanScope) context.getBean("beanScope");        BeanScope bean1 = (BeanScope) context.getBean("beanScope");        System.out.println(bean==bean1);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

从测试中的结果我们就可以看到是否该容器中存在的是同一个实例的bean

0 0
原创粉丝点击