<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Boot:企業(yè)常用的 Starter

          共 9455字,需瀏覽 19分鐘

           ·

          2020-09-19 09:25

          點(diǎn)擊上方藍(lán)色“小哈學(xué)Java”,選擇“設(shè)為星標(biāo)

          回復(fù)“資源”獲取獨(dú)家整理的學(xué)習(xí)資料!

          來(lái)源:Spring Boot:企業(yè)常用的 Starter

          • SpringBoot簡(jiǎn)介
          • SpringBoot運(yùn)行
          • SpringBoot目錄結(jié)構(gòu)
          • 整合JdbcTemplate
          • @RestController
          • 整合JSP
          • 整合JPA
          • 整合MyBatis
          • AOP功能使用
          • 任務(wù)調(diào)度
          • 倉(cāng)庫(kù)地址

          SpringBoot簡(jiǎn)介

          Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開(kāi)發(fā)人員不再需要定義樣板化的配置。通過(guò)這種方式,Boot致力于在蓬勃發(fā)展的快速應(yīng)用開(kāi)發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。

          Spring Boot讓我們的Spring應(yīng)用變的更輕量化。比如:你可以?xún)H僅依靠一個(gè)Java類(lèi)來(lái)運(yùn)行一個(gè)Spring引用。你也可以打包你的應(yīng)用為jar并通過(guò)使用java -jar來(lái)運(yùn)行你的Spring Web應(yīng)用。

          Spring Boot的主要優(yōu)點(diǎn):

          • 為所有Spring開(kāi)發(fā)者更快的入門(mén)
          • 開(kāi)箱即用,提供各種默認(rèn)配置來(lái)簡(jiǎn)化項(xiàng)目配置
          • 內(nèi)嵌式容器簡(jiǎn)化Web項(xiàng)目
          • 沒(méi)有冗余代碼生成和XML配置的要求

          在下面的代碼中只要有一定基礎(chǔ)會(huì)發(fā)現(xiàn)這寫(xiě)代碼實(shí)例非常簡(jiǎn)單對(duì)于開(kāi)發(fā)者來(lái)說(shuō)幾乎是“零配置”。

          SpringBoot運(yùn)行

          開(kāi)發(fā)工具:jdk8,IDEA,STS,eclipse(需要安裝STS插件)這些都支持快速啟動(dòng)SpringBoot工程。我這里就不快速啟動(dòng)了,使用maven工程。學(xué)習(xí)任何一項(xiàng)技術(shù)首先就要精通HelloWord,那我們來(lái)跑個(gè)初體驗(yàn)。

          首先只用maven我們創(chuàng)建的maven工程直接以jar包的形式創(chuàng)建就行了,首先我們來(lái)引入SpringBoot的依賴(lài)

          首先我們需要依賴(lài)SpringBoot父工程,這是每個(gè)項(xiàng)目中必須要有的。


          <parent>
          ????????<groupId>org.springframework.bootgroupId>
          ????????<artifactId>spring-boot-starter-parentartifactId>
          ????????<version>2.0.5.RELEASEversion>
          ????????<relativePath/>
          parent>

          <properties>
          ????????<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
          ????????<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
          ????????<java.version>1.8java.version>
          properties>

          我們啟動(dòng)WEB模塊當(dāng)然必須要引入WEB模塊的依賴(lài)

          <dependencies>
          ????????
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-webartifactId>
          ????????dependency>
          ????dependencies>

          我們需要編寫(xiě)一個(gè)SpringBoot啟動(dòng)類(lèi),SpringbootFirstExperienceApplication.java

          @SpringBootApplication
          public?class?SpringbootFirstExperienceApplication?{

          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication.run(SpringbootFirstExperienceApplication.class,?args);
          ????}
          }

          到了這里我們直接把他當(dāng)成SpringMVC來(lái)使用就行了,不過(guò)這里默認(rèn)是不支持JSP官方推薦使用模板引擎,后面會(huì)寫(xiě)到整合JSP。這里我就不寫(xiě)Controller了。

          @SpringBootApplication:之前用戶(hù)使用的是3個(gè)注解注解他們的main類(lèi)。分別是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于這些注解一般都是一起使用,spring boot提供了一個(gè)統(tǒng)一的注解@SpringBootApplication。

          注意事項(xiàng):我們使用這個(gè)注解在不指定掃描路徑的情況下,SpringBoot只能掃描到和SpringbootFirstExperienceApplication同包或子包的Bean;

          SpringBoot目錄結(jié)構(gòu)

          在src/main/resources中我們可以有幾個(gè)文件夾:

          • **templates:**用來(lái)存儲(chǔ)模板引擎的,Thymeleaf,F(xiàn)reeMarker,Velocity等都是不錯(cuò)的選擇。
          • **static:**存儲(chǔ)一些靜態(tài)資源,css,js等
          • **public:**在默認(rèn)SpringBoot工程中是不生成這個(gè)文件夾的,但是在自動(dòng)配置中我們可以有這個(gè)文件夾用來(lái)存放公共的資源(html等)
          • **application.properties:**這個(gè)文件名字是固定的,SpringBoot啟動(dòng)會(huì)默認(rèn)加載這些配置在這里面可以配置端口號(hào),訪問(wèn)路徑,數(shù)據(jù)庫(kù)連接信息等等。這個(gè)文件非常重要,當(dāng)然官方中推出了一個(gè)yml格式這是非常強(qiáng)大的數(shù)據(jù)格式。

          整合JdbcTemplate

          引入依賴(lài):

          ?<parent>
          ????????<groupId>org.springframework.bootgroupId>
          ????????<artifactId>spring-boot-starter-parentartifactId>
          ????????<version>1.5.2.RELEASEversion>
          ????parent>
          ????<dependencies>
          ????????
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-webartifactId>
          ????????dependency>
          ?????????
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-jdbcartifactId>
          ????????dependency>
          ?????????
          ????????<dependency>
          ????????????<groupId>mysqlgroupId>
          ????????????<artifactId>mysql-connector-javaartifactId>
          ????????dependency>
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-testartifactId>
          ????????????<scope>testscope>
          ????????dependency>
          ????dependencies>

          配置application.properties,雖然說(shuō)是“零配置”但是這些必要的肯定要指定,否則它怎么知道連那個(gè)數(shù)據(jù)庫(kù)?

          spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
          spring.datasource.username=root
          spring.datasource.password=root
          spring.datasource.driver-class-name=com.mysql.jdbc.Driver

          使用方式:

          @Service
          public?class?EmployeeService?{
          ????@Autowired
          ????private?JdbcTemplate?jdbcTemplate;

          ????public?boolean?saveEmp(String?name,String?email,String?gender){
          ????????String?sql?=?"insert?into?tal_employee?values(null,?,?,?)";
          ????????int?result?=?jdbcTemplate.update(sql,?name,email,gender);
          ????????System.out.println("result?:?"?+?result);
          ????????return?result?>?0???true:false;
          ????}
          }
          @RestController
          public?class?EmployeeController?{

          ????@Autowired
          ????private?EmployeeService?employeeService;

          ????@RequestMapping("/save")
          ????public?String?insert(String?name,String?email,String?gender){
          ????????boolean?result?=?employeeService.saveEmp(name,?email,?gender);
          ????????if(result){
          ????????????return?"success";
          ????????}
          ????????return?"error";
          ????}
          }

          這里我們直接返回一個(gè)文本格式。

          @RestController

          在上面的代碼中我們使用到這個(gè)注解修改我們的Controller類(lèi)而是不使用@Controller這個(gè)注解,其實(shí)中包含了@Controller,同時(shí)包含@ResponseBody既然修飾在類(lèi)上面那么就是表示這個(gè)類(lèi)中所有的方法都是@ResponseBody所以在這里我們返回字符串在前臺(tái)我們會(huì)以文本格式展示,如果是對(duì)象那么它會(huì)自動(dòng)轉(zhuǎn)換成json格式返回。

          整合JSP

          在創(chuàng)建整合JSP的時(shí)候指定要選WAR,一定要選WAR。

          引入依賴(lài):

          <parent>
          ????<groupId>org.springframework.bootgroupId>
          ????<artifactId>spring-boot-starter-parentartifactId>
          ????<version>1.5.2.RELEASEversion>
          parent>
          <dependencies>
          ????
          ????<dependency>
          ????????<groupId>org.springframework.bootgroupId>
          ????????<artifactId>spring-boot-starter-webartifactId>
          ????dependency>
          ????
          ????<dependency>
          ????????<groupId>org.springframework.bootgroupId>
          ????????<artifactId>spring-boot-starter-tomcatartifactId>
          ????dependency>
          ????<dependency>
          ????????<groupId>org.apache.tomcat.embedgroupId>
          ????????<artifactId>tomcat-embed-jasperartifactId>
          ????dependency>
          dependencies>

          然后我們只需要配置試圖解析器路徑就可以了。

          #配置試圖解析器前綴
          spring.mvc.view.prefix=/WEB-INF/views/
          #配置試圖解析器后綴
          spring.mvc.view.suffix=.jsp

          整合JPA

          同樣的整合JPA我們只需要啟動(dòng)我們SpringBoot已經(jīng)集成好的模塊即可。

          添加依賴(lài):

          <parent>
          ????????<groupId>org.springframework.bootgroupId>
          ????????<artifactId>spring-boot-starter-parentartifactId>
          ????????<version>1.5.2.RELEASEversion>
          ????parent>
          ????<dependencies>
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-webartifactId>
          ????????dependency>
          ????????
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-data-jpaartifactId>
          ????????dependency>
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-testartifactId>
          ????????????<scope>testscope>
          ????????dependency>
          ????????<dependency>
          ????????????<groupId>mysqlgroupId>
          ????????????<artifactId>mysql-connector-javaartifactId>
          ????????dependency>
          ????dependencies>

          啟動(dòng)JPA組件后直接配置數(shù)據(jù)庫(kù)連接信息就可以使用JPA功能。

          Application.properties

          spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
          spring.datasource.username=root
          spring.datasource.password=root
          spring.datasource.driver-class-name=com.mysql.jdbc.Driver

          實(shí)體類(lèi):Employee.java

          @Table(name="tal_employee")
          @Entity
          public?class?Employee?implements?Serializable{
          ????@Id
          ????@GeneratedValue(strategy?=?GenerationType.AUTO)
          ????private?Integer?id;
          ????@Column(name="last_Name")
          ????private?String?lastName;
          ????private?String?email;
          ????private?String?gender;
          ????//get?set?省略
          }

          EmployeeDao接口:

          public?interface?EmployeeDao?extends?JpaRepository<Employee,?Integer>{
          }

          EmployeeController.java:

          @Controller
          public?class?EmployeeController?{
          ????@Autowired
          ????private?EmployeeDao?employeeDao;

          ????@ResponseBody
          ????@RequestMapping("/emps")
          ????public?List?getEmployees(){
          ????????List?employees?=?employeeDao.findAll();
          ????????System.out.println(employees);
          ????????return?employees;
          ????}
          }

          整合MyBatis

          引入依賴(lài):

          <parent>
          ????????<groupId>org.springframework.bootgroupId>
          ????????<artifactId>spring-boot-starter-parentartifactId>
          ????????<version>1.5.2.RELEASEversion>
          ????parent>
          ????<dependencies>
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-webartifactId>
          ????????dependency>
          ????????
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-jdbcartifactId>
          ????????dependency>
          ?????????
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-loggingartifactId>
          ????????dependency>
          ????????
          ????????<dependency>
          ????????????<groupId>org.mybatis.spring.bootgroupId>
          ????????????<artifactId>mybatis-spring-boot-starterartifactId>
          ????????????<version>1.2.2version>
          ????????dependency>
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-testartifactId>
          ????????????<scope>testscope>
          ????????dependency>
          ????????<dependency>
          ????????????<groupId>mysqlgroupId>
          ????????????<artifactId>mysql-connector-javaartifactId>
          ????????dependency>
          ????dependencies>

          配置application.properties

          spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
          spring.datasource.username=root
          spring.datasource.password=root
          spring.datasource.driver-class-name=com.mysql.jdbc.Driver
          ##############datasource classpath 數(shù)據(jù)連接池地址##############
          #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

          #指定我們的mapper.xml位置
          mybatis.mapper-locations=classpath:com/simple/springboot/mybatis/dao/mapper/*.xml
          #entity.class 指定我們實(shí)體類(lèi)所在包位置
          mybatis.type-aliases-package=com.simple.springboot.mybatis.entity

          當(dāng)然這里還有很多屬性如果想要使用可以參考官方文檔。到了這里其他就不寫(xiě)了,把他當(dāng)作SSM使用就ok。

          注意事項(xiàng):在我們的Dao層接口中一定要在類(lèi)上加上注解@Mapper否則無(wú)法掃描到。

          AOP功能使用

          在我們SpringBoot中使用AOP非常簡(jiǎn)單。

          package?com.simple.springboot.aop;

          import?org.aspectj.lang.ProceedingJoinPoint;
          import?org.aspectj.lang.annotation.After;
          import?org.aspectj.lang.annotation.AfterThrowing;
          import?org.aspectj.lang.annotation.Around;
          import?org.aspectj.lang.annotation.Aspect;
          import?org.aspectj.lang.annotation.Before;
          import?org.aspectj.lang.annotation.Pointcut;
          import?org.springframework.stereotype.Component;

          @Aspect
          @Component
          public?class?SpringBootAspect?{

          ????/**
          ?????*?定義一個(gè)切入點(diǎn)
          ?????*?@author:SimpleWu
          ?????*?@Date:2018年10月12日
          ?????*/

          ????@Pointcut(value="execution(*?com.simple.springboot.util.*.*(..))")
          ????public?void?aop(){}

          ????/**
          ?????*?定義一個(gè)前置通知
          ?????*?@author:SimpleWu
          ?????*?@Date:2018年10月12日
          ?????*/

          ????@Before("aop()")
          ????public?void?aopBefore(){
          ????????System.out.println("前置通知?SpringBootAspect....aopBefore");
          ????}

          ????/**
          ?????*?定義一個(gè)后置通知
          ?????*?@author:SimpleWu
          ?????*?@Date:2018年10月12日
          ?????*/

          ????@After("aop()")
          ????public?void?aopAfter(){
          ????????System.out.println("后置通知??SpringBootAspect....aopAfter");
          ????}

          ????/**
          ?????*?處理未處理的JAVA異常
          ?????*?@author:SimpleWu
          ?????*?@Date:2018年10月12日
          ?????*/

          ????@AfterThrowing(pointcut="aop()",throwing="e")
          ????public?void?exception(Exception?e){
          ????????System.out.println("異常通知?SpringBootAspect...exception?.."?+?e);
          ????}

          ????/**
          ?????*?環(huán)繞通知
          ?????*?@author:SimpleWu
          ?????*?@throws?Throwable
          ?????*?@Date:2018年10月12日
          ?????*/

          ????@Around("aop()")
          ????public?void?around(ProceedingJoinPoint?invocation)?throws?Throwable{
          ????????System.out.println("SpringBootAspect..環(huán)繞通知?Before");
          ????????invocation.proceed();
          ????????System.out.println("SpringBootAspect..環(huán)繞通知?After");
          ????}
          }

          任務(wù)調(diào)度

          SpringBoot已經(jīng)集成好一個(gè)調(diào)度功能。

          @Component
          public?class?ScheduledTasks?{
          ????private?static?final?SimpleDateFormat?dateFormat?=?new?SimpleDateFormat("HH:mm:ss");

          ????/**
          ?????*?任務(wù)調(diào)度,每隔5秒執(zhí)行一次
          ?????*?@author:SimpleWu
          ?????*?@Date:2018年10月12日
          ?????*/


          ????@Scheduled(fixedRate?=?1000)
          ????public?void?reportCurrentTime()?{
          ????????System.out.println("現(xiàn)在時(shí)間:"?+?dateFormat.format(new?Date()));
          ????}
          }

          然后啟動(dòng)的時(shí)候我們必須要在主函數(shù)類(lèi)上加上注解:@EnableScheduling(翻譯過(guò)來(lái)就是開(kāi)啟調(diào)度)

          /**
          ?*?SpringBoot使用任務(wù)調(diào)度
          ?*?@EnableScheduling標(biāo)注程序開(kāi)啟任務(wù)調(diào)度
          ?*?@author?:SimpleWu
          ?*?@Date:2018年10月12日
          ?*/


          @SpringBootApplication
          @EnableScheduling
          public?class?App?{
          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication.run(App.class,?args);
          ????}
          }

          倉(cāng)庫(kù)地址

          本文所有測(cè)試代碼實(shí)例:

          https://gitlab.com/450255266/code

          END


          有熱門(mén)推薦?

          1.?因?yàn)锽itMap,白白搭進(jìn)去8臺(tái)服務(wù)器...

          2.?最新!薪酬最高的大學(xué)專(zhuān)業(yè)公布!

          3.?:: 是什么語(yǔ)法?

          4.?為什么HTTPS是安全的

          最近面試BAT,整理一份面試資料Java面試BATJ通關(guān)手冊(cè),覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫(kù)、數(shù)據(jù)結(jié)構(gòu)等等。

          獲取方式:點(diǎn)“在看”,關(guān)注公眾號(hào)并回復(fù)?Java?領(lǐng)取,更多內(nèi)容陸續(xù)奉上。

          文章有幫助的話(huà),在看,轉(zhuǎn)發(fā)吧。

          謝謝支持喲 (*^__^*)

          瀏覽 63
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  2025年亚洲国产精品视频 | 免费操骚逼视频 | 波多野吉衣一二三区乱码 | 五月丁香网色 | 一区二区黄色电影 |