Spring Boot

一个简单的Web应用



    
        luna-learn-spring-demo
        luna.learn.spring.demo
        1.0-SNAPSHOT
    
    4.0.0

    luna-learn-spring-boot

    
        
            org.springframework.boot
            spring-boot-starter-web
            2.1.6.RELEASE
        
    


package com.luna.learn.demo.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //用于创建Rest服务;
@EnableAutoConfiguration  //由Boot提供用于对Spring框架进行自动配置;
public class BbsDaemon {

    @RequestMapping(value = "/")  //用于创建Rest服务;
    public String index() {
        return "hello word~";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(BbsDaemon.class, args);
    }
}

运行BbsDaemon即可完成一个简单的页面;

  1. spring boot提供了一个命令行客户端运行工具spring boot CLI;
    2.spring boot依赖于Maven 3.2+;

代码结构规划

-src-|
------| -main-|
----------------|-java-|
------------------------|-com.smart-|
-----------------------------------------|-dao-|
-----------------------------------------|-domain-|
-----------------------------------------|-service-|
-----------------------------------------|-web-|
-----------------------------------------|-Application.java //应用主类main方法;
----------------|-resources-|
----------------|-webapp-|

  • Application.java应用主类包含main方法放置在主包下面,并在类级别上标注@Configuration、@ComponentScan、@EnableAutoConfiguration注解;
  • spring boot 1.2 + 以上可以使用@SpringBootApplication代替上面3个注解;
package com.luna.learn.demo;

import com.luna.learn.demo.controller.BbsDaemon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.WebApplicationInitializer;

import javax.sql.DataSource;

//@Configuration
//@ComponentScan
//@EnableAutoConfiguration
@SpringBootApplication
@EnableTransactionManagement //启用注解事物管理
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer { //继承Servlet初始化器

    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

持久层

  • spring boot提供spring内置轻量级的Jdbctemplate,提供了启动器spring-boot-starter-jdbc;
  • 也可以使用第三方持久化框架Hibernate或Mybatis,提供了启动器spring-boot-starter-jpa;
  • pom中引用上述依赖后,spring boot能够自动装配数据源的链接,需在resource子目录下添加application.properties配置数据库连接信息;
#配置数据库链接信息
spring.datasource.name=waimai_d_ocr_qa
spring.datasource.url=jdbc:mysql://10.4.243.14:3306/waimai_d_ocr_qa
spring.datasource.username=q3boy
spring.datasource.password=alkf@xpdw
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#server.contextPath=/
server.port=8080

#制定自定义连接池
#spring.datasource.type=org.apache.commons.dbcp.BasicDataSource

#连接池配置信息
spring.datasource.max-wait=10000
spring.datasource.max-active=50
spring.datasource.max-idle=10
spring.datasource.min-idle=8
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=select 1

#JNDI设置数据链接
#spring.datasource.jndi-name=java:comp/env/jdbc/waimai_d_ocr_qa

#初始化数据库脚本
#spring.datasource.platform=mysql
#设置DML脚本文件名;
#spring.datasource.data=data-mysql.sql
#设置DDL脚本文件名
#spring.datasource.schema=schema-mysql.sql

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

史上最全最详细JNDI数据源配置说明

业务层

  • 编写业务代码时正确的业务逻辑;
  • 对业务事物的管理

在主类Application上标注@EnableTransactionManagement 注解,开启事务支持,Boot为应用自动装配事务支持;然后再访问Service方法上注解@Transactional注解即可;如果@Transactional放在类级别上,那么当前Service类所有方法都将被事物增强,建议不这么使用;

自定义事务管理器

  • 在主类Application中添加自定义事务管理方法txManager(),并在方法上标注@Bean注解,此时SpringBoot会加载自定义的事务管理器,不会重新实例化其他事务管理器。

分布式事务管理器

  • 分布式事务支持Atomikos和Bitronix分布式事务处理框架,可以根据需要引入相应的启动器spring-boot-starter-jta-atomikos或spring-boot-starter-jta-bitronix;

展现层

配置pom.xml依赖




    4.0.0

    luna-learn-spring-demo
    luna.learn.spring.demo
    0.0.1-SNAPSHOT
    war

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
            org.projectlombok
            lombok
            1.18.8
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        
        
            javax.servlet
            jstl
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    false
                
            
        
    

    
    
        
            
                org.springframework.boot
                spring-boot-starter-parent
                2.1.6.RELEASE
                pom
                import
            
        
    

你可能感兴趣的:(Spring Boot)