日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot:企業(yè)常用的Starter以及實現(xiàn)

目錄

十年的懷寧網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整懷寧建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“懷寧網(wǎng)站設(shè)計”,“懷寧網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

  •  SpringBoot簡介
  •  SpringBoot運行
  •  SpringBoot目錄結(jié)構(gòu)
  •  整合JdbcTemplate
  •  @RestController
  •  整合JSP
  •  整合JPA
  •  整合MyBatis
  •  AOP功能使用
  •  任務(wù)調(diào)度
  •  整合RabbitMq
  •  整合郵件發(fā)送

SpringBoot簡介

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

Spring Boot讓我們的Spring應(yīng)用變的更輕量化。比如:你可以僅僅依靠一個Java類來運行一個Spring引用。你也可以打包你的應(yīng)用為jar并通過使用java -jar來運行你的Spring Web應(yīng)用。

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

1、為所有Spring開發(fā)者更快的入門

2、開箱即用,提供各種默認配置來簡化項目配置

3、內(nèi)嵌式容器簡化Web項目

4、沒有冗余代碼生成和XML配置的要求

在下面的代碼中只要有一定基礎(chǔ)會發(fā)現(xiàn)這寫代碼實例非常簡單對于開發(fā)者來說幾乎是“零配置”。

SpringBoot運行

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

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

首先我們需要依賴SpringBoot父工程,這是每個項目中必須要有的。

 
 
 
  1.  
  2.  
  3.   org.springframework.boot 
  4.   spring-boot-starter-parent 
  5.   2.0.5.RELEASE 
  6.     
  7.  
  8.  
  9.  
  10.   UTF-8 
  11.   UTF-8 
  12.   1.8
  13.  

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

 
 
 
  1.  
  2.    
  3.    
  4.    org.springframework.boot 
  5.    spring-boot-starter-web 
  6.    
  7.  

我們需要編寫一個SpringBoot啟動類,SpringbootFirstExperienceApplication.java

 
 
 
  1. @SpringBootApplication 
  2. public class SpringbootFirstExperienceApplication { 
  3.  public static void main(String[] args) { 
  4.   SpringApplication.run(SpringbootFirstExperienceApplication.class, args); 
  5.  } 
  6. }

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

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

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

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

SpringBoot目錄結(jié)構(gòu)# 在src/main/resources中我們可以有幾個文件夾:

templates:用來存儲模板引擎的,Thymeleaf,F(xiàn)reeMarker,Velocity等都是不錯的選擇。

static:存儲一些靜態(tài)資源,css,js等

public:在默認SpringBoot工程中是不生成這個文件夾的,但是在自動配置中我們可以有這個文件夾用來存放公共的資源(html等)

application.properties:這個文件名字是固定的,SpringBoot啟動會默認加載這些配置在這里面可以配置端口號,訪問路徑,數(shù)據(jù)庫連接信息等等。這個文件非常重要,當(dāng)然官方中推出了一個yml格式這是非常強大的數(shù)據(jù)格式。

整合JdbcTemplate

引入依賴:

 
 
 
  1.  
  2.   org.springframework.boot 
  3.   spring-boot-starter-parent 
  4.   1.5.2.RELEASE 
  5.   
  6.   
  7.          
  8.    
  9.    org.springframework.boot 
  10.    spring-boot-starter-web 
  11.    
  12.           
  13.    
  14.    org.springframework.boot 
  15.    spring-boot-starter-jdbc 
  16.    
  17.           
  18.    
  19.    mysql 
  20.    mysql-connector-java 
  21.    
  22.    
  23.    org.springframework.boot 
  24.    spring-boot-starter-test 
  25.    test 
  26.    
  27.  

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

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

