bean中注入其他类型

注入基本类型的值

使用value属性来注入。

注入集合类型的值

List,Set,Map,Properties 

代码如下:

 

package value;

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

public class ValueBean {

	private String name;
	private int age;
	private List<String> city;
	private Set<String> interest;
	private Map<String,Double> score;
	private Properties db;
	
	public ValueBean() {
		System.out.println("ValueBean()");
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setAge(int age) {
		this.age = age;
	}

	public void setCity(List<String> city) {
		this.city = city;
	}

	public void setInterest(Set<String> interest) {
		this.interest = interest;
	}

	public void setScore(Map<String, Double> score) {
		this.score = score;
	}

	public void setDb(Properties db) {
		this.db = db;
	}

	@Override
	public String toString() {
		return "name=" + name + "\n" +
				"age=" + age + "\n" +
				"city=" + city + "\n" +
				"interest=" + interest + "\n" +
				"score="+ score + "\n" +
				"db="+db;
	}
	
}

配置文件:

 

 

<!-- bean中注入其他类型 -->
	<bean id="vb1" class="value.ValueBean">
	  <!-- 基本类型    value指定注入的值 -->
	  <property name="name" value="胡八一"></property>
	  <property name="age" value="30"></property>
	  
	  <!-- bean中注入List集合 -->
	  <property name="city">
	    <list>
	      <value>北京</value>
	      <value>长沙</value>
	      <value>西安</value>
	      <value>西安</value>
	    </list>
	  </property>
	  
	   <!-- bean中注入Set集合 -->
	  <property name="interest">
	    <set>
	      <value>盗墓</value>
	      <value>战斗</value>
	      <value>喝酒</value>
	      <value>战斗</value>
	    </set>
	  </property>
	  
	   <!-- bean中注入Map -->
	  <property name="score">
	    <map>
	      <entry key="英语" value="60"></entry>
	      <entry key="数学" value="65"></entry>
	    </map>
	  </property>
	  
	   <!-- bean中注入Properties集合(key和value都是字符串类型) -->
	   <property name="db">
	     <props>
	       <prop key="username">Tiger</prop>
	       <prop key="password">1234</prop>
	     </props>
	   </property>
	</bean>

测试文件:

@Test
	/**
	 * 测试注入其他类型
	 * 1.基本类型的值 value属性
	 * 2.集合List(允许重复元素 有序)
	 * 3.集合Set(若注入重复元素,则覆盖)
	 * 4.Map集合(无序)
	 * 5.Properties集合(无序)
	 */
	public void test4(){
		ApplicationContext ac = 
				new ClassPathXmlApplicationContext("value.xml");
		ValueBean vb1 = ac.getBean("vb1",ValueBean.class);
		System.out.println(vb1);
		/**
		 * 输出结果如下(利用set注入):
		 * ValueBean()
		 * name=胡八一
		 * age=30
	 	 * city=[北京, 长沙, 西安, 西安]
		 * interest=[盗墓, 战斗, 喝酒]
		 * score={英语=60.0, 数学=65.0}
		 * db={password=1234, username=Tiger}
		 */
	}

引用的方式注入集合类型的值

<!-- 使用引入的方式注入 -->
	<!-- util命名空间中的list元素 -->
	<!-- 将集合类型的值配置成一个bean -->
	<util:list id="cityBean">
	  <value>上海</value>
	  <value>深圳</value>
	  <value>武汉</value>
	</util:list>
	
	<util:set id="interestBean">
	  <value>书法</value>
	  <value>绘画</value>
	  <value>拳击</value>
	</util:set>
	
	<util:map id="scoreBean">
	  <entry key="英语" value="99"></entry>
	  <entry key="体育" value="88"></entry>
	</util:map>
	
	<util:properties id="dbBean">
	  <prop key="username">Sally</prop>
	  <prop key="password">1234</prop>
	</util:properties>
	
	<bean id="vb2" class="value.ValueBean">
	  <property name="city" ref="cityBean"></property>
	  <property name="interest" ref="interestBean"></property>
	  <property name="score" ref="scoreBean"></property>
	  <property name="db" ref="dbBean"></property>
	  <property name="name" value="蛋蛋"></property>
	  <property name="age" value="33"></property>
	</bean>

测试文件:

 

@Test
	//测试 引用的方式注入集合类型的值
	public void test5(){
		ApplicationContext ac = 
				new ClassPathXmlApplicationContext("value.xml");
		ValueBean vb2 = ac.getBean("vb2",ValueBean.class);
		System.out.println(vb2);
		/**
		 * 输出结果如下:
		 * name=蛋蛋
		 * age=33
		 * city=[上海, 深圳, 武汉]
		 * interest=[书法, 绘画, 拳击]
		 * score={英语=99.0, 体育=88.0}
		 * db={password=1234, username=Sally}
		 */
	}

 

读取properties文件的内容

 

<!-- 读取properties文件内容 
	     classpath:按照类路径来搜索
	     spring容器会根据路径找到对应的配置文件,然后读取该文件的内容到Properties对象
	-->
	<util:properties id="config" location="classpath:config.properties">
	</util:properties>

测试文件:

 

@Test
	//测试 读取Properties文件
	public void test6(){
		ApplicationContext ac = 
				new ClassPathXmlApplicationContext("value.xml");
		Properties p = ac.getBean("config",Properties.class);
		System.out.println(p);
		/**
		 * {pagesize=10}
		 */
	}

使用Spring表达式

	<bean id="vb1" class="value.ValueBean">
		<property name="name" value="胡八一" />
		<property name="age" value="30" />
		<property name="city">
			<list>
				<value>北京</value>
				<value>长沙</value>
				<value>西安</value>
				<value>西安</value>
			</list>
		</property>
		<property name="interest">
		    <set>
		        <value>钓鱼</value>
		        <value>做饭</value>
		        <value>喝酒</value>
		    </set>
		</property>
		<property name="score">
			<map>
				<entry key="english" value="60" />
				<entry key="数学" value="66" />
			</map>
		</property>
		<property name="db">
			<props>
				<prop key="username">Tiger</prop>
				<prop key="password">1234</prop>
			</props>
		</property>
	</bean>
<util:properties id="config" location="classpath:config.properties">
	</util:properties>
	
	<!-- 使用spring表达式 -->
	<bean id="sp1" class="value.SpelBean">
	  <property name="name" value="#{vb1.name}"></property>
	  <property name="city" value="#{vb1.city[1]}"></property>
	  <!-- 如果map的key为中文 value="#{vb1.score['数学']}"-->
	  <property name="score" value="#{vb1.score.math}"></property>
	  <!-- name="pageSize"为SpelBean中的bean属性名字 
	       config.pagesize为bean的ID为config,属性为pagesize
	    -->
	  <property name="pageSize" value="#{config.pagesize}"></property>
	</bean>

测试文件:

 

@Test
	//测试 使用spring表达式
	public void test7(){
		ApplicationContext ac = 
				new ClassPathXmlApplicationContext("value.xml");
		SpelBean sb = ac.getBean("sp1",SpelBean.class);
		System.out.println(sb);
		/**
		 * 输出结果如下:
		 * 无参构造器SpelBean()
		 * SpelBean [name=胡八一, city=北京, score=60.0, pageSize=10]
		 */
	}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

荒--

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

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

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

打赏作者

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

抵扣说明:

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

余额充值