[JAVA] Spring Framework self-study memo series_1

It was a memo when I self-taught the Spring framework the other day. ..

  1. History & basic concept Spring in 2002, Rod Jahnson in his masterpiece "Expert One-on-One J2EE Design and Development" For the first time, I submitted the core concept of Spring. Initially to replace EJB, but over the course of 10 years, it will have a wealth of features. It was made possible and became the main open source application framework within the Java platform. The main module is as follows   ●Spring Framework On the basis of the Spring ecosystem, all other modules run on top of the Spring Framework.   ●Spring Boot Development Framework and Developer can quickly create applications. Easily integrate other third party modules (such as MyBatis) If you place it easily, you can use it. Supports JSON format.   ●Spring Cloud Collecting modules for distributed development, Developer has these modules You can use to build microservices quickly. The following image (figure from the official website)     undefined

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

Spring Framework self-study memo series_1
[Personal memo] About Spring framework
spring framework Simple study memo (2): AOP
Spring retrospective memo
spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
[Spring Framework] Configuration split
JJUG CCC Spring 2018 memo
Spring Framework multilingual support
1. Start Spring framework from 1
Spring Framework Summary-About DI
Spring Shell usage memo
Spring boot memo writing (1)
MEMO about Rails 6 series
Spring boot memo writing (2)
JJUG CCC 2018 Spring participation memo
Play Framework Study Memo Database ①
Spring Security usage memo CSRF
About Spring Framework context error
Spring Security usage memo Run-As
Spring Security Usage memo Method security
Spring Security usage memo Remember-Me
Spring Security usage memo CORS
Spring Security usage memo test
Spring boot controller method memo
Spring Security usage memo Authentication / authorization
◆ Spring Boot + gradle environment construction memo
Spring Framework 5.0 Summary of major changes
Spring Security usage memo response header
Java --Jersey Framework vs Spring Boot
Memo after the first Spring project-MVC-
Spring Framework tools for Java developer
Spring Security usage memo session management
Upgrade spring boot from 1.5 series to 2.0 series
Spring Security usage memo Basic / mechanism
A memo that touched Spring Boot
Spring thorough introduction version upgrade memo
Memo after the first Spring project-Database-
Test Spring framework controller with Junit