使用方式:

 
 
 
  1. @Service 
  2. public class EmployeeService { 
  3.  @Autowired 
  4.  private JdbcTemplate jdbcTemplate; 
  5.  public boolean saveEmp(String name,String email,String gender){ 
  6.   String sql = "insert into tal_employee values(null,?,?,?)"; 
  7.   int result = jdbcTemplate.update(sql, name,email,gender); 
  8.   System.out.println("result : " + result); 
  9.   return result > 0 ? true:false; 
  10.  } 
 
 
 
  1. @RestController 
  2. public class EmployeeController { 
  3.  @Autowired 
  4.  private EmployeeService employeeService; 
  5.  @RequestMapping("/save") 
  6.  public String insert(String name,String email,String gender){ 
  7.   boolean result = employeeService.saveEmp(name, email, gender); 
  8.   if(result){
  9.     return "success"; 
  10.   } 
  11.   return "error"; 
  12.  } 
  13. }

這里我們直接返回一個文本格式。

@RestController

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

整合JSP

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

引入依賴:

 
 
 
  1.  
  2.     org.springframework.boot 
  3.     spring-boot-starter-parent 
  4.     1.5.2.RELEASE 
  5.  
  6.  
  7.      
  8.      
  9.         org.springframework.boot 
  10.         spring-boot-starter-web 
  11.      
  12.      
  13.      
  14.         org.springframework.boot 
  15.         spring-boot-starter-tomcat 
  16.      
  17.      
  18.         org.apache.tomcat.embed 
  19.         tomcat-embed-jasper 
  20.      

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

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

整合JPA

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

添加依賴:

 
 
 
  1.  
  2.   org.springframework.boot 
  3.   spring-boot-starter-parent 
  4.   1.5.2.RELEASE 
  5.   
  6.   
  7.    
  8.    org.springframework.boot 
  9.    spring-boot-starter-web 
  10.    
  11.          
  12.    
  13.    org.springframework.boot 
  14.    spring-boot-starter-data-jpa 
  15.    
  16.    
  17.    org.springframework.boot 
  18.    spring-boot-starter-test 
  19.    test 
  20.    
  21.    
  22.    mysql 
  23.    mysql-connector-java 
  24.    
  25.  

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

Application.properties

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

實體類:Employee.java

 
 
 
  1. @Table(name="tal_employee") 
  2. @Entity 
  3. public class Employee implements Serializable{ 
  4.  @Id
  5.  @GeneratedValue(strategy = GenerationType.AUTO) 
  6.  private Integer id; 
  7.  @Column(name="last_Name") 
  8.  private String lastName; 
  9.  private String email; 
  10.  private String gender;
  11.  //get set 省略 
  12. }

EmployeeDao接口:

 
 
 
  1. public interface EmployeeDao extends JpaRepository
  2. }

EmployeeController.java:

 
 
 
  1. @Controller 
  2. public class EmployeeController { 
  3.  @Autowired 
  4.  private EmployeeDao employeeDao; 
  5.  @ResponseBody
  6.  @RequestMapping("/emps") 
  7.  public List getEmployees(){ 
  8.   List employees = employeeDao.findAll(); 
  9.   System.out.println(employees); 
  10.   return employees; 
  11.  } 
  12. }

整合MyBatis

引入依賴:

 
 
 
  1.  
  2.   org.springframework.boot 
  3.   spring-boot-starter-parent 
  4.   1.5.2.RELEASE 
  5.   
  6.   
  7.   
  8.     org.springframework.boot 
  9.    spring-boot-starter-web 
  10.    
  11.          
  12.    
  13.    org.springframework.boot
  14.    spring-boot-starter-jdbc 
  15.    
  16.          
  17.     
  18.    org.springframework.boot 
  19.    spring-boot-starter-logging 
  20.    
  21.   
  22.     
  23.    org.mybatis.spring.boot 
  24.    mybatis-spring-boot-starter 
  25.    1.2.2 
  26.    
  27.    
  28.    org.springframework.boot 
  29.    spring-boot-starter-test 
  30.    test 
  31.    
  32.    
  33.    mysql 
  34.    mysql-connector-java 
  35.   
  36.   

