spring-bean的配置

本文介绍了Spring框架中IOC(控制反转)与DI(依赖注入)的概念,并通过具体示例展示了如何利用XML配置文件来实现bean的创建与管理,包括属性注入、构造器注入、bean引用及集合类型的配置。

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

IOC和DI:

> IOC (Inversion of Control):反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式。

> DI (Dependency Injection):IOC 的另一种表述方式;即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接


一、基于想xml形式配置bean

创建一个computer对象

package com.spring.bean;

public class Computer {
	
	private String brand;
	private String cpu;
	private int mermory;
	private double price;
	
	public Computer(String brand, String cpu, int mermory) {
		super();
		this.brand = brand;
		this.cpu = cpu;
		this.mermory = mermory;
	}
	
	public Computer(String brand, String cpu, double price) {
		super();
		this.brand = brand;
		this.cpu = cpu;
		this.price = price;
	}
	
	public void setBrand(String brand) {
		this.brand = brand;
	}

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public void setMermory(int mermory) {
		this.mermory = mermory;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
	

	@Override
	public String toString() {
		return "Computer [brand=" + brand + ", cpu=" + cpu + ", mermory=" + mermory + ", price=" + price + "]";
	}

	public Computer() {
		super();
	}
	
	
}


1、配置bean的属性

1)属性注入

2)使用构造器配合

<?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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 
		配置bean
		class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求bean中一定要有无参的构造器
	 -->
	 
	<!-- 属性注入 -->
	<bean id="computer" class="com.spring.bean.Computer">
		<property name="brand" value="lenovo"></property>
		<property name="cpu" value="core i3"></property>
		<property name="mermory" value="200"></property>
		<property name="price" value="400.0"></property>
	</bean>
	
	<!-- 使用构造器来配置bean的属性 -->
	<!-- 使用构造器注入属性的值可以指定参数的位置和参数的类型,以区分重载的构造器 -->
	<bean id="computer2" class="com.spring.bean.Computer">
		<constructor-arg value="dell" index="0"></constructor-arg>
		<constructor-arg value="core i5" index="1"></constructor-arg>
		<constructor-arg value="200" type="int"></constructor-arg>
	</bean>
	
</beans>

2、配置bean的一些细节
1)可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入。

2)若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来。

<bean id="computer3" class="com.spring.bean.Computer">
		<constructor-arg value="dell" index="0"></constructor-arg>
		<constructor-arg type="java.lang.String">
			<value><![CDATA[core i5 &]]></value>
		</constructor-arg>
		<constructor-arg type="int">
			<value>250</value>
		</constructor-arg>
</bean>

3、bean的引用

创建一个Persion类,与Computer类存在依赖关系

package com.spring.bean;

public class Persion {
	
	private String name;
	private String age;
	private Computer com;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public Computer getCom() {
		return com;
	}
	public void setCom(Computer com) {
		this.com = com;
	}
	
	@Override
	public String toString() {
		return "Persion [name=" + name + ", age=" + age + ", com=" + com + "]";
	}
	
}

配置bean属性(通过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用)

<bean id="persion" class="com.spring.bean.Persion">
		<property name="name" value="jim"></property>
		<property name="age" value="13"></property>
		<property name="com" ref="computer3"></property>
</bean>

<bean id="persion" class="com.spring.bean.Persion">
		<property name="name" value="jim"></property>
		<property name="age" value="13"></property>
		<property name="com" >
			<ref bean="computer3"/>
		</property>
	</bean>

也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

<bean id="persion" class="com.spring.bean.Persion">
		<property name="name" value="jim"></property>
		<property name="age" value="13"></property>
		<property name="com" >
			<bean class="com.spring.bean.Computer">
				<constructor-arg value="dell" index="0"></constructor-arg>
				<constructor-arg value="core i5" index="1"></constructor-arg>
				<constructor-arg value="200" type="int"></constructor-arg>
			</bean>
		</property>
</bean>

4、集合属性(可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义)

<util:list id="cars">
		<ref bean="car"/>
		<ref bean="car2"/>
</util:list>

<set>
           <ref bean="address1"/>
           <ref bean="address2"/>
           <value>Pakistan</value>
</set>
<map>
           <entry key="one" value="INDIA"/>
           <entry key ="two" value-ref="address1"/>
           <entry key ="three" value-ref="address2"/>
</map>

5、配置Properties属性值

<property name="addressProp">
        <props>
           <prop key="one">INDIA</prop>
           <prop key="two">Pakistan</prop>
           <prop key="three">USA</prop>
           <prop key="four">USA</prop>
        </props>
</property>

6、p标签使用

<bean id="user3" class="com.spring.helloworld.User"
		p:cars-ref="cars" p:userName="Titannic"></bean>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值