我如何学习Spring框架-第2部分(对初学者的帮助-初学者自己的工作)



下午好

邀请继续出版的灵感启发了我所以我继续。

这次,我们将考虑通过构造函数和setter实现依赖的主要方法。 在此处搜索所有资源

课程06.通过构造函数实现。


同样,以第2课中的项目为基础。

添加另一位诗人。 src \ main \ java \ spring \ impls \ Severyanin.java

package spring.impls; import spring.intarfaces.Lyricist; public class Severyanin implements Lyricist { @Override public String Generate() { return "   ,   ,\r\n" + "    …\r\n" + "  —    — ,\r\n" + ",  ,   . "; } } 

在配置文件src \ main \ resources \ 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="LyricistBean1" class="spring.impls.Poushkin"/> <bean id="LyricistBean2" class="spring.impls.Mayakovsky"/> <bean id="LyricistBean3" class="spring.impls.Severyanin"/> </beans> 

在二十世纪初,文学对决非常流行。

我们将安排两位诗人之间的文学对决。 为此,创建src \ main \ java \ spring \ impls \ Stage.java场景

 package spring.impls; import spring.intarfaces.Lyricist; public class Stage { private Lyricist lyr1; private Lyricist lyr2; public Stage(Lyricist lyr1, Lyricist lyr2) { this.lyr1 = lyr1; this.lyr2 = lyr2; } public void Act() { System.out.println(" :"); System.out.println(lyr1.Generate()); System.out.println(); System.out.println(" :"); System.out.println(lyr2.Generate()); System.out.println(); System.out.print("    "); if (Math.random() < 0.1) { System.out.println(" ."); } else { System.out.println(" ."); } } } 

原则上,您可以更改src \ main \ java \ spring \ main \ Start.java-它将起作用:

 package spring.main; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.impls.Stage; import spring.intarfaces.Lyricist; public class Start { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Lyricist lyr1 = context.getBean("LyricistBean2", Lyricist.class); Lyricist lyr2 = context.getBean("LyricistBean3", Lyricist.class); Stage myStage = new Stage(lyr1, lyr2); myStage.Act(); ((ConfigurableApplicationContext) context).close();//   } } 

我们开始-一切正常。 诗人发行了一部杰作,第二部很有可能获胜。 应当如此,1918年2月27日,在北方人诗人之王选举中的理工学院博物馆击败玛雅科夫斯基。 但是我们给了弗拉基米尔·弗拉基米罗维奇一个很小的机会。 也许在你的版本中他赢了。

现在,我们将在配置文件src \ main \ resources \ ApplicationContext.xml中进行所有设置,不值得在启动文件中显式配置Bean。

 <?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.xsd"> <bean id="LyricistBean1" class="spring.impls.Poushkin"/> <bean id="LyricistBean2" class="spring.impls.Mayakovsky"/> <bean id="LyricistBean3" class="spring.impls.Severyanin"/> <bean id="StageBean" class="spring.impls.Stage"> <constructor-arg ref="LyricistBean2" /> <constructor-arg ref="LyricistBean3" /> </bean> </beans> 

通常,bean是使用不带参数的默认构造函数创建的。 但是在我们的情况下,它将不起作用。 让我们将参数传递给Mayakovsky和Severyanin的其他生成的bin。

它仍然可以从src \ main \ java \ spring \ main \ Start.java类中删除所有不必要的内容

 package spring.main; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.impls.Stage; public class Start { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Stage myStage = context.getBean("StageBean", Stage.class); myStage.Act(); ((ConfigurableApplicationContext) context).close();//   } } 

我们开始。 一切正常。 Bean被创建。 北方人再次获胜。

无论如何,在我的现实中。

现在让我们看一下如何设置setter。

教训07.通过安装员的实现。


我们继续折磨上一个项目。
创建场景时创建诗人可能是错误的。
纠正此疏忽并稍微更改场景的类别。 让我们从src \ main \ java \ spring \ impls \ Stage.java中删除lyr1和lyr2的构造函数并添加设置器

 package spring.impls; import spring.intarfaces.Lyricist; public class Stage { private Lyricist lyr1; private Lyricist lyr2; public void setLyr1(Lyricist lyr1) { this.lyr1 = lyr1; } public void setLyr2(Lyricist lyr2) { this.lyr2 = lyr2; } public void Act() { System.out.println(" :"); System.out.println(lyr1.Generate()); System.out.println(); System.out.println(" :"); System.out.println(lyr2.Generate()); System.out.println(); System.out.print("    "); if (Math.random() < 0.1) { System.out.println(" ."); } else { System.out.println(" ."); } } } 

修改src \ main \ resources \ ApplicationContext.xml配置文件。 我们删除构造函数参数并添加setter值。

 <?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.xsd"> <bean id="LyricistBean1" class="spring.impls.Poushkin"/> <bean id="LyricistBean2" class="spring.impls.Mayakovsky"/> <bean id="LyricistBean3" class="spring.impls.Severyanin"/> <bean id="StageBean" class="spring.impls.Stage"> <property name="lyr1" ref="LyricistBean2"></property> <property name="lyr2" ref="LyricistBean3"></property> </bean> </beans> 

在这种情况下,不能触摸开始类别。 我们开始。 一切正常。 请注意,带有两个参数的构造函数不会从场景开始,但是在创建场景之后,将设置两个设置器。

待续...

Source: https://habr.com/ru/post/zh-CN421093/


All Articles