準備工作做完之后我們就開始依賴注入之旅了:
好了,廢話不多說了,下面即將進入我們的正題,依賴注入的三種方式(setter注入、構造注入、接口注入):
先建立三種方式都會用到的類和接口
接口:Arrow(箭)、Person(人)
實現類:ArrowImpl、PersonImpl
測試類:MainTest
Arrow接口:
package iocdi;public interface Arrow { public String getArrow();}
Person接口:
package iocdi;public interface Person { public void hunt();}
ArrowImpl類:
package iocdi;public class ArrowImpl implements Arrow { @Override public String getArrow() { return "an arrow"; }}
PersonImpl類:
package iocdi;public class PersonImpl implements Person { private Arrow arrow; @Override public void hunt() { System.out.println("I get " + arrow.getArrow() + " to hunt."); } //set注入一支箭 public void setArrow(Arrow arrow) { this.arrow = arrow; }}
MainTest類:
package iocdi;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathxmlApplicationContext;/** * @author ForeverLover */public class MainTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); System.out.println("-----------------setter注入-----------------"); Person p = ac.getBean("PersonImpl",PersonImpl.class); p.hunt(); }}
看到測試類會有疑問,ApplicationContext.xml從哪兒冒出來的,這里要說的就是Spring容器幫助我們去創建實例對象bean,在進程啟動時,Spring容器會自動加載此配置文件,解析通過配置文件配置的bean并創建對應類的實例,被調用者使用指定方式注入到調用者中,從而控制反轉和依賴注入。ApplicationContext.xml配置文件內容如下:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="ArrowImpl" class="iocdi.ArrowImpl"/> <bean id="PersonImpl" class="iocdi.PersonImpl"> <!-- setter注入 --> <property name="arrow" ref="ArrowImpl"/> </bean> </beans>
所有工作都完成了,然后從測試類MainTest的main方法執行一下,從控制臺可以看到結果,是OK的:
setter注入講完了,現在我們來聊聊構造注入了,所謂構造注入就是在實例化對象的時候就把參數傳給這個對象,我們知道對于JavaBean都必須有構造器,最少有一個無參構造器,到這我們可以繼續下面的構造注入,與setter注入不同之處在于,PersonImpl獲得ArrowImpl實例的方法,接口Arrow和Person不變,ArrowImpl類也不變,我們修改一下PersonImpl類和ApplicationContext.xml文件:
修改后的PersonImpl類:
package iocdi;public class PersonImpl implements Person { private Arrow arrow; public PersonImpl() {} public PersonImpl(Arrow arrow) { this.arrow = arrow; } @Override public void hunt() { System.out.println("I get " + arrow.getArrow() + " to hunt."); } }
修改后的ApplicationContext.xml文件:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="ArrowImpl" class="iocdi.ArrowImpl"/> <bean id="PersonImpl" class="iocdi.PersonImpl"> <!-- 構造注入 --> <constructor-arg ref="ArrowImpl"/> </bean> </beans>
然后再次運行一下MainTest測試類,看結果,仍然可行:
不同于setter注入和構造注入,接口注入無需在xml文件里配置bean,而利用Java反射創建實現接口類的實例。
讓我們來修改一下MainTest測試類和PersonImpl類:
修改的PersonImpl類:
package iocdi;public class PersonImpl implements Person { private Arrow arrow; @Override public void hunt() { try { Object obj = Class.forName("iocdi.ArrowImpl").newInstance(); arrow = (Arrow) obj; System.out.println("I get " + arrow.getArrow() + " to hunt."); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalaccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
修改的MainTest類:
package iocdi;/** * @author ForeverLover */public class MainTest { public static void main(String[] args) { try { Object obj = Class.forName("iocdi.PersonImpl").newInstance(); Person p = (Person) obj; System.out.println("-----------------接口注入-----------------"); p.hunt(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }}
運行一下測試類,從控制臺得出結果:
說到這順便說一下Lookup方法注入吧,對于Lookup注入需要一下幾個類:
Arrow類
Person抽象類
MainTest測試類
創建Arrow類:
package iocdi;import java.util.Random;public class Arrow { private String arrow; private String[] arrows = {"aaaaArrow", "bbbbArrow", "ccccArrow","ddddArrow","eeeeArrow", "ffffArrow","ggggArrow","hhhhArrow","iiiiArrow"}; public Arrow() { this.arrow = arrows[new Random().nextInt(9)]; } public void getArrow() { System.out.println("I get a " + arrow); } }
創建Person類:
package iocdi;public abstract class Person { public abstract Arrow createArrow(); public Arrow getArrow() { return new Arrow(); } }
創建MainTest類:
package iocdi;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author ForeverLover */public class MainTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Person p = ac.getBean("Person", Person.class); Arrow arrow1 = p.getArrow(); Arrow arrow2 = p.getArrow(); System.out.println(arrow1.equals(arrow2)); System.out.println("------------I am a dividing line------------"); Arrow arrow3 = p.createArrow(); Arrow arrow4 = p.createArrow(); System.out.println(arrow3.equals(arrow4)); }}
修改ApplicationContext.xml配置文件:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="Arrow" class="iocdi.Arrow"/> <bean id="Person" class="iocdi.Person"> <lookup-method name="createArrow" bean="Arrow" /> </bean> </beans>
最后測試一下,控制臺查看一下:
先說一下,分隔線前面之所以會輸出false是因為創建的兩個對象的引用不同,雖然這里打印出來是eeeeArrow和ddddArrow結果確實不同,因為是隨機的,即便有可能結果相同,兩個對象的引用也不相同。但是分隔線下面無論如何創建的兩個對象的引用相同。大家也可能會疑惑,為什么在配置文件里配置的抽象類也可以實例化對象,并且抽象類中的抽象方法createArrow()并沒具體實現卻可以創建Arrow實例,這里就跟Spring容器有關,其具體實現了abstarct類,如果createArrow()不是抽象方法,那abstract實現類也會覆蓋這個方法。
新聞熱點
疑難解答