JavaBean的相关处理

本文详细介绍Spring框架中Bean的配置方式,包括属性注入、构造方法参数注入及复杂类型的配置方法,如List、Set、Map和Properties等。

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

一.Bean的子节点
1.property节点可以配置类的属性,name类中属性的名称,ref值所对应的<bean>的id

2.constructor-arg节点配置构造方法的参数值,index:第几个参数,ref值所对应的<bean>的id

<bean id="sampleBean" class="cn.tedu.spring.bean.SampleBean">
<property name="date" ref="date"></property>
</bean>
<bean id="date" class="java.util.Date">
    <constructor-arg index="0"><value type="long">0</value></constructor-arg>
</bean>

二.List、Set、Map和赋值操作
注意:Set集合中判断数据是否相同/唯一的标准
    2个数据的equals()对比结果为true,并且2个数据的hashCode()返回值相同。

1.List的赋值操作
(1)直接bean下面写

<property name="list">
    <list>
        <value>list-1</value>
        <value>list-2</value>
    </list>
</property>

(2)数据抽取出来写

<bean id="sampleBean" class="cn.tedu.spring.bean.SampleBean">
<property name="list" ref="list"></property>
</bean>
<util:list id="list">
    <value>list-1</value>
    <value>list-2</value>
</util:list>


2.Set的赋值操作
(1)直接bean下面写
<property name="set">
    <set>
        <value>set-1</value>
        <value>set-2</value>
    </set>
</property>
(2)数据抽取出来写
<bean id="sampleBean" class="cn.tedu.spring.bean.SampleBean">
<property name="set" ref="set"></property>
</bean>
<util:set id="set">
    <value>set-1</value>
    <value>set-2</value>
</util:set>

3.Map的赋值操作
(1)直接bean下面写

<bean id="sampleBean" class="cn.tedu.spring.bean.SampleBean">
<property name="map">
    <map>
        <entry key="K-1" value-ref="object"></entry>
        <entry key="K-2" value="V-2"></entry>
    </map>
</property>
</bean>
<bean id="object" class="java.lang.Object"></bean>
(2)数据抽取出来写
<bean id="sampleBean" class="cn.tedu.spring.bean.SampleBean">
<property name="map" ref="map"></property>
</bean>
<util:map id="map">
    <entry key="K-1" value-ref="object"></entry>
    <entry key="K-2" value="V-2"></entry>
</util:map>
<bean id="object" class="java.lang.Object"></bean>


三.Properties的赋值操作
1.直接赋值
<property name="properties">
    <props>
        <prop key="username">linsa</prop>
        <prop key="password">123</prop>
    </props>
</property>


2.读取resource文件夹下的*.properties文件
location:以classpath:作为前缀,加上文件的路径与文件名

<bean id="sampleBean" class="cn.tedu.spring.bean.SampleBean">
    <property name="properties" ref="dbConfig"></property>
</bean>
<util:properties id="dbConfig" location="classpath:db-config.properties"></util:properties>


四.实例
1.resource文件夹下的db-config.properties文件:
#properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql
jdbc.username=root
jdbc.password=

