⬆️ ⬇️

How I study the Spring framework - part 2 (help for beginners is the work of the beginners themselves)





Good day!



I was inspired by the invitation to continue publishing, so I continue.

')

This time we will look at the main options for implementing dependencies — through the constructor and through setters. All sources look here



Lesson 06. Implementation through the designer.



Again we take the project from lesson 2 as a basis.



Add another poet. 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" + ",  ,   . "; } } 


Register a class in the src \ main \ resources \ ApplicationContext.xml configuration file



 <?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> 


At the beginning of the twentieth century, literary duels were very popular.



Let's arrange a literary duel between two poets. To do this, create a scene 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(" ."); } } } 


In principle, you can change the src \ main \ java \ spring \ main \ Start.java - and everything will work:



 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();//   } } 


We start - everything works. The poets gave out one masterpiece, the second, most likely, won. So it should be, February 27, 1918 in the Polytechnic Museum at the Election of the King of Poets Northerner defeated Mayakovsky. But we gave Vladimir Vladimirovich a small chance. Maybe in your version he won.



Now we will put all the settings in the src \ main \ resources \ ApplicationContext.xml configuration file, it is not necessary to configure the bins explicitly in the start file.



 <?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> 


Usually, beans are created with a default constructor with no arguments. But in our case it will not work. Let us pass as arguments references to other created bins of Mayakovsky and Severyanin.



It remains to remove all unnecessary from the class 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();//   } } 


We start. Everything is working. Bins are created. Northerner won again.



Anyway, in my reality.



Now let's see how the setters can be configured.



Lesson 07. Implementation through setters.



Continue to torment the previous project.

Probably wrong when creating scenes to create and poets.

Correct this mistake and slightly change the class of the scene. Remove the constructor and add the setters for lyr1 and lyr2 from src \ main \ java \ spring \ impls \ Stage.java



 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(" ."); } } } 


Modify the src \ main \ resources \ ApplicationContext.xml configuration file. Remove the constructor arguments and add the values ​​of the setters.



 <?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> 


Class start in this case, you can not touch. We start. Everything is working. Note that for the scene the constructor with two parameters does not start, but after creating the scene two setters are set.



To be continued…

Source: https://habr.com/ru/post/421093/



All Articles