配置application.properties

 
 
 
  1. spring.datasource.url=jdbc:mysql://localhost:3306/mybatis 
  2. spring.datasource.username=root 
  3. spring.datasource.password=root 
  4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
  5. ##############datasource classpath 數(shù)據(jù)連接池地址##############
  6. #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 
  7. #指定我們的mapper.xml位置
  8.  mybatis.mapper-locations=classpath:com/simple/springboot/mybatis/dao/mapper/*.xml 
  9. #entity.class 指定我們實體類所在包位置 
  10. mybatis.type-aliases-package=com.simple.springboot.mybatis.entity

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

注意事項:在我們的Dao層接口中一定要在類上加上注解@Mapper否則無法掃描到。

AOP功能使用

在我們SpringBoot中使用AOP非常簡單。

 
 
 
  1. package com.simple.springboot.aop; 
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.After; 
  4. import org.aspectj.lang.annotation.AfterThrowing; 
  5. import org.aspectj.lang.annotation.Around; 
  6. import org.aspectj.lang.annotation.Aspect; 
  7. import org.aspectj.lang.annotation.Before; 
  8. import org.aspectj.lang.annotation.Pointcut; 
  9. import org.springframework.stereotype.Component; 
  10. @Aspect 
  11. @Component 
  12. public class SpringBootAspect { 
  13.  /** 
  14.   * 定義一個切入點 
  15.   * @author:SimpleWu 
  16.   * @Date:2018年10月12日 
  17.   */ 
  18.  @Pointcut(value="execution(* com.simple.springboot.util.*.*(..))") 
  19.  public void aop(){} 
  20.  /** 
  21.   * 定義一個前置通知 
  22.   * @author:SimpleWu 
  23.   * @Date:2018年10月12日 
  24.   */ 
  25.  @Before("aop()") 
  26.  public void aopBefore(){ 
  27.   System.out.println("前置通知 SpringBootAspect....aopBefore"); 
  28.  } 
  29.  /** 
  30.   * 定義一個后置通知 
  31.   * @author:SimpleWu 
  32.   * @Date:2018年10月12日 
  33.   */ 
  34.  @After("aop()") 
  35.  public void aopAfter(){ 
  36.   System.out.println("后置通知  SpringBootAspect....aopAfter"); 
  37.  } 
  38.  /** 
  39.   * 處理未處理的JAVA異常 
  40.   * @author:SimpleWu 
  41.   * @Date:2018年10月12日 
  42.   */ 
  43.  @AfterThrowing(pointcut="aop()",throwing="e") 
  44.  public void exception(Exception e){
  45.    System.out.println("異常通知 SpringBootAspect...exception .." + e); 
  46.  } 
  47.  /** 
  48.   * 環(huán)繞通知 
  49.   * @author:SimpleWu 
  50.   * @throws Throwable  
  51.   * @Date:2018年10月12日 
  52.   */ 
  53.  @Around("aop()") 
  54.  public void around(ProceedingJoinPoint invocation) throws Throwable{ 
  55.   System.out.println("SpringBootAspect..環(huán)繞通知 Before"); 
  56.   invocation.proceed(); 
  57.   System.out.println("SpringBootAspect..環(huán)繞通知 After"); 
  58.  }
  59.  }

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

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

 
 
 
  1. @Component 
  2. public class ScheduledTasks { 
  3.  private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); 
  4.   /** 
  5.   * 任務(wù)調(diào)度,每隔5秒執(zhí)行一次 
  6.   * @author:SimpleWu 
  7.   * @Date:2018年10月12日 
  8.   */ 
  9.  @Scheduled(fixedRate = 1000)
  10.      public void reportCurrentTime() { 
  11.         System.out.println("現(xiàn)在時間:" + dateFormat.format(new Date())); 
  12.     } 
  13. }

