It was a memo when I self-taught the Spring framework the other day. ..
The core mechanisms of Spring are IoC (Inversion of Control) and AOP (Aspect Oriented Programming).
About the IoC For traditional methods, when you want an instance of a class, Developer should New and use it yourself. Regarding Ioc of Spring, IocContainer of Framework will do all the creation of class instances, so Developer can just be used directly. Compared to the traditional formula, the flow is reversed, so it is an inversion of control. Traditional, for the following classes public class Book { private String id; private String name; private int price; } When you want an instance, create it with the following New. Book bk = new Book(); If you leave it to Spring's IoC Container, you can do the following. ● Pattern realized by XML placement ① Create an empty Spring Starter project ② In the resources folder, create the following empty XML with IoCbeans.xml
<Beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
③ Using the bean tag, IoC Container creates an instance. <bean id="book" class="learningAOP.learningAOP.bean.Book"></bean> ● id: instance name class: Target class In the following example, an instance is created without a Constructor. ④ Get an instance ● Get using id //ApplicationContextでXMLをロード ApplicationContext applicationContext = new ClassPathXmlApplicationContext("IoCbeans.xml"); //bookとのIDでインスタンスを取得 Book book = (Book) applicationContext.getBean("book"); System.out.println(book); Output is below ID: nullname: null price: 0
All the values of each property are initial values. If you want to set the value of a property, the XML definition is You can change it as follows. PS: Book class Getter & Setter is required <bean id="book" class="learningAOP.learningAOP.bean.Book"> <property name="ID" value="1"></property> <property name="name" value="demo"></property> <property name="price" value="100"></property> </bean>
Result of running again ID: 1name: demo price: 100 ● Get using class type //Beanのクラスを使って、インスタンスを取得 Book book1 = applicationContext.getBean(Book.class); System.out.println(book1); The disadvantage is that if there are multiple bean definitions of the same class, an error will occur. ⑤ When there is a Constructor //引数付けConstructor public Book(String id, String name, int price) { this.ID = id; this.name = name; this.price = price; } The XML Bean definition is as follows <!-Create instance with constructor-> <bean id="bookcon" class="learningAOP.learningAOP.bean.Book"> <constructor-arg name="ID" value="2"> <constructor-arg name="name" value="demo2"> <constructor-arg name="price" value="200"> </bean>
In the Main function, get the instance as follows Book bookcon= (Book)applicationContext.getBean("bookcon"); System.out.println(bookcon); Output is ID: 2 name: demo2 price: 200 The above is a simple instance control, but when there are multiple classes and there are dependencies, Realize as follows ( In the Book class, there are members of the class with Author, in short, the Book class depends on the Author class. public class Author { private String ID; private String name; private int age; } Book class is below public class Book { private String id; private String name; private int price; private Author author } PS: Getter & Setter Omitted here In XML, define the dependency as follows <!-Create Author instance-> <bean id="author" class="learningAOP.learningAOP.bean.Author"> <property name="ID" value="1"> <property name="name" value="demo"> <property name="age" value="30"> </bean> <bean id="book" class="learningAOP.learningAOP.bean.Book"> <property name="ID" value="1"> <property name="name" value="demo"> <property name="price" value="100"> <!-Add dependency-> <property name="author" ref="author"> Here, ref expresses the dependency between classes and calls DI (Dependency Injection). This is a concrete way to realize the IoC. If you try it Book bookcon= (Book)applicationContext.getBean("book"); System.out.println(bookcon) Output is ID: 1 name: demo price: 100 Author: ID: 1 name: demo age: 30 PS: In the latest Spring boot, Ioc & DI is all realized by annotation.
Sample source https://github.com/panguohua/learnSpring.git
Recommended Posts