今天小編給大家分享一下Spring反射技術怎么用的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
站在用戶的角度思考問題,與客戶深入溝通,找到遷安網站設計與遷安網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網站設計、成都網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣、域名注冊、虛擬空間、企業(yè)郵箱。業(yè)務覆蓋遷安地區(qū)。
無參數的
Java代碼
Class.forName(className).newInstance; //有參數的 Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”); //通過反射獲取屬性 Introspector.getBeanInfo(Person.class).getPropertyDescriptors() //通過反射機制修改bean屬性的值 Person person=(Person)Class.forName(className).getDeclaredConstructor(String.class).newInstance("黎明"); PropertyDescriptor[] ps = Introspector.getBeanInfo(Person.class).getPropertyDescriptors(); for(PropertyDescriptor p :ps){ System.out.println(p.getName()); if(p.getName().equals("name")){ Method setter=p.getWriteMethod(); if(setter!=null){ setter.setAccessible(true);//允許訪問private屬性 setter.invoke(person, "小燕子"); //通過反射機制修改bean字段的值 Field field=Person.class.getDeclaredField("name"); field.setAccessible(true);//允許訪問private字段 field.set( person , "sss"); Class.forName(className).newInstance; //有參數的 Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”); //通過反射獲取屬性 Introspector.getBeanInfo(Person.class).getPropertyDescriptors() //通過反射機制修改bean屬性的值 Person person=(Person)Class.forName(className).getDeclaredConstructor(String.class).newInstance("黎明"); PropertyDescriptor[] ps = Introspector.getBeanInfo(Person.class).getPropertyDescriptors(); for(PropertyDescriptor p :ps){ System.out.println(p.getName()); if(p.getName().equals("name")){ Method setter=p.getWriteMethod(); if(setter!=null){ setter.setAccessible(true);//允許訪問private屬性 setter.invoke(person, "小燕子"); //通過反射機制修改bean字段的值 Field field=Person.class.getDeclaredField("name"); field.setAccessible(true);//允許訪問private字段 field.set( person , "sss");
Spring提供了聲明式的事務管理
軟件的解耦合,不是硬編碼
Spring 需要的jar
Dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面編程(AOP),還需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,還需要下列jar文件
lib\j2ee\common-annotations.jar
配置文件beans.xml
Java代碼
<?xml version="1.0" encoding="UTF-8"?> t;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="xx" class="junit.test.Person" lazy-init="true"> </bean> t;/beans> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="xx" class="junit.test.Person" lazy-init="true"> </bean> </beans>
怎么啟動spring容器Java代碼
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person s = (Person)context.getBean("xx"); ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person s = (Person)context.getBean("xx");
默認bean是容器啟動時實例化,只在容器中創(chuàng)建一次,spring中的對象一直存在容器中,是單例模式
表 3.4. Bean作用域
作用域 描述
singleton 在每個Spring IoC容器中一個bean定義對應一個對象實例。
prototype 一個bean定義對應多個對象實例。
request 在一次HTTP請求中,一個bean定義對應一個實例;即每次HTTP請求將會有各自的bean實例, 它們依據某個bean定義創(chuàng)建而成。該作用域僅在基于web的Spring ApplicationContext情形下有效。
session 在一個HTTP Session中,一個bean定義對應一個實例。該作用域僅在基于web的Spring ApplicationContext情形下有效。
global session 在一個全局的HTTP Session中,一個bean定義對應一個實例。典型情況下,僅在使用portlet context的時候有效。該作用域僅在基于web的Spring ApplicationContext情形下有效。
Spring入門—利用工廠方法創(chuàng)建bean
Java代碼
public class PersonFactory { public static Person createPerson(){ return new Person(); } public Person createPerson2(){ return new Person(); } public class PersonFactory { public static Person createPerson(){ return new Person(); } public Person createPerson2(){ return new Person(); } }
Java代碼
//使用靜態(tài)工廠方法實例化 <bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean> 使用實例工廠方法實例化 <bean id="personFactory" class="bean.PersonFactory"></bean> <bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean> //使用靜態(tài)工廠方法實例化 <bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean> 使用實例工廠方法實例化 <bean id="personFactory" class="bean.PersonFactory"></bean> <bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean>
Spring入門—依賴注入
Java代碼
public class Person { private Integer id; private String name="aa"; private IDCard idcard; public IDCard getIdcard() { return idcard; } public void setIdcard(IDCard idcard) { this.idcard = idcard; } public Person(String name) { this.name = name; } public Person() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class IDCard { private String no; public String getNo() { return no; } public void setNo(String no) { this.no = no; } } public class Person { private Integer id; private String name="aa"; private IDCard idcard; public IDCard getIdcard() { return idcard; } public void setIdcard(IDCard idcard) { this.idcard = idcard; } public Person(String name) { this.name = name; } public Person() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class IDCard { private String no; public String getNo() { return no; } public void setNo(String no) { this.no = no; } }
***種方法
Java代碼
<bean id="xx" class="junit.test.Person" lazy-init="true"> <property name="idcard"> <bean class="junit.test.IDCard"> <property name="no" value="9999"></property> </bean> </property> </bean> <bean id="xx" class="junit.test.Person" lazy-init="true"> <property name="idcard"> <bean class="junit.test.IDCard"> <property name="no" value="9999"></property> </bean> </property> </bean>
第二種方法
Java代碼
<bean id="aa" class="junit.test.IDCard"> <property name="no" value="88888888"></property> </bean> <bean id="xx" class="junit.test.Person" lazy-init="true"> <property name="idcard" ref="aa"> </property> </bean> <bean id="aa" class="junit.test.IDCard"> <property name="no" value="88888888"></property> </bean> <bean id="xx" class="junit.test.Person" lazy-init="true"> <property name="idcard" ref="aa"> </property> </bean>
為屬性配置null值
Java代碼
<property name="name"><null/></property> public class Person { private String name="ss"; public Person(){} public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void say(){ System.out.println("我說了"); } } <property name="name"><null/></property> public class Person { private String name="ss"; public Person(){} public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void say(){ System.out.println("我說了"); } }
初始化bean執(zhí)行say方法相當于測試單元的@BeforeClass
<bean id="xxx" class="bean.Person" scope="singleton" lazy-init="false" init-method="say">
集合依賴注入
Java代碼
<property name="lists"> <list> <value>1111</value> <value>2222</value> <value>3333</value> <value>4444</value> </list> </property> for(String s : p.getLists){ System.out.println(s); } <property name="sets"> <set> <value>TTT</value> <value>YYY</value> </set> </property> for(String s : p.getSets){ System.out.println(s); } <property name="maps"> <map> <entry key="key1" value="value1"></entry> <entry key="key2" value="value2"></entry> </map> </property> for(String key : p.getMaps().keySet()){ System.out.println(key+"="+ p. getMaps ().get(key)); } Properties 是注入 <property name="propers"> <props> <prop key="proper1">value1</prop> <prop key="proper2">value2</prop> </props> </property> for(Object key : p.getPropers().keySet()){ System.out.println(key+"="+ p.getPropers().get(key)); }
以上就是“Spring反射技術怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注創(chuàng)新互聯行業(yè)資訊頻道。
分享題目:Spring?反射技術怎么用
分享URL:http://chinadenli.net/article36/jgcesg.html
成都網站建設公司_創(chuàng)新互聯,為您提供標簽優(yōu)化、服務器托管、Google、品牌網站設計、定制開發(fā)、網站制作
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