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>
由此可见:这两种标签使代码的规范度大大提高,可读性也大大提高