构造方法赋值——#06

类型1

public class UserInfo {
 public UserInfo(int age, String name) {
  System.out.println(age + ", " + name);
 }
}
<?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">
 <context:component-scan base-package="com.jd"></context:component-scan>
 <!-- 如何调用构造方法 -->
 <bean class="com.jd.vo.UserInfo">
  <constructor-arg value="12"></constructor-arg>
  <constructor-arg value="Tom"></constructor-arg>
 </bean> 
public class Test {
 public static void main(String[] args) {
  ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
  UserInfo userInfo = applicationContext.getBean( UserInfo.class);
  System.out.println(userInfo);
  applicationContext.close();
 }
}

赋值方式是有顺序的,如果无序,可用如下方法解决:

<constructor-arg index="0" value="18"></constructor-arg>
<constructor-arg index="1" value="Tom"></constructor-arg>
———————————————————————————————————————————————
<constructor-arg  type="int" value="18"></constructor-arg>
<constructor-arg type="java.lang.String" value="Tom"></constructor-arg>
———————————————————————————————————————————————
<constructor-arg name="age"  value="18"></constructor-arg>
<constructor-arg name="name" value="Tom"></constructor-arg>
<!-- 如何为不同类型赋值 -->
 <!-- 
  1、基本数据类型或则String类型则直接使用value标签属性
  2、自定义类型,则使用ref标签属性
  3、数组类型,则使用array子标签
  4、list集合类型,则使用list子标签,set,map
  5、Properties类型,使用prop标签
  -->

类型2(Date)

<bean id="date" class="java.util.Date"></bean>
<bean class="com.jd.vo.UserInfo">
   <constructor-arg ref="date"></constructor-arg>
</bean>
public UserInfo(Date birth) {
  System.out.println(birth);
 }

类型3(数组)

public UserInfo(String [] mobiles) {
  for (String mobile : mobiles) {
   System.out.println(mobile);
  }
 }
<bean class="com.jd.vo.UserInfo">
   <constructor-arg>
    <array>
     <value>110</value>
     <bean class="java.lang.String">
      <constructor-arg value="119"></constructor-arg>
     </bean>
    </array>
   </constructor-arg>
  </bean>

类型4(List)

public UserInfo(List<Double> list) {
  for (Double money : list) {
   System.out.println(money);
  }
 }
<bean class="com.jd.vo.UserInfo">
   <constructor-arg>
    <list>
     <value>110.00</value>
     <value>1199.00</value>
     <value>999.00</value>
    </list>
   </constructor-arg>
  </bean> 

类型5(Set)

public UserInfo(Set<Date> births) {
  for (Date birth : births) {
   System.out.println(birth);
  }
 }
<bean id="date" name="date" class="java.util.Date"></bean>
<bean class="com.jd.vo.UserInfo">
   <constructor-arg>
    <set>
     <ref bean="date"/>name对应值
     <bean class="java.util.Date"></bean>
    </set>
   </constructor-arg>
  </bean>

类型5(Map)

public UserInfo(Map<String,Integer> map) {
  for (String name : map.keySet()) {
   System.out.println(name +", " + map.get(name));
  }
 }
  <bean id="xiaogang" class="java.lang.String">
   <constructor-arg value="小刚"></constructor-arg>
  </bean>
  <bean id="ui" name="u, i, userinfo" class="com.jd.vo.UserInfo">
   <constructor-arg>
    <map>
     <entry key="小明" value="12"></entry>
     <entry key-ref="xiaogang" value="34"></entry>
    </map>
   </constructor-arg>
  </bean>
UserInfo userInfo = applicationContext.getBean("ui", UserInfo.class);
System.out.println(userInfo);

类型6(Properties)

public UserInfo(Properties properties) {
  System.out.println(properties.getProperty("driver"));
  System.out.println(properties.getProperty("url"));
  System.out.println(properties.getProperty("username"));
  System.out.println(properties.getProperty("password"));
 }
 <bean class="com.jd.vo.UserInfo">
   <constructor-arg>
    <props>
     <prop key="driver">com.mysql.jdbc.Driver</prop>
     <prop key="url">jdbc:mysql://localhost:3306/test</prop>
     <prop key="username">root</prop>
     <prop key="password">root</prop>
    </props>
   </constructor-arg>
  </bean>

util标签

<util:list id="s">
    <value>90</value>
    <value>100</value>
    <value>70</value>
  </util:list>
  ....
  <bean class="com.jd.vo.Student" p:scores-ref="s">
  .............................................................二者的功能是一样的,对于其他标签,也是如此的效果。
  <property name="scores">
    <list>
     <value>90</value>
     <value>100</value>
     <value>70</value>
    </list>
   </property>
public class Student {
 private List<Double> scores;
 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> getScores() {
  return scores;
 }
 public void setScores(List<Double> scores) {
  this.scores = scores;
 }
}

Test类

public class Test {
 public static void main(String[] args) {
  ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
  UserInfo userInfo = applicationContext.getBean("ui", UserInfo.class);
  System.out.println(userInfo);
  Student student = applicationContext.getBean(Student.class);
  System.out.println(student.getScores().size());
  System.out.println(student.getBirth());
  System.out.println(student.getMap().get("小李"));
  applicationContext.close();
 }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值