然后啟動的時候我們必須要在主函數(shù)類上加上注解:@EnableScheduling(翻譯過來就是開啟調(diào)度)

 
 
 
  1. /** 
  2.  * SpringBoot使用任務(wù)調(diào)度 
  3.  * @EnableScheduling標(biāo)注程序開啟任務(wù)調(diào)度 
  4.  * @author :SimpleWu 
  5.  * @Date:2018年10月12日 
  6.  */ 
  7. @SpringBootApplication 
  8. @EnableScheduling 
  9. public class App { 
  10.  public static void main(String[] args) { 
  11.   SpringApplication.run(App.class, args); 
  12.  } 
  13. }

整合RabbitMq

安裝RabbitMq 由于RabbitMQ依賴Erlang, 所以需要先安裝Erlang。

 
 
 
  1. sudo yum install -y make gcc gcc-c++ m4 openssl openssl-devel ncurses-devel unixODBC unixODBC-devel java java-devel 
  2. sudo yum install epel-release 
  3. sudo yum install erlang 
  4. sudo yum install socat

下載RabbitMQ,并且安裝

 
 
 
  1. sudo wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el7.noarch.rpm 
  2. sudo yum install rabbitmq-server-3.6.15-1.el7.noarch.rpm

進入cd /etc/rabbitmq/ 創(chuàng)建vim rabbitmq.config

 
 
 
  1. [{rabbit, [{loopback_users, []}]}].

這里的意思是開放使用,rabbitmq默認創(chuàng)建的用戶guest,密碼也是guest,這個用戶默認只能是本機訪問,localhost或者127.0.0.1,從外部訪問需要添加上面的配置。如果沒有找到清除日志

 
 
 
  1. rm rabbit\@rabbit@localhost-sasl.log

最好還是直接sudo rabbitmqctl set_user_tags root administrator ,給root用戶賦值管理員權(quán)限 RabbitMQ,基本操作

 
 
 
  1. # 添加開機啟動RabbitMQ服務(wù) 
  2. systemctl enable rabbitmq-server.service 
  3. # 查看服務(wù)狀態(tài) 
  4. systemctl status  rabbitmq-server.service 
  5. # 啟動服務(wù) 
  6. systemctl start rabbitmq-server.service 
  7. # 停止服務(wù) 
  8. systemctl stop rabbitmq-server.service 
  9. # 查看當(dāng)前所有用戶 
  10. rabbitmqctl list_users 
  11. # 查看默認guest用戶的權(quán)限 
  12. rabbitmqctl list_user_permissions guest 
  13. # 由于RabbitMQ默認的賬號用戶名和密碼都是guest。為了安全起見, 先刪掉默認用戶 
  14. rabbitmqctl delete_user guest 
  15. # 添加新用戶 
  16. rabbitmqctl add_user username password 
  17. # 設(shè)置用戶tag 
  18. rabbitmqctl set_user_tags username administrator
  19.  # 賦予用戶默認vhost的全部操作權(quán)限 
  20. rabbitmqctl set_permissions -p / username ".*" ".*" ".*" 
  21. # 查看用戶的權(quán)限 
  22. rabbitmqctl list_user_permissions username

如果只從命令行操作RabbitMQ,多少有點不方便。幸好RabbitMQ自帶了web管理界面,只需要啟動插件便可以使用。

 
 
 
  1. rabbitmq-plugins enable rabbitmq_management

訪問: http://服務(wù)器IP:15672

整合RabbitMq

導(dǎo)入Maven依賴

 
 
 
  1.  
  2.  
  3.         org.springframework.boot 
  4.         spring-boot-starter-parent 
  5.         2.1.4.RELEASE 
  6.           
  7.  
  8.  
  9.  
  10.         1.8 
  11.  
  12.  
  13.  
  14.    
  15.          
  16.             org.springframework.boot 
  17.             spring-boot-starter-amqp 
  18.          
  19.    
  20.          
  21.             org.springframework.boot 
  22.             spring-boot-starter-web 
  23.          
  24.    
  25.          
  26.             org.springframework.boot 
  27.             spring-boot-starter-test 
  28.             test 
  29.          

