spring 第2天,bean作用域,自动注入集合

本文详细介绍了Spring框架中Bean的配置方式,包括全局属性设置、构造函数注入、属性值注入、集合属性注入等,并探讨了Bean的作用域、别名及自动装配策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sping容器中的Bean
<!---beans的全局属性设置-->
<beans>
default-lazy-init:指定该beans下的所有bean 默认延迟初始化行为
default-merge:指定beans下的所有bean默认的mege行为
default-autowire:指定beans下的所有bean的默认自动装配行为
default-autowire-candidates:指定beans下所有bean默认是否为自动装配的候选bean
default-init-method:指定beans下的所有bean默认初始化的方法
default-destory-method:指定beans下的所有bean默认 回收方法
</beans>
<!---去掉default 后就是bean的属性,这样就可以设置单个bean的属性-->

<bean id="bean1" class="lee.test1">
    <constructor-args values="hello"/>
    <constructor-args values="23"/>
</bean>

<!--上面的bean可以配置构造函数为两个string,或者 一个 string 一个 int
这种情况下可以配置 -->

<constructor-args values="23" type="int"/>



说说Bean的别名问题,name,alias
<!--定义一个能够获取spring容器的bean  -->
	<bean id="myContent" name="CH,CN" class="cn.sh.springmvc.applicationContextAware.MyContent" lazy-init="true">
	</bean>
	<alias name="CH" alias="ZG"/>
	
	<alias name="chinese" alias="ZH"/>
	<!-- 别名之间还可以传承 -->
	<alias name="ZH" alias="HH"/>


//测试spring name alias 别名的 创建对象
@Test
	public void test3() {
		ApplicationContext act=new ClassPathXmlApplicationContext("classpath*:applicationContent.xml");
		MyContent p=act.getBean("chinese",MyContent.class);
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

                p=act.getBean("ZH",MyContent.class);
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

                p=act.getBean("CH",MyContent.class);
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

                p=act.getBean("ZG",MyContent.class);
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

                p=act.getBean("CN",MyContent.class);
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

                p=act.getBean("HH",MyContent.class);
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

	}


bean的作用域
<!--
singleton:默认值 单例模式
prototype:原型 每次都会创建对象
下面只在web中采用用 
request (prototype) :比如struts2中 action
session
global session: application
 -->
<!--原型  -->
	<bean id="userService2" class="cn.sh.springmvc.service.user.UserService" scope="prototype">
		<constructor-arg name="age" value="18" type="int"/>
                 <!--基本类型值  -->
		<property name="name" value="wawa"/>
		<property name="age" value="1"/>
                <!--douvle值  -->
		<property name="salary">
                     <value>2165.5</value>   
                </property>

                <!--注入嵌套bean 不让spring容器访问到  -->
                 <property name="userdao">
			<bean class="cn.sh.springmvc.dao.UserDAO"/>
		</property>
   
                <property name="userdao1" ref="userdao"/>
                
                 <property name="userdao2">
			<ref local="userdao"/>
		</property>

	</bean>


自动注入Bean
<!-- 此Bean不参与自动装配 -->
	<bean id="userdao" class="cn.sh.springmvc.dao.UserDAO" autowire-candidate="false"/>
	
	<bean id="userService3" class="cn.sh.springmvc.service.user.UserService" autowire="default">
		<property name="userdao" ref="userdao"/>
	</bean>
	<!-- 默认不在用自动 注入 -->
	<bean id="userService4" class="cn.sh.springmvc.service.user.UserService" autowire="no">
		<property name="userdao">
			<ref local="userdao"/>
		</property>
	</bean>
	
	<!-- 根据 setName()-->	
	<bean id="userService5" class="cn.sh.springmvc.service.user.UserService" autowire="byName">
	</bean>
	
	<!-- 根据 setName(Type type) 如果spring 容器中有多个 Type 的bean 会出异常 -->	
	<bean id="userService6" class="cn.sh.springmvc.service.user.UserService" autowire="byType">
	</bean>

	<!-- 根据 类中的构造函数进行装配 -->	
	<bean id="userService8" class="cn.sh.springmvc.service.user.UserService" autowire="constructor">
	</bean>
	
	<!--用于spring2.5 ,spring3.0  交给spring自动帮我我们抉择 是采用 constructor 或者 byType
	<bean id="userService9" class="cn.sh.springmvc.service.user.UserService" autowire="autodetect">
	</bean>
	-->


给bean注入集合属性

<bean id="chinese" class="cn.sh.springmvc.model.Chinese">
		<property name="schools">
			<list>
				<value>小学</value>
				<value>中学</value>
				<value>大学</value>
			</list>
		</property>
		<property name="scores">
			<map>
				<entry key="数学"><value>87</value></entry>
				<entry key="英语" value="89"/>
				<entry key="语文" value="82"/>
			</map>
		</property>
		<property name="phaseAxes">
			<map>
				<entry key="原始社会" value-ref="stoneAxe"/>
				<entry key="农业社会" value-ref="steelAxe"/>
			</map>
		</property>
		<!-- properties 设置 -->
		<!-- 
		<property name="health">
			<props>
				<prop key="血压">正常</prop>
				<prop key="身高">175</prop>
			</props>
		</property> -->
		<!-- properties 的另一种 设值方法  key value 只能是 英文和 数组-->
		<property name="health">
			<value>
				pressure=normal
				height=175
			</value>
		</property>
		<property name="axes">
			<set>
				<value>普通的字符串</value>
				<bean class="cn.sh.springmvc.model.SteelAxe"/>
				<ref local="stoneAxe"/>
			</set>
		</property>
		<property name="books">
			<list>
				<value>Struts2 权威指南</value>
				<value>轻量级JavaEE企业级应用</value>
				<value>疯狂java讲义</value>
			</list>
		</property>
		<property name="favourity">
			<array>
				<value>Struts2 权威指南</value>
				<value>轻量级JavaEE企业级应用</value>
				<value>疯狂java讲义</value>
			</array>
		</property>
	</bean>


Bean的组合属性赋值
<!--组合属性赋值  -->
	<bean id="userChild1" parent="userParent">
		<!--组合属性赋值的时候,只有最后一个属性可以为null,其他属性都不能为null  
			aa.bb.cc.dd : aa bb cc 都不能为null dd可以为null
		-->
		<property name="chinese.books">
			<array>
				<value>javaEE</value>
				<value>JS</value>
				<value>Oracle</value>
			</array>
		</property>
	</bean>


spring对Bea没有特殊要求,但是我们建议满足一下几个原则

1.尽量为每个Bean提供无参数构造函数
2.接受构造注入的Bean,应该提供构造函数
3.接受设值注入的bean,要提供setting方法,但并不要提供getter方法
4.Bean的id命名首字母尽量小写
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值