Detail the Spring XML file -- call the parameter construction method and assign the parameter

Keywords: xml Spring Java encoding

Catalog

 

1, If the constructor can be called

1. Write a UserInfo class:

2. The spring XML file is configured as follows:

3. Write a Test class

2, Assign values to different types of parameters of the constructor

1. Custom object

1) Write UserInfo class

2) Configuration XML file

3) Write Test class

2, array

1) Write UserInfo class

2) Configuration XML file

3) Write Test class

3. List collection

1) Write UserInfo class

2) Configuration XML file

3) Write Test class

4. Set set

1) Write UserInfo class

2) Configuration XML file

3) Write Test class

5. Map set

1) Write UserInfo class

2) Configuration XML file

3) Write Test class

6. Properties object

1) Write UserInfo class

2) Configuration XML file

3) Write Test class

1, If the constructor can be called

1. Write a UserInfo class:

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

2. The spring XML file is configured as follows:

<?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>
	
	<!-- How to call a constructor -->
	<bean class="com.jd.vo.UserInfo"><!--Set up Bean-->
		<constructor-arg name="age" value="12"></constructor-arg><!--Set parameters passed in by construction method-->
		<constructor-arg name="name" value="Tim"></constructor-arg>
	</bean>
	
</beans>

3. Write a Test class

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();
	}
}

Operation, observation results:

 

2, Assign values to different types of parameters of the constructor

1. Custom object

1) Write UserInfo class

import java.util.Date;

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

2) Configuration XML file

<?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><!--To configure Date object-->
	
	<context:component-scan base-package="com.jd"></context:component-scan>
	
	
	<!-- Assign value to constructor parameter: custom object -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg ref="date"></constructor-arg><!--utilize ref Attribute will Date Object afferent-->
	</bean>
	
</beans>

3) Write Test class

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();
	}
}

Operation result:

The run result indicates that the Date object has been passed in as a parameter to the constructor.

2, array

1) Write UserInfo class

public class UserInfo {
	
	public UserInfo(String [] mobiles) {//Pass in an array in the argument of the constructor
		for (String mobile : mobiles) {
			System.out.println(mobile);//Output each value in the collection to verify our operation
		}
	}
	
}

2) Configuration XML file

<?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>
	
	<!-- Assign value to constructor parameter: array -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg>
			<array>
				<value>119</value><!--The first way: assign values directly to array elements-->
				<bean class="java.lang.String"><!--The second way: through configuration Bean Establish String Class object to assign values to array elements-->
					<constructor-arg  value="120"></constructor-arg>
				</bean>
			</array>
		</constructor-arg>
	</bean>
	
</beans>

3) Write Test class

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();

	}
}

Operation result:

As you can see from the observation results, the String array has been passed in as a parameter construction method.

3. List collection

1) Write UserInfo class

import java.util.List;

public class UserInfo {
	
	public UserInfo(List<Double> moneys) {//In the constructor parameter, a List collection is passed in, and the generic type is Double
		for (Double money : moneys) {//Output each element in the collection to verify our operation
			System.out.println(money);
		}
	}
}

2) Configuration XML file

<?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>
	<!-- Assign a value to the constructor parameter: List aggregate -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg>
			<list>
				<value>21000</value>
				<value>23222</value>
				<value>42455</value>
			</list>
		</constructor-arg>
	</bean>
</beans>

3) Write Test class

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();
	}
}

Operation result:

As you can see from the observation, the List set has been used as the parameter input constructor.

4. Set set

1) Write UserInfo class

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

public class UserInfo {
	
	public UserInfo(Set<Date> births) {//In the constructor parameter, a List collection is passed in, and the generic type is Date
		for (Date birth : births) {//Output each element in the collection to verify our operation
			System.out.println(birth);
		}
	}
}

2) Configuration XML file

<?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>
	
	<!-- Assign a value to the constructor parameter: set aggregate -->
	<bean class="com.jd.vo.UserInfo">
		<constructor-arg>
			<set>
				<ref bean="date"/><!--Two different ways to assign values to elements-->
				<bean class="java.util.Date"></bean>
			</set>
		</constructor-arg>
		
	</bean>
</beans>

3) Write Test class

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();
	}
}

Operation result:

As can be seen from the observation results, the Set set has been used as the parameter input construction method.

5. Map set

1) Write UserInfo class

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

public class UserInfo {
	
	public UserInfo(Map<String,Integer> map) {
		for (String name : map.keySet()) {//Output each pair of key and value in the collection to verify our operation
			System.out.println(name+","+map.get(name));
		}
	}
}

2) Configuration XML file

<?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"><!--Through use Bean Establish String Object in map In set key assignment-->
		<constructor-arg value="Xiaogang"></constructor-arg>
	</bean>
	
	<!-- Assign a value to the constructor parameter: map aggregate -->
	<bean id="ui" class="com.jd.vo.UserInfo">
		<constructor-arg>
			<map>
				<entry key="Xiao Ming" value="12"></entry>
				<entry key-ref="xiaogang" value="23"></entry><!--utilize key-ref Attribute call String object-->
			</map>
		</constructor-arg>
	</bean>

</beans>

3) Write Test class

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();
	}
}

Operation result:

 

As can be seen from the observation results, the Map set has been used as the parameter input construction method.

6. Properties object

1) Write UserInfo class

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) Configuration XML file

<?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) Write Test class

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();
	}
}

Operation result:

 

As you can see from the observation, the Properties object has passed in the constructor as a parameter.

Published 80 original articles, won praise 7, visited 4760
Private letter follow

Posted by Grim... on Thu, 27 Feb 2020 02:44:19 -0800