1.参数为基本数据类型或String
使用constructor-arg标签属性:
name属性:通过参数名找到参数列表中对应参数
index属性:通过参数在参数列表中的索引找到参数列表中对应参数,index从0开始
type属性:通过参数数据类型找到参数列表中对应参数
value属性:设置参数列表参数对应的值,用于设定基本数据类型和String类型的数据
application.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"
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">
<context:component-scan base-package="com.jd"></context:component-scan>
<!--调用构造方法 name type index可乱序 无的话按原顺序 -->
<bean class="com.jd.vo.UserInfo">
<constructor-arg name="age" value="19"></constructor-arg>
<constructor-arg name="name" value="61"></constructor-arg>
</bean>
<beans/>
UserInfo.java
package com.jd.vo;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Service;
@Service
public class UserInfo {
public UserInfo(int age,String name) {
System.out.print(age+","+name);
}
}
Test.java
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();
}
}
console:
19,61
2.自定义类型
使用ref标签属性
<?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"
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">
<bean id="date" class="java.util.Date"></bean>
<context:component-scan base-package="com.jd"></context:component-scan>
<beans/>
public class UserInfo {
public UserInfo(Date date) {
System.out.println(date);
}
}
console出运行时间
3.参数为数组
使用constructor-arg子标签array:
(1)value(String类型、基本数据类型)
(2)ref(自定义类型)
public class UserInfo {
public UserInfo(String [] mobiles) {
for(String mobile:mobiles) {
System.out.println(mobile);
}
}
}
<?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"
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">
<context:component-scan base-package="com.jd"></context:component-scan>
<bean class="com.jd.vo.UserInfo">
<constructor-arg>
<array>
<value>puff</value>
<bean class="java.lang.String">
<constructor-arg value="kiwi"></constructor-arg>
</bean>
</array>
</constructor-arg>
</bean>
<beans/>
4、参数为集合:
1. List:使用constructor-arg标签的子标签list
(1)value(String类型、基本数据类型)
(2)ref(自定义类型)
public class UserInfo {
public UserInfo(List<Double> list) {
for(Double money:list) {
System.out.println(money);
}
}
}
<bean class="com.jd.vo.UserInfo">
<constructor-arg>
<list>
<value>9870</value>
<value>2500</value>
<value>1000</value>
</list>
</constructor-arg>
</bean>
2. Set:使用constructor-arg标签的子标签set
(1)value(String类型、基本数据类型)
(2)ref(自定义类型)
构造方法参数为List集合或Set集合的时候,其配置方式与array类似
public class UserInfo {
public UserInfo(Set<Date> births) {
for(Date birth:births) {
System.out.println(birth);
}
}
}
<bean class="com.jd.vo.UserInfo">
<constructor-arg>
<set>
<ref bean="date"/>
<bean class="java.util.Date"></bean>
</set>
</constructor-arg>
</bean>
3. Map:使用constructor-arg标签的子标签map来配置
(1)entry:key或value是String类型、基本数据类型,则分别使用entry标签中key和value标签属性
(2) entry:key或value是自定义类型,应该使用对相应的key-ref或value-ref
public class UserInfo {
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"class="com.jd.vo.UserInfo">
<constructor-arg>
<map>
<entry key="小明" value="77"></entry>
<entry key-ref="xiaogang" value="99"></entry>
</map>
</constructor-arg>
</bean>
5、参数为Properties类型数据
使用constructor-arg 标签的子标签props的prop标签:
public class UserInfo {
public UserInfo(Properties properties) {
System.out.println(properties.get("driver"));
System.out.println(properties.get("url"));
}
}
<bean class="com.jd.vo.UserInfo">
<constructor-arg>
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://127.0.0.1:3306/test</prop>
</props>
</constructor-arg>
</bean>
配置问题:
勾选context红框部分,后可在xml中使用constructor-arg标签
来源:CSDN
作者:脊椎类哺乳动物
链接:https://blog.csdn.net/Kiwi_no/article/details/104517630