2.resource文件夹下的applicationContext.xml文件:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
	<bean id="sampleBean" class="cn.tedu.spring.bean.SampleBean">
		<property name="name" value="演示专用名"></property>
		<property name="id" value="2985"></property>
		<property name="date" ref="date"></property>
		
		<constructor-arg index="0" value="演示专用标签"></constructor-arg>
		<constructor-arg index="1" value="演示专用构造方法名称"></constructor-arg>
		
		<!-- 配置List集合的数据 -->
		<property name="list" ref="list"></property>
		<!-- 配置Set集合的数据 -->
		<property name="set" ref="set"></property>
		<!-- 配置Map表的数据 -->
		<property name="map" ref="map"></property>
		<!-- 配置Properties的数据 -->
		<!-- <property name="properties">
			<props>
				<prop key="username">linsa</prop>
				<prop key="password">123</prop>
			</props>
		</property> -->
		<property name="properties" ref="dbConfig"></property>
	</bean>
	<bean id="date" class="java.util.Date">
		<constructor-arg index="0"><value type="long">0</value></constructor-arg>
	</bean>
	<bean id="object" class="java.lang.Object"></bean>
	
	<!-- 自动读取resource文件夹下的*.properties文件读到的数据将保存到类型为Properties的对象中
		id:同<bean>节点的id,location:以classpath:作为前缀,加上文件的路径与文件名 -->
	<util:properties id="dbConfig" location="classpath:db-config.properties"></util:properties>
	<util:list id="list">
		<value>list-1</value>
		<value>list-2</value>
		<value>list-3</value>
	</util:list>
   <util:set id="set">
   	<value>set-1</value>
		<value>set-2</value>
		<value>set-3</value>
   </util:set>
   <util:map id="map">
   	<entry key="K-1" value-ref="object"></entry>
		<entry key="K-2" value="V-2"></entry>
		<entry key="K-3" value="V-3"></entry>
   </util:map>
</beans>

3.JavaBean实例:

package cn.tedu.spring.bean;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class SampleBean {
	private Integer id;
	private String tag;
	private String name;
	private Date date;
	private List<String> list;
	private Set<String> set;
	private Map<String,Object> map;
	private Properties properties;
	
	public SampleBean(String tag,String name) {
		this.tag=tag;
		this.name=name;
	}
	
	public String getTag() {
		return tag;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	public Set<String> getSet() {
		return set;
	}

	public void setSet(Set<String> set) {
		this.set = set;
	}

	public Map<String,Object> getMap() {
		return map;
	}

	public void setMap(Map<String,Object> map) {
		this.map = map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	
}
4.测试类:
package cn.tedu.spring.test;

import java.util.List;
import java.util.Map;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.tedu.spring.bean.SampleBean;

public class Test {
	public static void main(String[] args) {
		//读取Spring配置文件
		String file = "applicationContext.xml";
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(file);
		
		//获取Bean对象
		SampleBean bean = ctx.getBean("sampleBean",SampleBean.class);
		
		//测试数据
		System.out.println("SampleBean name="+bean.getName());
		System.out.println("SampleBean tag="+bean.getTag());
		System.out.println("SampleBean id="+bean.getId());
		System.out.println("SampleBean date="+bean.getDate());
		System.out.println("SampleBean list="+bean.getList());
		System.out.println("SampleBean list="+bean.getList().getClass());
		System.out.println("SampleBean set="+bean.getSet());
		System.out.println("SampleBean set="+bean.getSet().getClass());
		System.out.println("SampleBean map="+bean.getMap());
		System.out.println("SampleBean map="+bean.getMap().getClass());
		System.out.println("SampleBean properties="+bean.getProperties());
		
		System.out.println("\n------------");
		List<?> list 
			= ctx.getBean("list", List.class);
		System.out.println("list:\n\t" + list);
		
		Map<String, Object> map
			= ctx.getBean("map", Map.class);
		
		System.out.println("map:\n\t" + map);
		System.out.println("\n------------");
		
		// 释放资源
		ctx.close();
		
		/*程序运行结果如下:
		SampleBean name=演示专用名
		SampleBean tag=演示专用标签
		SampleBean id=2985
		SampleBean date=Thu Jan 01 08:00:00 CST 1970
		SampleBean list=[list-1, list-2, list-3]
		SampleBean list=class java.util.ArrayList
		SampleBean set=[set-1, set-2, set-3]
		SampleBean set=class java.util.LinkedHashSet
		SampleBean map={K-1=java.lang.Object@887af79, K-2=V-2, K-3=V-3}
		SampleBean map=class java.util.LinkedHashMap
		SampleBean properties={jdbc.url=jdbc:mysql://localhost:3306/mysql, jdbc.username=root, jdbc.driver=com.mysql.jdbc.Driver, jdbc.password=}

		------------
		list:
			[list-1, list-2, list-3]
		map:
			{K-1=java.lang.Object@887af79, K-2=V-2, K-3=V-3}

		------------*/
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linsa_pursuer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值