This article demonstrates the injection of properties such as int, String, array, list, set, map, Date, etc.
Among them, the injection of Date type is achieved with the attribute editor provided by Spring. First, the five entity classes are used.
package com.jadyer.model; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; /** * Injection of common properties* @see Injection of int,String,Array,list,set,map,Date*/ public class Bean11 { private Integer intValue; private String strValue; private String[] arrayValue; private List listValue; private Set setValue; private Map mapValue; private Date dateValue; /* Setter and getter for seven attributes */ } package com.jadyer.model; public class Bean22 { private Bean33 bean33; private Bean44 bean4422; //Injection: irrelevant to the attribute name, but related to setBean44() private Bean55 bean55; /* Setter and getter for three attributes */ } package com.jadyer.model; public class Bean33 { private Integer id; private String name; private String sex; /* Setter and getter for three attributes */ } package com.jadyer.model; public class Bean33 { private Integer id; private String name; private String sex; /* Setter and getter for three attributes */ } package com.jadyer.model; public class Bean44 { private Integer id; private String name; private String sex; private Integer age; /* Setter and getter of the four attributes are slightly*/ } package com.jadyer.model; public class Bean55 { private String password; /* Setter and getter of the password are slightly*/ } Then there is our custom java.util.Date type converter
package com.jadyer.util; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * java.util.Date property editor. Equivalent to a type converter. Here is to convert String to Date type * @see ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Custom property editor things, you must inherit the PropertyEditorSupport class and overwrite the setAsText() method * @see Finally, inject the custom property editor into Spring, and then * @see -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void setAsText(String text) throws IllegalArgumentException { System.out.println("======= UtilDatePropertyEditor.setAsText()==========" + text); try { Date date = new SimpleDateFormat(pattern).parse(text); this.setValue(date); //Note: What is put in here is a java.util.Date object, so the output time is the default format} catch (ParseException e) { e.printStackTrace(); throw new IllegalArgumentException(text); //Continue to throw illegal exceptions with parameters upwards} } } The applicationContext-beans.xml file used for all entity classes
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-lazy-init="true"> <!-- The default-lazy-init="true" attribute is described in Line 49 of the InjectionTest.java class --> <!-- default-autowire="byName or byType" are two ways to automatically assemble beans provided by Spri0ng. For details-> <!-- ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* When executing testInjection22(), the default output will be ====== UtilDatePropertyEditor.setAsText()====== June 4, 2010--> <!-- That is, the default-lazy-init="true" is not set at this time, which means that the value of the dateValue property in Bean11 has been injected --> <!-- In fact, when the default Spring creates the ApplicationContext, it will instantiate all objects in the configuration file and inject --> <!-- The advantage of this is that if some configurations in the Spring configuration file are written incorrectly, it can be detected immediately --> <!-- The configuration file of Struts1.X will not have any problems if a class is written incorrectly. Only when it is actually executed --> <!-- For Spring, you can also use the relevant attributes to delay the initialization of the configuration file, that is, default-lazy-init="true" --> <!-- That is, when it is actually used, go to the New object and inject it into the attribute. This involves LAZY, that is, delay initialization --> <!-- Just modify the Spring configuration file, such as <beans xsi:schemaLocation="http://www...." default-lazy-init="true"> --> <!-- The scope of action at this time is the entire configuration file. Similarly, the lazy-init attributes of each <bean> tag can also be configured separately --> <!-- But this is not set, but the injection is completed when the BeanFactory is created, which is convenient for checking errors --> <!-- *************************************************************************************************************************************************************************** --> <bean id="bean11"> <property name="intValue" value="123"/><!-- When injected, the string 123 will automatically convert to int--> <property name="strValue" value="Hello_Spring"/> <property name="arrayValue"> <list> <value>array11</value> <value>array22</value> </list> </property> <property name="listValue"> <list> <value>list11</value> <value>list22</value> </list> </property> <property name="setValue"> <set> <value>set11</value> <value>set22</value> </set> </property> <property name="mapValue"> <map> <entry key="key11" value="value11"/> <entry key="key22" value="value22"/> </map> </property> <property name="dateValue" value="June 4, 2010"/><!-- Here the Date format should be the same as the configuration of applicationContext-editor.xml --> </bean> <bean id="bean22"> <property name="bean33" ref="bean33"/> <property name="bean44" ref="bean44"/> <property name="bean55" ref="bean55"/> </bean> <bean id="bean55"> <property name="password" value="123"/> </bean> </beans>
The applicationContext-common.xml file used for public entity classes
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- Extract public configuration using abstract beans--> <!-- First, specify the abstract property of the <bean> tag to true, and then specify its parent in other <beans>--> <bean id="AbstractBean" abstract="true"> <property name="id" value="2"/> <property name="name" value="Zhang Qiling"/> <property name="sex" value="Male"/> </bean> <bean id="bean33" parent="AbstractBean"/> <bean id="bean44" parent="AbstractBean"> <property name="age" value="26"/> </bean> </beans> <!-- The prototypes of bean33 and bean44 before using AbstractBean are as follows-> <!-- <bean id="bean33"> <property name="id" value="100"/> <property name="name" value="Zhang San"/> <property name="sex" value="Male"/> </bean> <bean id="bean44"> <property name="id" value="100"/> <property name="name" value="Zhang San"/> <property name="sex" value="Male"/> <property name="age" value="90"/> </bean> -->
The applicationContext-editor.xml file used for the java.util.Date property editor
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="utilDatePropertyEditor"> <property name="pattern" value="yyyyy MM month ddd"/> </bean> <!-- Check the source code and find that a setCustomEditors method is provided in line 131 of the CustomEditorConfigurer class, so you can inject --> <bean id="customEditors"> <property name="customEditors"> <map> <entry key="java.util.Date" value-ref="utilDatePropertyEditor"/> </map> </property> </bean> </beans> <!-- You can also use the internal <bean> to write the utilDatePropertyEditor internally--> <!-- In this way, only it has the right to use it, and the external cannot be used --> <!-- Since external access is not provided, there is no id attribute inside the <bean>--> <!-- Example is as follows-> <!-- <bean id="customEditors"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean> <property name="pattern" value="yyyyy MM month ddd day"/> </bean> </entry> </map> </property> </bean> -->
Finally, the unit test class written using JUnit3.8
package com.jadyer.junit; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jadyer.model.Bean11; import com.jadyer.model.Bean22; public class PropertyInjectionTest extends TestCase { private ApplicationContext factory; @Override protected void setUp() throws Exception { /****====Read a single configuration file ====****/ //factory = new ClassPathXmlApplicationContext("applicationContext.xml"); /****====Read multiple configuration files using arrays ====****/ //This way two configuration files will be used as one, on the surface, as two//In fact, internally as one, so in multiple configuration files, the id inside cannot be repeated // But the name attribute can be repeated, just as the difference between the person's ID number and name is the same //String[] configLocations = new String[]{"applicationContext.xml", "applicationContext-editor.xml"}; //factory = new ClassPathXmlApplicationContext(configLocations); /****====== Use the * matching pattern to read multiple configuration files ====****/ // A popular saying in the industry: Conventions are better than configuration //So when there is a unified and better agreement, you can use the functions provided by the framework to reduce the configuration amount //In addition: If applicationContext-*.xml is not read, even if applicationContext.xml exists, it will not read factory = new ClassPathXmlApplicationContext("applicationContext-*.xml"); } /** * This method demonstrates the injection of common attributes, including int,String,Array,list,set,map,Date injection* @see Where the injection of Date type is implemented with the help of Spring property editor */ public void testInjection11() { //Bean11 bean11 = new Bean11(); //If you are simple new, then its attributes will not be injected. The prerequisite for injection must be taken out of the IoC container before it will inject Bean11 bean11 = (Bean11)factory.getBean("bean11"); //At this time bean11 is obtained from the IoC container, so its dependencies will be injected into System.out.println("bean11.intValue=" + bean11.getIntValue()); System.out.println("bean11.strValue=" + bean11.getStrValue()); System.out.println("bean11.arrayValue=" + bean11.getArrayValue()); System.out.println("bean11.listValue=" + bean11.getListValue()); System.out.println("bean11.setValue=" + bean11.getSetValue()); System.out.println("bean11.mapValue=" + bean11.getMapValue()); System.out.println("bean11.dateValue=" + bean11.getDateValue()); } /** * This method mainly demonstrates the abstraction of public configurations to reduce the amount of configuration*/ public void testInjection22() { Bean22 bean22 = (Bean22)factory.getBean("bean22"); System.out.println("bean22.bean33.id=" + bean22.getBean33().getId()); System.out.println("bean22.bean33.name=" + bean22.getBean33().getName()); System.out.println("bean22.bean33.sex=" + bean22.getBean33().getSex()); System.out.println("bean22.bean44.id=" + bean22.getBean44().getId()); System.out.println("bean22.bean44.name=" + bean22.getBean44().getName()); System.out.println("bean22.bean44.sex=" + bean22.getBean44().getSex()); System.out.println("bean22.bean44.age=" + bean22.getBean44().getAge()); System.out.println("bean22.bean55.password=" + bean22.getBean55().getPassword()); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.