新聞中心
Spring入門(mén)—反射技術(shù)

創(chuàng)新互聯(lián)公司專(zhuān)注于嵐山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供嵐山營(yíng)銷(xiāo)型網(wǎng)站建設(shè),嵐山網(wǎng)站制作、嵐山網(wǎng)頁(yè)設(shè)計(jì)、嵐山網(wǎng)站官網(wǎng)定制、重慶小程序開(kāi)發(fā)公司服務(wù),打造嵐山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供嵐山網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
無(wú)參數(shù)的
Java代碼
- Class.forName(className).newInstance;
- //有參數(shù)的
- Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);
- //通過(guò)反射獲取屬性
- Introspector.getBeanInfo(Person.class).getPropertyDescriptors()
- //通過(guò)反射機(jī)制修改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);//允許訪問(wèn)private屬性
- setter.invoke(person, "小燕子");
- //通過(guò)反射機(jī)制修改bean字段的值
- Field field=Person.class.getDeclaredField("name");
- field.setAccessible(true);//允許訪問(wèn)private字段
- field.set( person , "sss");
- Class.forName(className).newInstance;
- //有參數(shù)的
- Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);
- //通過(guò)反射獲取屬性
- Introspector.getBeanInfo(Person.class).getPropertyDescriptors()
- //通過(guò)反射機(jī)制修改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);//允許訪問(wèn)private屬性
- setter.invoke(person, "小燕子");
- //通過(guò)反射機(jī)制修改bean字段的值
- Field field=Person.class.getDeclaredField("name");
- field.setAccessible(true);//允許訪問(wèn)private字段
- field.set( person , "sss");
Spring提供了聲明式的事務(wù)管理
軟件的解耦合,不是硬編碼
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代碼
- 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">
id="xx" class="junit.test.Person" lazy-init="true"> - t;/beans>
- version="1.0" encoding="UTF-8"?>
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">
id="xx" class="junit.test.Person" lazy-init="true">
怎么啟動(dòng)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");
默認(rèn)bean是容器啟動(dòng)時(shí)實(shí)例化,只在容器中創(chuàng)建一次,spring中的對(duì)象一直存在容器中,是單例模式
表 3.4. Bean作用域
作用域 描述
singleton 在每個(gè)Spring IoC容器中一個(gè)bean定義對(duì)應(yīng)一個(gè)對(duì)象實(shí)例。
prototype 一個(gè)bean定義對(duì)應(yīng)多個(gè)對(duì)象實(shí)例。
request 在一次HTTP請(qǐng)求中,一個(gè)bean定義對(duì)應(yīng)一個(gè)實(shí)例;即每次HTTP請(qǐng)求將會(huì)有各自的bean實(shí)例, 它們依據(jù)某個(gè)bean定義創(chuàng)建而成。該作用域僅在基于web的Spring ApplicationContext情形下有效。
session 在一個(gè)HTTP Session中,一個(gè)bean定義對(duì)應(yīng)一個(gè)實(shí)例。該作用域僅在基于web的Spring ApplicationContext情形下有效。
global session 在一個(gè)全局的HTTP Session中,一個(gè)bean定義對(duì)應(yīng)一個(gè)實(shí)例。典型情況下,僅在使用portlet context的時(shí)候有效。該作用域僅在基于web的Spring ApplicationContext情形下有效。
Spring入門(mén)—利用工廠方法創(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)工廠方法實(shí)例化
id="person" class="bean.PersonFactory" factory-method="createPerson"> - 使用實(shí)例工廠方法實(shí)例化
id="personFactory" class="bean.PersonFactory"> id="person2" factory-bean="personFactory" factory-method="createPerson2"> - //使用靜態(tài)工廠方法實(shí)例化
id="person" class="bean.PersonFactory" factory-method="createPerson"> - 使用實(shí)例工廠方法實(shí)例化
id="personFactory" class="bean.PersonFactory"> id="person2" factory-bean="personFactory" factory-method="createPerson2">
Spring入門(mén)—依賴注入
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代碼
id="xx" class="junit.test.Person" lazy-init="true"> name="idcard"> class="junit.test.IDCard"> name="no" value="9999"> id="xx" class="junit.test.Person" lazy-init="true"> name="idcard"> class="junit.test.IDCard"> name="no" value="9999">
第二種方法
Java代碼
id="aa" class="junit.test.IDCard"> name="no" value="88888888"> id="xx" class="junit.test.Person" lazy-init="true"> name="idcard" ref="aa"> id="aa" class="junit.test.IDCard"> name="no" value="88888888"> id="xx" class="junit.test.Person" lazy-init="true"> name="idcard" ref="aa">
為屬性配置null值
Java代碼
name="name"> - 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("我說(shuō)了");
- }
- }
name="name"> - 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("我說(shuō)了");
- }
- }
初始化bean執(zhí)行say方法相當(dāng)于測(cè)試單元的@BeforeClass
id="xxx" class="bean.Person" scope="singleton" lazy-init="false" init-method="say">
集合依賴注入
Java代碼
name="lists">
1111 2222 3333 4444 - for(String s : p.getLists){
- System.out.println(s);
- }
name="sets"> TTT YYY - for(String s : p.getSets){
- System.out.println(s);
- }
name="maps"> key="key1" value="value1"> key="key2" value="value2"> - for(String key : p.getMaps().keySet()){
- System.out.println(key+"="+ p. getMaps ().get(key));
- }
- Properties 是注入
name="propers"> key="proper1">value1 key="proper2">value2 - for(Object key : p.getPropers().keySet()){
- System.out.println(key+"="+ p.getPropers().get(key));
- }
【編輯推薦】
- Struts2教程:攔截器概述
- Struts2教程:上傳任意多個(gè)文件
- Struts2教程:在Action類(lèi)中獲得HttpServletResponse對(duì)象
- Struts2教程:使用Validation框架驗(yàn)證數(shù)據(jù)
- Struts2教程:使用validate方法驗(yàn)證數(shù)據(jù)
本文標(biāo)題:淺談Spring入門(mén)
新聞來(lái)源:http://m.fisionsoft.com.cn/article/cdsepij.html


咨詢
建站咨詢
