您的位置:首页 > 编程语言 > Java开发

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

2016-12-02 10:58 387 查看
1.Bean的配置项:



2.Bean的作用域:



例子:

package com.wuyonghu.insert;

public class BeanScope {
public void printMes(String meString){
System.out.println("参数的数据是:"+meString);
}
}


<?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>


@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);
}


从测试中的结果我们就可以看到是否该容器中存在的是同一个实例的bean
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: