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 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.
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)
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 **
As mentioned above, I'll delve into some of the nice features of Spring.
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" **.
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.
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.
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.
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.
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