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

    xiaoxiao2021-12-14  20

    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

    转载请注明原文地址: https://ju.6miu.com/read-962556.html

    最新回复(0)