Recent contact with a bit SpringBoot, a "convention is greater than the configured" Micro Framework. Probably use it, and indeed our traditional spring than in the configuration above simplifies a lot, a lot of things do not need to be configured through the web.xml and applicationConetxt.xml. And to start the service, there is a built-in tomcat and jboss container, is also very easy. In general, in fact, it is a collection of libraries that can be used to build systems of any item. I created a maven project, write an example of a case and start a web connection to the database.
1.Web
POM configuration:
1 |
<> |
Start springboot then, just springboot-starter-web on it. Then processing method comprising the preparation of a class of HTTP requests and a main () function:
1 |
@Controller |
After starting the main function, can be found in the Control Panel to start a Tomcat container, a Spring MVC-based application also starts up, then visit http: // localhost: 8080 will see the Hello ! World appears in the browser a. Print out the control panel above the spring looked so cool. This configuration is springboot @EnableAutoConfiguration unique role is configured in accordance with application-dependent automatic configuration of the spring, to reduce the amount of development. @Controller @RequestMapping and may be fit to @ResController. Effect is the same. SpringApplication is a helper class, its role is to help start context. So the overall process is the above code SpringApplication run method, and found that this configuration is a Web application (dependent on the class determined in accordance with path), then start the application context in a Spring embedded Tomcat container, and listen to the default tcp port 8080 (default convention). Context while the Spring Spring WebMvc configured in accordance with the default convention:
the Servlet container default Context path /
paths DispatherServlet matched (url-patterns servlet-mapping in) is / *
@ComponentScan path set by default to the same name package SampleController , that is, all @ Controller under this package, after @ Service, @Component, @Repository will be instantiated and added in Spring Context.
Note that, @ ComponentScan default scan all SampleController following bean, so the following example database connection test, SampleController it in the root directory.
2. Database
examples of connections to the database I use mysql, have the opportunity to write an example of a mongoDB. springboot have very good support for relational databases and non-relational database. spring-boot-starter-data- mongodb mongodb is disposed in the maven. Structure of the entire project are:
information DateSource the Configuration document in the resource in a application.properties:
1 |
spring.datasource.url=jdbc:mysql: |
bean is the Visitor, say nothing of this, VisitorDao wrote in a method of insert:
1 |
@Component |
Followed by a service layer, I call run executed inset method to insert data, ok.
1 |
@Component |
然后启动SampleController,就会在控制面板看到“inset in test”的输出。插入数据库成功。
原文链接 大专栏 https://www.dazhuanlan.com/2019/08/16/5d55fc4baed17/