이 기사는 int, String, Array, List, Set, Map, Date 등과 같은 속성의 주입을 보여줍니다.
그중에서도 날짜 유형의 주입은 Spring에서 제공 한 속성 편집기로 달성됩니다. 먼저 5 개의 엔티티 클래스가 사용됩니다.
패키지 com.jadyer.model; import java.util.date; Java.util.list 가져 오기; java.util.map import; java.util.set import; / *** 공통 속성의 주입* @int, String, Array, List, Set, Map, Date*/ public class bean11 {private integer intvalue; 개인 문자열 strvalue; 개인 문자열 [] ArrayValue; 개인 목록 ListValue; 개인 세트 setValue; 개인지도 MapValue; 개인 날짜 날짜 값; / * 7 개의 속성에 대한 setter and getter */} 패키지 com.jadyer.model; 공개 클래스 Bean22 {private bean33 bean33; 개인 Bean44 Bean4422; // 주입 : 속성 이름과 관련이 없지만 setBean44 () private bean55 bean55; / * 세 가지 속성에 대한 setter and getter */} 패키지 com.jadyer.model; 공개 클래스 Bean33 {개인 정수 ID; 개인 문자열 이름; 개인 문자열 섹스; / * 세 가지 속성에 대한 setter and getter */} 패키지 com.jadyer.model; 공개 클래스 Bean33 {개인 정수 ID; 개인 문자열 이름; 개인 문자열 섹스; / * 세 가지 속성에 대한 setter and getter */} 패키지 com.jadyer.model; 공개 클래스 Bean44 {개인 정수 ID; 개인 문자열 이름; 개인 문자열 섹스; 개인 정수 시대; /* 네 가지 속성의 세트 및 getter는 약간*/} 패키지 com.jadyer.model; 공개 클래스 Bean55 {개인 문자열 비밀번호; /* 비밀번호의 setter와 getter는 약간입니다*/} 그런 다음 사용자 정의 java.util.date 유형 변환기가 있습니다
패키지 com.jadyer.util; Java.beans.propertyeditorsupport import; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; /*** java.util.date 속성 편집기. 유형 변환기와 동일합니다. 다음은 문자열을 날짜로 변환하는 것입니다 * @see --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Custom Property Editor Things, PropertyEditorSupport 클래스를 상속하고 settasext () 메소드 * @see를 덮어 쓰고, 마지막으로, 사용자 정의 속성 편집기를 스프링에 주입 한 다음 * @see를 덮어 쓸어 야합니다. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void settastext (문자열 텍스트)는 불법적 인 일을 던졌습니다. {system.out.println ( "======= UTILDATEPROPERTYEDITOR.SETASTEXT () ========" + 텍스트); try {date date = new SimpledateFormat (Pattern) .parse (텍스트); this.setValue (날짜); // 참고 : 여기에 넣은 것은 java.util.date 객체이므로 출력 시간은 기본 형식} catch (parseexception e) {e.printstacktrace (); 새로운 불법 행정 exception (텍스트)을 던지십시오. // 매개 변수와 함께 불법적 인 예외를 계속해서}}} 모든 엔티티 클래스에 사용되는 ApplicationContext-beans.xml 파일
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.springframwork.org/schema/schema/schemas http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.spramework.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-2.5.xsd "" "" "" "" "" "" " Default-Lazy-Init = "True"속성은 imperectiontest.java 클래스의 49 행에 설명되어 있습니다 .-> <!-Default-Autowire = "ByName 또는 Bytype"는 Spri0NG가 제공하는 Bean을 자동으로 조립하는 두 가지 방법입니다. 자세한 내용-> <!- **********************************************************************************************************************************************************************************************, ********************************************************************************************************************************************** : **********************************************************************************************************************************************************************************************, ********************************************************************************************************************************************** : **********************************************************************************************************************************************************************************************, ********************************************************************************************************************************************** : ********************************************************************************************************************************************** : ********************************************************************************************************************************************** : TestInjection22 ()를 실행할 때 기본 출력은 ======= UtildAtePropertyEditor.setAtext () ====== 2010 년 6 월 4 일-> <!-즉, 기본적으로 Lazy-init = "true"는이 시점에서 설정되지 않았으며, 이는 BEAN11의 DateValue 속성의 가치가 주입되었음을 의미합니다. 구성 파일의 모든 객체를 인스턴스화하고 인젝트-> <!-이 장점은 스프링 구성 파일의 일부 구성이 잘못 작성되면 즉시 감지 할 수 있다는 것입니다 .-> <!-Struts1.x의 구성 파일은 클래스가 잘못 작성되면 문제가 없습니다. 실제로 실행 된 경우-> <!-스프링의 경우에만 관련 속성을 사용하여 구성 파일의 초기화를 지연시킬 수도 있습니다. 여기에는 게으른 초기화-> <!-<beans xsi : schemalocation = "http : // www ...."default-lazy-init = "true">-> <!과 같은 스프링 구성 파일을 수정합니다.이 시점의 동작 범위는 전체 구성 파일입니다. 마찬가지로 각 <ean> 태그의 게으른 속성은 별도로 구성 할 수 있지만-> <!-이 설정은 설정되지 않았지만 BeanFactory가 생성 될 때 주입이 완료되며 오류를 확인하는 데 편리합니다 (> <!-). *****************************************************************************************************************************************************-> <Property Name_______e 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 이름 = "setValue"> <set> <value> set11 </value> <value> set22 </value> </set> </property> <속성 이름 = "mapValue"> <map> <alture key = "key11"value = "value11"/> <Entry Key = "key22"value22 "/> </map> <속성 이름 =-속성 이름" "value name" 여기서 날짜 형식은 ApplicationContext-editor.xml-> </bean> <bean id = "bean22"> <property name = "bean33"ref = "bean33"/> <property name = "bean44"ref = "bean44"/> <property name = "bean55"ref = "bean55"</bean> <eD = "eD ="eD = "bean 55"> 값 = "123"/> </bean> </beans>
공개 엔티티 클래스에 사용되는 ApplicationContext-Common.xml 파일
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.springframwork.org/schema/schema/schemas http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.spramework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.spramework.org/schema/spring-tx-2.5.xsd "! <!-먼저, <ean> 태그의 초록 속성을 true로 지정한 다음 다른 <eans>-> <bean id = "bean id ="actractbean "acpract ="true "> <property name ="id "value ="2 "/> <property name ="name "value ="zhang qing "/> <property name ="sex "value"/</bean> <ead = "bean" "bean" "bean" "bean" "bean" "bean" "ead ="a bean ""id id = "jhang" id = "bean44"parent = "acpractbean"> <property name = "age"value = "26"/> </bean> </beans> <!-AbstractBean을 사용하기 전에 Bean33 및 Bean44의 프로토 타입은 다음과 같습니다.> <!-<bean id = "bean 33"> <property name = "id"value = "100 ="value = "zhang san"///>. value = "male"/> </bean> <bean id = "bean44"> <속성 이름 = "id"value = "100"/> <속성 이름 = "name"value = "zhang san"/> <property name = "sex"value = "male"/<property name = "age"value = "90"/> </bean> -->
java.util.date 속성 편집기에 사용되는 ApplicationContext-editor.xml 파일
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.springframwork.org/schema/schema/schemas http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.spramework.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-2.5.xsd id = "utildatePropertyEditor"> <속성 이름 = "Pattern"value = "yyyyy mm month ddd"/> </bean> <!-소스 코드를 확인하고 setCustomEditors 메소드가 CustomeditorConfigurer 클래스의 131 행에 제공되는지 확인하십시오. key = "java.util.date"value-ref = "utildatePropertyEditor"/> </map> </beans> </beans> </beans> <!-내부적으로 utildatepropertyeditor를 내부적으로 작성하기 위해 내부적으로 사용될 수 있습니다. <ain>-> <!-예제 내부에서 예제는 다음과 같습니다 .-> <!-<bean id = "communityedors"> <property name = "customeditors"> <map> <enterd key = "java.util.date"> <bean> <bean> <bean name = "value ="yyyyy mm month ddd day "</bean> </ent
마지막으로, 단위 테스트 클래스는 Junit3.8을 사용하여 작성되었습니다
패키지 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는 TestCase {private ApplicationContext Factory; @override protected void setup ()은 예외를 던져 { /**** ==== 단일 구성 파일 읽기 ==== **** /// factory = new ClassPathXmlApplicationContext ( "ApplicationContext.xml"); /**** ==== 배열을 사용하여 여러 구성 파일을 읽습니다 ==== **** //이 방법으로 두 개의 구성 파일이 표면에서 하나로 하나로 사용됩니다. 실제로 하나는 내부적으로 하나로 내부적으로 하나로 사용됩니다. 따라서 여러 구성 파일에서 ID가 반복 될 수는 없지만 이름 속성이 반복 될 수는 없지만 사람의 ID 번호와 이름이 동일 할 수 있습니다. 문자열 [] { "ApplicationContext.xml", "ApplicationContext-Editor.xml"}; // factory = new ClassPathXmlApplicationContext (configlocations); /**** ======*일치 패턴을 사용하여 여러 구성 파일을 읽습니다. ==== **** // 업계에서 인기있는 말 : 컨벤션이 구성보다 낫습니다. 따라서 통합적이고 더 나은 계약이있을 때 프레임 워크가 제공 한 함수를 사용하여 구성 금액을 줄이기 위해 다음 //*. 팩토리 읽기 = 새로운 classPathXmlApplicationContext ( "ApplicationContext-*. XML"); } /***이 방법은 int, string, array, list, set, set, map, date meatection을 포함한 일반적인 속성의 주입을 보여줍니다. 스프링 속성 편집기* /public void testinjection11 () {// bean11 bean11 = new bean11 (); // 단순하면 새롭면 속성이 주입되지 않습니다. 주입 전제 조건은 Bean11 Bean11 = (Bean11) factory.getBean ( "Bean11")을 주입하기 전에 IOC 컨테이너에서 꺼내야합니다. // 현재 Bean11은 IOC 컨테이너에서 얻어 지므로 System.out.println ( "bean11.intvalue =" + bean11.getIntValue ())에 종속성이 주입됩니다. System.out.println ( "bean11.strvalue =" + bean11.getStvalue ()); 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 ()); } / ***이 방법은 주로 구성의 양을 줄이기위한 공개 구성의 추상화를 보여줍니다.* / 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 ()); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.