Chapter 2 The second spring-boot program and Chapter 2 spring-boot

Source: Internet
Author: User

Chapter 2 The second spring-boot program and Chapter 2 spring-boot

The code in the previous section is the spring-boot entry program, which is also a program in the official documentation. This section will introduce the method recommended by the official spring-boot documentation to develop code, and introduce calls at the service layer in spring development.

1. The code structure is as follows:

2. pom. xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 5 <modelVersion> 4.0.0 </modelVersion> 6 7 <groupId> com. xxx </groupId> 8 <artifactId> myboot </artifactId> 9 <version> 1.0-SNAPSHOT </version> 10 11 <properties> 12 <java. version> 1.8 </java. version> <! -- Official recommendation --> 13 </properties> 14 <! -- Introducing spring-boot-starter-parent for parent is the best method. 15. However, sometimes we may introduce our own parent. There are two solutions: 16. 1) our own parent pom. xml parent is set to spring-boot-starter-parent (not verified, but it feels feasible) 17 2) use the method in the springboot document: see spring-boot-1.2.5-referencecepage 18 --> 19 <parent> 20 <groupId> org. springframework. boot </groupId> 21 <artifactId> spring-boot-starter-parent </artifactId> 22 <version> 1.2.5.RELEASE </version> 23 </parent> 24 25 <! -- <DependencyManagement> 26 <dependencies> 27 <dependency> 28 Import dependency management from Spring Boot29 <groupId> org. springframework. boot </groupId> 30 <artifactId> spring-boot-dependencies </artifactId> 31 <version> 1.2.5.RELEASE </version> 32 <type> pom </type> 33 <scope> import </scope> 34 </dependency> 35 </dependencies> 36 </dependencyManagement> --> 37 38 <! -- Introduce actual dependencies --> 39 <dependencies> 40 <dependency> 41 <groupId> org. springframework. boot </groupId> 42 <artifactId> spring-boot-starter-web </artifactId> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <! -- Used to compress an application into a jar that can be run directly (this jar is used in the production environment). It is worth noting that if spring-boot-starter-parent is not referenced for parent, 49. The second method is adopted, and corresponding changes must be made here --> 50 <plugin> 51 <groupId> org. springframework. boot </groupId> 52 <artifactId> spring-boot-maven-plugin </artifactId> 53 </plugin> 54 </plugins> 55 </build> 56 </project>View Code

Note: The pom. xml file is exactly the same as that in the previous section.

 

3. Application. java

1 package com. xxx. firstboot; 2 3 import org. springframework. boot. springApplication; 4 import org. springframework. boot. autoconfigure. springBootApplication; 5 6/** 7 * @ EnableAutoConfiguration: Annotation of spring boot, which is generally used only for the main class. 8 * is a key part for enabling xml-free configurations, specify the scan package path as the package of the main class modified by the package (that is why the main class should be placed under the root package path) 9*10 * @ ComponentScan scans the package. @ EnableAutoConfiguration specifies the 11*12 * main class to be located under the root package path, scan after convenience (We generally recommend that you locate your main application class in a root package above other classes .) 13 */14 @ SpringBootApplication // same as @ Configuration + @ EnableAutoConfiguration + @ ComponentScan15 public class Application {16/** 17 * spring boot entry, which is included in the entire sub-project, 18 * only one main method can be used. Otherwise, spring boot cannot start up 19 */20 public static void main (String [] args) {21 SpringApplication. run (Application. class, args); 22} 23 24}View Code

Note:

  • The main class must be located in the root package path (for example, com. xxx. firstboot ).
  • Each jar (that is, each sub-project) must have a primary method to start the jar (that is, a microservice)
  • Add annotation @ SpringBootApplication to the main class. This annotation is equivalent to adding the following three Annotations:
    • @ Configuration: This annotation indicates that the class is managed by the spring container.
    • @ EnableAutoConfiguration: This annotation is a key part for enabling xml-free configuration.
    • @ ComponentScan: This annotation specifies the scan package (if the main class is not located under the root path, you need to specify the scan path here), similar to the spring package scan Annotation

 

