IOC容器属性的命名空间

https://blog.csdn.net/weixin_45104211/article/details/104534915

关于如何传入不同类型的值,上述博客已经论述,有所不同的就是在标签<property>中比喻先指定其name,下面主要说明再加入新标签util、p标签后在传输map、set、list集合和properties属性上的简化

util的作用:可以使map、set、list集合单独存在

p的作用:可以使property变成bean表签的属性

Test.java

package club.shaoyu.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import club.shaoyu.vo.Student;
import club.shaoyu.vo.UserInfo;

public class Test {

		public static void main(String[] args) {
			ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("app.xml");
			Student student =applicationContext.getBean(Student.class);
			applicationContext.close();
		}
}

Student.java

package club.shaoyu.vo;

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

public class Student {

	private List<Double> socres;
	
	private Date birth;
	
	private Map<String,String > map;

	private Properties properties;
	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

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

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

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public List<Double> getSocres() {
		return socres;
	}

	public void setSocres(List<Double> socres) {
		this.socres = socres;
	}
}

app.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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	<bean name="date" class="java.util.Date"></bean>
	<!--命名空间context 开启后,便可使用component-scan标签 其作用是扫描包下所有的类,并为添加了@Controller、@Service、@Component、@Repository 修饰的类创建对象并存入loC容器中,调用的是默认构造方法 -->
	<!-- 如果扫描下有@Controller、@Service、@Component、@Repository的类下有含有@Autowired 注释标注,那么将会自动获取IoC容器中的对象进行赋值 -->
	<!-- 如何向构造方法里面传值 -->
	
	<!-- String 和基本类型 -->
	
	
	<!-- 自定义类 -->

	<!-- 数组类型 -->
	
	
	<!-- List 类型 -->
	
	<!-- set集合中的自定义类型 -->
	
	<!-- map集合 -->
	
	<!-- properties -->
	
	<!-- property -->
	<util:list id="l">
		<value>1</value>
		<value>2</value>
		<value>3</value>
	</util:list>
	<util:map id="M">
			<entry value="110" key="小李"></entry>
	 </util:map>
	 <util:properties id="P">
				<prop key="url">mysql://49.232.163.112:3306/mail</prop>
	 			<prop key="driver">jdbc</prop>
	 			<prop key="userName">root</prop>
	 </util:properties>
	 <bean class="club.shaoyu.vo.Student" p:socres-ref="l" p:map-ref="M" p:properties-ref="P"></bean>
</beans>

由此可见:这两种标签使代码的规范度大大提高,可读性也大大提高

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值