本文讲解了构造注入以及spring的基本使用方式,通过一个杂技演员的例子,讲述了依赖注入属性或者对象的使用方法。
如果想要使用spring来实现依赖注入,需要几个重要的步骤:
1定义主要的类和需要分离的属性。这里主要的类,是指程序的主要对象,在例子中是Juggler杂技员。而想要分离构造的属性,是它手中的袋子的数目beanBags。
2配置bean.xml。通过配置文件,确定主要的类和属性之间的关系,以及实现类。
3通过应用上下文,获取bean,并进行使用。
实例代码:
1表演者接口:Performer.java
packagecom.spring.test.action1; publicinterfacePerformer{ voidperform()throwsPerformanceException; }2杂技员:Juggler,继承了表演者接口
packagecom.spring.test.action1; publicclassJugglerimplementsPerformer{ privateintbeanBags=3; publicJuggler(){ } publicJuggler(intbeanBags){ this.beanBags=beanBags; } publicvoidperform()throwsPerformanceException{ System.out.println("Juggler"+beanBags+"beanBags"); } }3Spring配置文件:bean.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <beanid="duke"class="com.spring.test.action1.Juggler"> <constructor-argvalue="15"/> </bean> </beans>4应用上下文获取指定的bean
packagecom.spring.test.action1; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclasstest{ publicstaticvoidmain(String[]args){ ApplicationContextctx=newClassPathXmlApplicationContext("bean.xml"); Performerperformer=(Performer)ctx.getBean("duke"); try{ performer.perform(); }catch(PerformanceExceptione){ e.printStackTrace(); } } }执行结果如下:
Juggler15beanBags1例如,上面的杂技演员不仅仅会仍袋子,还会表演诗歌,那么诗歌这个对象就需要注入到表演者的构造函数中,可以如下表示会朗诵诗歌的杂技演员:PoeticJuggler
packagecom.spring.test.action1; publicclassPoeticJugglerextendsJuggler{ privatePoempoem; publicPoeticJuggler(Poempoem){ super(); this.poem=poem; } publicPoeticJuggler(intbeanBags,Poempoem){ super(beanBags); this.poem=poem; } publicvoidperform()throwsPerformanceException{ super.perform(); System.out.println("Whilereciting..."); poem.recite(); } }2诗歌对象:Sonnet29
packagecom.spring.test.action1; publicclassSonnet29implementsPoem{ privatestaticStringlines="嘛咪嘛咪哄"; publicSonnet29(){ } publicvoidrecite(){ System.out.println(lines); } }3Bean.xml配置文件如下
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <beanid="duke"class="com.spring.test.action1.Juggler"> <constructor-argvalue="15"/> </bean> <beanid="sonnet29"class="com.spring.test.action1.Sonnet29"/> <beanid="poeticDuke"class="com.spring.test.action1.PoeticJuggler"> <constructor-argvalue="15"/> <constructor-argref="sonnet29"/> </bean> </beans>4主要的执行代码,通过应用上下文获取制定的bean
packagecom.spring.test.action1; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclasstest{ publicstaticvoidmain(String[]args){ ApplicationContextctx=newClassPathXmlApplicationContext("bean.xml"); //Performerperformer=(Performer)ctx.getBean("duke"); Performerperformer1=(Performer)ctx.getBean("poeticDuke"); try{ //performer.perform(); performer1.perform(); }catch(PerformanceExceptione){ e.printStackTrace(); } } }5执行结果如下
一月24,20154:40:46下午org.springframework.context.support.AbstractApplicationContextprepareRefresh 信息:Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@35bf8de1:startupdate[SatJan2416:40:46CST2015];rootofcontexthierarchy 一月24,20154:40:46下午org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions 信息:LoadingXMLbeandefinitionsfromclasspathresource[bean.xml] 一月24,20154:40:46下午org.springframework.beans.factory.support.DefaultListableBeanFactorypreInstantiateSingletons 信息:Pre-instantiatingsingletonsinorg.springframework.beans.factory.support.DefaultListableBeanFactory@3401a0ad:definingbeans[duke,sonnet29,poeticDuke];rootoffactoryhierarchy Juggler15beanBags Whilereciting... 嘛咪嘛咪哄本文内容总结:注入属性,注入对象,
原文链接:https://www.cnblogs.com/xing901022/p/4246090.html