详述Spring XML文件——调用有参构造方法并为参数赋值

目录

 

一、如可调用构造方法

1、写一个UserInfo类:

2、SpringXML文件做如下配置:

3、写一个Test类

二、为构造方法不同类型参数赋值

1、自定义对象

1)编写UserInfo类

2)配置XML文件

3)编写Test类

2、数组

1)编写UserInfo类

2)配置XML文件

3)编写Test类

3、List集合

1)编写UserInfo类

2)配置XML文件

3)编写Test类

4、Set集合

1)编写UserInfo类

2)配置XML文件

3)编写Test类

5、Map集合

1)编写UserInfo类

2)配置XML文件

3)编写Test类

6、Properties对象

1)编写UserInfo类

2)配置XML文件

3)编写Test类


一、如可调用构造方法

1、写一个UserInfo类:

public class UserInfo {
	
	public UserInfo(int age,String name) {
		System.out.println(age+","+name);
	}
	
}

2、SpringXML文件做如下配置:

<?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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	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 id="date" class="java.util.Date"></bean>
	
	<context:component-scan base-package="com.jd"></context:component-scan>
	
	<!-- 如何调用构造方法 -->
	<bean class="com.jd.vo.UserInfo"><!--设置Bean-->
		<constructor-arg name="age" value="12"></constructor-arg><!--设置构造方法传入的参数-->
		<constructor-arg name="name" value="Tim"></constructor-arg>
	</bean>
	
</beans>

3、写一个Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.vo.UserInfo;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		UserInfo userInfo =  applicationContext.getBean(UserInfo.class);
		applicationContext.close();
	}
}

 运行,观察结果:

二、为构造方法不同类型参数赋值

1、自定义对象

1)编写UserInfo类

import java.util.Date;

public class UserInfo {
	
	public UserInfo(Date birth) {
		System.out.println(birth);
	}
	
}

2)配置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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	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 id="date" class="java.util.Date"></bean><!--配置Date对象-->
	
	<context:component-scan base-package="com.jd"></context:component-scan>
	
	
	<!-- 给构造方法参数赋值:自定义对象 -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg ref="date"></constructor-arg><!--利用ref属性将Date对象传入-->
	</bean>
	
</beans>

3)编写Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.vo.UserInfo;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		UserInfo userInfo =  applicationContext.getBean(UserInfo.class);
		applicationContext.close();
	}
}

运行结果:

 运行结果说明Date对象已作为参数被传入构造方法。

2、数组

1)编写UserInfo类

public class UserInfo {
	
	public UserInfo(String [] mobiles) {//在构造方法的参数中传入一个数组
		for (String mobile : mobiles) {
			System.out.println(mobile);//将集合中每个值输出,以验证我们的操作
		}
	}
	
}

2)配置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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	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 id="date" class="java.util.Date"></bean>
	
	<context:component-scan base-package="com.jd"></context:component-scan>
	
	<!-- 给构造方法参数赋值:数组 -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg>
			<array>
				<value>119</value><!--第一种方式:直接给数组元素赋值-->
				<bean class="java.lang.String"><!--第二种方式:通过配置Bean创建String类对象的方式给数组元素赋值-->
					<constructor-arg  value="120"></constructor-arg>
				</bean>
			</array>
		</constructor-arg>
	</bean>
	
</beans>

3)编写Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.vo.UserInfo;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		UserInfo userInfo =  applicationContext.getBean(UserInfo.class);
		applicationContext.close();

	}
}

运行结果:

 观察结果可得,String数组已做为参数传入构造方法。

3、List集合

1)编写UserInfo类


import java.util.List;

public class UserInfo {
	
	public UserInfo(List<Double> moneys) {//在构造方法参数中传入一个List集合,泛型为Double
		for (Double money : moneys) {//将集合中每一个元素输出,以验证我们的操作
			System.out.println(money);
		}
	}
}

2)配置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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	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 id="date" class="java.util.Date"></bean>
	<context:component-scan base-package="com.jd"></context:component-scan>
	<!-- 给构造方法参数赋值:List集合 -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg>
			<list>
				<value>21000</value>
				<value>23222</value>
				<value>42455</value>
			</list>
		</constructor-arg>
	</bean>
</beans>

3)编写Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jd.vo.UserInfo;
public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		UserInfo userInfo =  applicationContext.getBean(UserInfo.class);
		applicationContext.close();
	}
}

运行结果:

 观察结果可得,List集合已做为参数传入构造方法。

4、Set集合

1)编写UserInfo类

import java.util.Date;
import java.util.Set;

public class UserInfo {
	
	public UserInfo(Set<Date> births) {//在构造方法参数中传入一个List集合,泛型为Date
		for (Date birth : births) {//将集合中每一个元素输出,以验证我们的操作
			System.out.println(birth);
		}
	}
}

2)配置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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	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 id="date" class="java.util.Date"></bean>
	<context:component-scan base-package="com.jd"></context:component-scan>
	
	<!-- 给构造方法参数赋值:set集合 -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg>
			<set>
				<ref bean="date"/><!--两种不同的为元素赋值的方式-->
				<bean class="java.util.Date"></bean>
			</set>
		</constructor-arg>
		
	</bean>
</beans>

3)编写Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.vo.UserInfo;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		UserInfo userInfo =  applicationContext.getBean(UserInfo.class);
		applicationContext.close();
	}
}

 运行结果:

  观察结果可得,Set集合已做为参数传入构造方法。

5、Map集合

1)编写UserInfo类

import java.util.Map;
import java.util.Properties;

public class UserInfo {
	
	public UserInfo(Map<String,Integer> map) {
		for (String name : map.keySet()) {//将集合中每一对key、value输出,以验证我们的操作
			System.out.println(name+","+map.get(name));
		}
	}
}

2)配置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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	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 id="date" class="java.util.Date"></bean>
	
	<context:component-scan base-package="com.jd"></context:component-scan>
	
	<bean id="xiaogang" class="java.lang.String"><!--通过用Bean创建String对象的方式为map集合中的key赋值-->
		<constructor-arg value="小刚"></constructor-arg>
	</bean>
	
	<!-- 给构造方法参数赋值:map集合 -->
	<bean id="ui" class="com.jd.vo.UserInfo">
		<constructor-arg>
			<map>
				<entry key="小明" value="12"></entry>
				<entry key-ref="xiaogang" value="23"></entry><!--利用key-ref属性调用String对象-->
			</map>
		</constructor-arg>
	</bean>

</beans>

3)编写Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.vo.UserInfo;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		UserInfo userInfo =  applicationContext.getBean(UserInfo.class);
		applicationContext.close();
	}
}

运行结果:

 

 观察结果可得,Map集合已做为参数传入构造方法。

6、Properties对象

1)编写UserInfo类

import java.util.Properties;

public class UserInfo {
	
	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"));
	}
	
}

2)配置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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	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>
			<props>
				<prop key="driver">com.mysql.jdbc.Driver</prop>
				<prop key="url">jdbc://127.0.0.1:3306/test</prop>
				<prop key="username">root</prop>
				<prop key="password">root</prop>
			</props>
		</constructor-arg>
	</bean>
	
</beans>

3)编写Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.vo.UserInfo;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		UserInfo userInfo =  applicationContext.getBean(UserInfo.class);
		applicationContext.close();
	}
}

运行结果:

 

观察结果可得,Properties对象已做为参数传入构造方法。

发布了91 篇原创文章 · 获赞 10 · 访问量 8013

猜你喜欢

转载自blog.csdn.net/Liuxiaoyang1999/article/details/104540462