設(shè)置application.properties配置文件

 
 
 
  1. spring.application.name=springboot-rabbitmq 
  2. #RabbitMq所在服務(wù)器IP 
  3. spring.rabbitmq.host=192.168.197.133 
  4. #連接端口號 
  5. spring.rabbitmq.port=5672
  6. #用戶名 
  7. spring.rabbitmq.username=root 
  8. #用戶密碼 
  9. spring.rabbitmq.password=123456 
  10. # 開啟發(fā)送確認 
  11. spring.rabbitmq.publisher-confirms=true 
  12. # 開啟發(fā)送失敗退回 
  13. spring.rabbitmq.publisher-returns=true 
  14. spring.rabbitmq.virtual-host=/

創(chuàng)建RabbitMq隊列初始化類,初始化隊列

 
 
 
  1. /** 
  2.  * @author SimpleWu 
  3.  * @Date 2019-05-17 
  4.  * 該類初始化隊列 
  5.  */ 
  6. @Configuration 
  7. public class RabbitMqInitialization { 
  8.     /** 
  9.      * 創(chuàng)建隊列 隊列名字為SayQueue 
  10.      * @return 
  11.      */ 
  12.     @Bean 
  13.     public Queue SayQueue() { 
  14.         return new Queue("SayQueue"); 
  15.     } 
  16. }

創(chuàng)建生產(chǎn)者

 
 
 
  1. /** 
  2.  * @author SimpleWu 
  3.  * @Date 2019-05-17 
  4.  * 生產(chǎn)者 
  5.  */ 
  6. @Component 
  7. public class SayProducer { 
  8.     @Autowired 
  9.     private RabbitTemplate rabbitTemplate;
  10.     public void send(String name){ 
  11.         String sendMsg = "hello:    " + name + "   " + new Date(); 
  12.   //指定隊列 
  13.         this.rabbitTemplate.convertAndSend("SayQueue", sendMsg); 
  14.     } 
  15. }

創(chuàng)建消費者 @RabbitListener:當(dāng)監(jiān)聽到隊列 SayQueue 中有消息時則會進行接收并處理 @RabbitHandler :標(biāo)注在類上面表示當(dāng)有收到消息的時候,就交給 @RabbitHandler 的方法處理,具體使用哪個方法處理,根據(jù) MessageConverter 轉(zhuǎn)換后的參數(shù)類型

 
 
 
  1. /** 
  2.  * @author SimpleWu 
  3.  * @Date 2019-05-17 
  4.  * 消費者 
  5.  * queues 指定監(jiān)聽的隊列 
  6.  */ 
  7. @Component 
  8. @RabbitListener(queues = "SayQueue") 
  9. public class SayConsumer { 
  10.     @RabbitHandler 
  11.     public void process(String hello) { 
  12.         System.out.println("SayConsumer  : " + hello); 
  13.     } 
  14. }

創(chuàng)建接口進行測試

 
 
 
  1. @RestController 
  2. public class SayController {
  3.     @Autowired 
  4.     private SayProducer sayProducer; 
  5.     @RequestMapping("/send/{name}") 
  6.     public String send(@PathVariable String name){ 
  7.         sayProducer.send(name); 
  8.         return "Send Succcess SimpleWu"; 
  9.     } 
  10. }

啟動類就用IDEA默認生成的就好了。http://10.192.132.22:8080/send/First 發(fā)送請求 消費者接受消息:SayConsumer : hello: First Tue May 07 17:57:02 CST 2019 注:在傳輸對象時一定要序列化

整合郵件發(fā)送

導(dǎo)入依賴

 
 
 
  1.  
  2.  
  3.  org.springframework.boot 
  4.  spring-boot-starter-mail 

配置Propert
文章題目:SpringBoot:企業(yè)常用的Starter以及實現(xiàn)
文章地址:http://www.5511xx.com/article/ccoicse.html