This chapter contents: JAVA EE> Spring> Spring Boot
A, JAVA EE Introduction
1.1 Java ee advantages : the end of the technical state of disorder Web development, programmers, architects with the same kind of thinking to think about how to structure and develop applications (refer to enterprise applications and Web sites). Because it provides enterprise application technology to achieve standardized , simplified development.
Implementation Specification:
-
-
-
- Web Support: Rise B / S structure, JAVA EE has a corresponding Servlet specification, the predetermined Web container, Servlet assembly, as well as JSP & JSTL processing dynamic page.
- Transaction support: provides transaction manager.
- Messaging service: JMS
- Database persistence layer: EJB specification, and later proposed a more practical operability of the JPA, the method used to access the database
- Container: Container, such as a management component (EJB and Servlet), and provide the required service components, such as JTS, JMS and the like.
-
-
1.2 Java ee disadvantages :
-
-
- Too complex: the development and deployment more complex way
- Distributed pursuit: Most applications are not all JAVA EE Jiading distributed systems.
- It can not be combined with timely popular open source technologies: such as JMS message handling only
- Expensive
-
Two, Spring
2.1 Spring IoC container and AOP
IoC Core Container
Business in object code into the Spring Container, Spring containers coming through adding annotation profiles on the business object code to initialize and enhancements.
For an object annotation @Container declaration, Spring would think that this object is a Web Container, if this object methods are @RequestMapping comment, it will be initiated by the client HTTP requests into JAVA method calls.
EG Spring usually provide some @Controller, @ Service, @ Conponent, @ Configuration annotation, only use these annotated classes, will draw attention to the Spring container, and to manage and enhance the meaning of the object based on the annotation.
// Spring Container initialization HelloworldController instance in the container, for /sayhello.html request initiated by the client, say the method performs, and automatically according to the correspondence request parameter name say method declaration.
@Controller public class HelloworldController { @RequestMapping ( "/sayhello.html" ) public @ResponseBody String say (name String) return "Hello" + name }
egSpring can enhance management and any objects, such as the common annotation @Service typically used on business logic. Container management can also be managed Bean Managed Bean provide enhanced and, as a @Service has been annotated UserService classes, in class HelloworldController using an automatic injection @Autowired this example.
@Controller public class HelloworldController{ @Autowired UserService userService; }
The AOP (Aspect Oriented Programming)
Dynamically add functionality to the target object by way of pre-compiled or run time. AOP separate enterprise application business logic and system-level services such as transaction services, as well as auditing, secure access to applications and other code. For example, to achieve user access control, Function may use a custom annotations for each method of the Controller, with, Spring AOP permission verification function to add a user to dynamically Controller each method, similar to the following:
@RequestMapping ( "/ adduser.html" ) @Function ( "user.add" ) public @ResponseBody the addUser String (String name) { ...... } // Function annotation is a custom annotation, receiving a string It represents Controller method corresponding service function. Whether the user can access the "user.add" function, the configuration in the database.
三、Spring Boot
Spring Boot Spring simplifies application development, does not require configuration can run Spring applications. To provide system-level services through Starter, such as developing a Web application, only need to declare in pom.xml (maven) at: <artifactId> spring-boot-starter-web </ artifactId>
Spring Boot has many advantages: does not require complex configuration such as to provide built-in Tomcat or Jetty container. By relying on the jar package management, automated assembly techniques.
四、Hello,Spring Boot
4.1 Application project turned into Spring Boot:
A simple web application: First, create a Maven project. Then find the pom.xml file, add the following entry to make the project into a Spring Boot application:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M4</version> //版本可能会有问题。 </parent>
Because Web 4.2 applications are built, so add a spring-boot-starter-web dependent, add the following
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependencies>
Note: spring-boot-dependencies will default to the built-in Tomcat, and support for Spring MVC, RESTFul service.
4.3 Spring-boot into the general category class
For a general Java class Ch1Application, @SpringBootApplication adding a line in front, then add a line SpirngApplication.run (C h1Application.class, args) in the main method
Code Example:
package com.bee.sample.ch1;
@SpringBootApplication public class Ch1Application{ public static void main(String[] args){ SpringApplication.run(Ch1Application.class,args); } }
This class would be entirely a Spring Boot application, you can run. Just because no written Controller, it is not accessible through the browser, and then create a class called HelloworldController, the package name com.bee.sample.chq.controller, as shown in FIG.
package com.bee.sample.ch1.controller @Controller public class HellowwoeldController{ @RequestMapping("/say.html") public@ResbonseBody String say(){ return "Hello Spring Boot"; } }
Notes: @Controller is Spring MVC annotation indicating such is responsible for processing web requests
Is @RequestMapping Spring MVC annotation indicating if the requested path matching process is called annotated
@ResponseBody indicates that this method returns the text rather than the name of the view
4.4 hot deployment
In the above example, it must re-run at startup when modifying this class, very inconvenient. The solution is Spring-boot-devtools dependencies. After the modified class, spring boot will automatically restart.
<dependency> <groupId>org.springframework.boot<groupId> <artifactId>spring-boot-devtools<artifacId> <optional>ture</optional> </dependency>
4.5 Adding REST support
There are many ways calls between systems, RESTFul is a good one. Spring Boot can easily support RESTFul applications. Such as:
@RestController public class UserReditRestController { @RequestMapping (value = "/ usercredit / {id}" ) public Integer getCreditLevel (@PathVariable String id) { // simulate the user's credit rating id return . 3 ; } } // at this time is the access path http://127.0.0.1:8080/usercredit/123 number 123 is arbitrary. Id getCreditLevel of the corresponding parameter.
Note: For mutual access to multiple systems, it is best not to directly access each other's databases, but should be similar RESTFul architecture, encapsulates the logic interface. In this way, the other system database changes, business logic changes or version upgrades. It will not affect other systems.