1. Start Spring framework from 1

What is Spring framework

In the first place, the existence of a framework is indispensable for Java Web application development. One of the most used frameworks is this Spring framework. Spring has a long history and has been improved as open source since the first Spring was released in 2002. It seems that the main reasons why Spring is popular are the ease of changing system specifications, the ease of program testing, expandability, and system stability.

Spring overview

Spring can be used to develop applications and software in a wide range of genres depending on how the module is used. Among them, if you mention some familiar ones that are often used in actual work etc. ・ I / O project ·application development ・ Web application development ・ Software development on the cloud ・ Database management software ・ Android application development You can see that it is used in a wide variety of genres, even if only a few are mentioned.

Benefits of Spring

Some specific benefits of Spring ** ・ High-level development environment with DI container and AOP framework ** (AOP reference article) ** ・ Abundant modules ** ** ・ RESTful support ** (RESTful API reference article) ** ・ IDE (STS) for Spring is released for free ** ** ・ Large-scale database NoSQL support ** (NoSQL reference article)

Disadvantages of Spring

There are not only advantages but also disadvantages. Overall, there are many disadvantages in terms of learning costs. ** ・ The scale of the framework is large and it is difficult to grasp the whole ・ It is difficult to use the DI container correctly. ・ Advanced Java knowledge is required to understand correctly **

Typical functions of Spring

As mentioned above, I'll delve into some of the nice features of Spring.

DI container

What is DI (Dependency injection) in the first place? When I look it up, I often see "dependency injection", but I don't really understand the meaning even if I listen to this much. Looking at the wiki, it is written as follows.

Eliminate dependencies between components from the program source code It is a software pattern that enables injection with an external configuration file. When creating a program that uses dependency injection, the relationships between components are described using interfaces. Do not specify a specific component. By describing which component to use in an external file, the dependency between components can be excluded from the source code.

What I want to do is ** "Let's loosely connect the classes and let the actual data go out" **.

Benefits of DI

Since component A and component B are described in the configuration file, Eliminates the need to define relationships within components. by this, ** If component A is not completed, a compile error will occur and component B cannot be created ** Development can be done without the influence of the work order.

It also makes it easier to unit test your classes. One of the problems when writing test code is the dependency problem. In the above example, to test component B, you would have to extend the test to component A. By using a mock object that implements the interface in the DI container It enables you to test only the class you want to test regardless of the completion of the class itself or bugs. Also, even if component A is modified, the test code of component B can be used as it is.

DI usage example

The configuration file is xml as usual. ・ Servlet-context.xml

1<context:annotation-config />
2<context:component-scan base-package="com.example.controller" />

com.example.controller Set all the following classes so that they can be DI.

@Controller
public class ExampleController {

    @Autowired
    private ExampleService exampleService ;

    @RequestMapping('/')
    @ResponseBody
    public String find(String itemId) {
        ItemEntity item = exampleService.findItemData(itemId);
        return item.getItemName();
    }
}

DI is added with @Autowired annotation to ExampleService. This will allow you to use the object without new.

Spring MVC and data access

It is a framework for developing web applications in Spring, and uses MVC as an architectural pattern. As the name implies, it is a framework for the controller layer to realize MVC. Needless to say, as an overview of MVC

** ・ Model: Model. Process business logic (the core function of the application) -View: View. Display information -Controller: Controller. Accepts events from users and controls processing and display **

In Spring, Model operation and value passing from View are realized based on the controller class. In addition, the controller class or Logic class (usually across transaction boundaries) makes domain-tier service class calls.

Annotation

In Spring, processing is clearly divided by various annotations. There are Spring annotations "@Component, @Service, @Repository, @Controller" etc.

@Controller Grant it to the controller layer class in Spring MVC. Just give it this and Spring will recognize this class as a controller class.

In Spring MVC, when a certain URL is requested, the request and Java processing method are mapped in the flow of "calling the init () method of class A".

Not only the URL but also the condition that determines the method to perform this process header, cookies, http methods (GET, POST, etc.), parameters, etc. It is possible to use almost all the information sent from the browser. It is possible to pass the above values with annotations.

Also, annotate the method in the controller class with @ModelAttribute and add it. It is possible to add the return value of the method to Model and return it to the view implemented by JSP etc.

@Service It is assigned to the service layer class (business logic, etc.). It is mainly called from the controller side and calls the class that accesses DB such as DAO.

@Repository Assign to the data layer class (DB access class). It is called from a service such as a class called DAO to access the DB.

@Component Give it to the class you want to register in the Spring DI container. You will be able to get an instance with @Autowired with annotations for components under component-scan.

next time

This is the end of Spring article # 1. Next time, I will delve into various annotations and the functions I have written so far. We will actually create a small system.

Recommended Posts

1. Start Spring framework from 1
[Spring Framework] Configuration split
Spring Framework multilingual support
About the official start guide of Spring Framework
Spring Framework Summary-About DI
A record of studying the Spring Framework from scratch
[Personal memo] About Spring framework
Try Spring Boot from 0 to 100.
Spring + Gradle + Java Quick Start
Spring Framework self-study memo series_1
About Spring Framework context error
Spring Framework 5.0 Summary of major changes
spring framework Simple study memo (2): AOP
Java --Jersey Framework vs Spring Boot
Spring Framework tools for Java developer
Java, interface to start from beginner
Upgrade spring boot from 1.5 series to 2.0 series
Test Spring framework controller with Junit
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1
call spring management bean from pojo
How to get the setting value (property value) from the database in Spring Framework
Automatically generated memo from swagger condegen (spring) -1
Introducing Basic Authentication on Heroku [Spring Framework]
Spring Boot 2.0.0 does not start built-in tomcat
Major changes related to Spring Framework 5.0 Test
Spring Framework bean definition XML: custom tag
Story when moving from Spring Boot 1.5 to 2.1
Why spring consider as a lightweight framework
Spring Framework study notes [Part 1] DI container
Start web application development with Spring Boot
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
About the initial display of Spring Framework
Transition from Struts2 to Spring MVC (Controller)
Post to Slack from Play Framework 2.8 (Java)
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
Major changes in Spring Framework 5.0 core functionality
Features of spring framework for java developers