4. application. properties

1 # user info2 user. id = 13 user. username = zhaojigang4 user. password = 123View Code

Note:

  • The application. properties file is the default file of spring-boot. Generally, various configurations (including data source configuration and httpclient configuration) are configured here.

 

5. User. java

1 package com. xxx. firstboot. domain; 2 3 import org. springframework. boot. context. properties. configurationProperties; 4 import org. springframework. stereotype. component; 5 6/** 7 * @ ConfigurationProperties (prefix = "user") 8 * automatically reads the application. properties (the default file for spring-boot) file. * attribute 9 * If @ ConfigurationProperties is not used, you can use @ Value ("$ {user. id} ") to specify the attribute values for 10*11 *. Note: To use @ ConfigurationProperties and @ Value, add @ Component to the bean, 12 * in subsequent use of this class, the class needs to be directly injected using the @ Autowire annotation so that automatic injection of these attributes will take effect, 13 * For details, see "UserService" 14 */15 @ Component16 @ ConfigurationProperties (prefix = "user") 17 public class User {18 19 // @ Value ("$ {user. id} ") 20 private int id; 21 22 // @ Value (" wangna ") 23 private String username; 24 25 private String password; 26 27 public int getId () {28 return id; 29} 30 31 public void setId (int id) {32 this. id = id; 33} 34 35 public String getUsername () {36 return username; 37} 38 39 public void setUsername (String username) {40 this. username = username; 41} 42 43 public String getPassword () {44 return password; 45} 46 47 public void setPassword (String password) {48 this. password = password; 49} 50 51}View Code

Note:

  • This class is a common model. In the ssm framework, we did not assign this model to the spring container for management. Here, we use the @ Component annotation to submit it to the spring container for processing, in this way, the model can be directly injected into its usage class.
  • The @ ConfigurationProperties (prefix = "user") annotation is added to this class, which means that the application can be automatically scanned. configure the prefix of the properties file, and configure each attribute of the class according to the name.
  • You can also use the @ Value annotation to separately repeat the Value in the attribute. Of course, the premise is that @ ConfigurationProperties is not configured. If the annotation is configured, the @ Value annotation is invalid.

 

6. UserService. java

1 package com. xxx. firstboot. service; 2 3 import org. springframework. beans. factory. annotation. autowired; 4 import org. springframework. stereotype. service; 5 6 import com. xxx. firstboot. domain. user; 7 8 @ Service 9 public class UserService {10 11 @ Autowired12 private User user; 13 14 public User getUser () {15 return user; 16} 17 18}View Code

Note:

  • User is injected directly here, which is exactly the model above the class

 

7. UserController. java

1 package com. xxx. firstboot. web; 2 3 import org. springframework. beans. factory. annotation. autowired; 4 import org. springframework. web. bind. annotation. requestMapping; 5 import org. springframework. web. bind. annotation. restController; 6 7 import com. xxx. firstboot. domain. user; 8 import com. xxx. firstboot. service. userService; 9/** 10 * @ RestController: spring mvc annotation. 11 * is equivalent to the combination of @ Controller and @ ResponseBody, json12 */13 @ RestController14 @ RequestMapping ("/user") 15 public class UserController {16 17 @ Autowired18 private UserService userService; 19 20 @ RequestMapping ("/getUser ") 21 public User getUser () {22 return userService. getUser (); 23} 24 25}View Code

 

Note:

  • This class is actually the most basic and commonly used method for developing a spring-boot program. (In microservice applications, parent-child module development is used in the series similar to "Java Enterprise Application Development practices". I will talk about it later)
  • Compared with ssm, spring-boot can easily read attribute files. Three methods are commonly used to read attribute files.
    • Use FileUtil to read: see chapter 1 attribute file operation tool class
    • Use the annotation above for implementation (the most recommended method)
    • You can use the Environment class to obtain it (this may be the wrong class name)

For spring-boot, there are many integrated jar packages (see below). We can introduce corresponding jar packages as needed, but there is no jar integrated with mybatis.

Dependency packages related to spring-boot (you can introduce them as needed ):

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.