新聞中心
目錄

十年的懷寧網(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)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
- SpringBoot簡介
- SpringBoot運(yùn)行
- SpringBoot目錄結(jié)構(gòu)
- 整合JdbcTemplate
- @RestController
- 整合JSP
- 整合JPA
- 整合MyBatis
- AOP功能使用
- 任務(wù)調(diào)度
- 整合RabbitMq
- 整合郵件發(fā)送
SpringBoot簡介
Spring Boot是由Pivotal團(tuán)隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Boot致力于在蓬勃發(fā)展的快速應(yīng)用開發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。
Spring Boot讓我們的Spring應(yīng)用變的更輕量化。比如:你可以僅僅依靠一個Java類來運(yùn)行一個Spring引用。你也可以打包你的應(yīng)用為jar并通過使用java -jar來運(yùn)行你的Spring Web應(yīng)用。
Spring Boot的主要優(yōu)點(diǎn):
1、為所有Spring開發(fā)者更快的入門
2、開箱即用,提供各種默認(rèn)配置來簡化項目配置
3、內(nèi)嵌式容器簡化Web項目
4、沒有冗余代碼生成和XML配置的要求
在下面的代碼中只要有一定基礎(chǔ)會發(fā)現(xiàn)這寫代碼實例非常簡單對于開發(fā)者來說幾乎是“零配置”。
SpringBoot運(yùn)行
開發(fā)工具:jdk8,IDEA,STS,eclipse(需要安裝STS插件)這些都支持快速啟動SpringBoot工程。我這里就不快速啟動了,使用maven工程。學(xué)習(xí)任何一項技術(shù)首先就要精通HelloWord,那我們來跑個初體驗。
首先只用maven我們創(chuàng)建的maven工程直接以jar包的形式創(chuàng)建就行了,首先我們來引入SpringBoot的依賴
首先我們需要依賴SpringBoot父工程,這是每個項目中必須要有的。
org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE UTF-8 UTF-8 1.8
我們啟動WEB模塊當(dāng)然必須要引入WEB模塊的依賴
org.springframework.boot spring-boot-starter-web
我們需要編寫一個SpringBoot啟動類,SpringbootFirstExperienceApplication.java
- @SpringBootApplication
- public class SpringbootFirstExperienceApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringbootFirstExperienceApplication.class, args);
- }
- }
到了這里我們直接把他當(dāng)成SpringMVC來使用就行了,不過這里默認(rèn)是不支持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:在默認(rèn)SpringBoot工程中是不生成這個文件夾的,但是在自動配置中我們可以有這個文件夾用來存放公共的資源(html等)
application.properties:這個文件名字是固定的,SpringBoot啟動會默認(rèn)加載這些配置在這里面可以配置端口號,訪問路徑,數(shù)據(jù)庫連接信息等等。這個文件非常重要,當(dāng)然官方中推出了一個yml格式這是非常強(qiáng)大的數(shù)據(jù)格式。
整合JdbcTemplate
引入依賴:
org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java org.springframework.boot spring-boot-starter-test test
配置application.properties,雖然說是“零配置”但是這些必要的肯定要指定,否則它怎么知道連那個數(shù)據(jù)庫?
- 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";
- }
- }
這里我們直接返回一個文本格式。
@RestController
在上面的代碼中我們使用到這個注解修改我們的Controller類而是不使用@Controller這個注解,其實中包含了@Controller,同時包含@ResponseBody既然修飾在類上面那么就是表示這個類中所有的方法都是@ResponseBody所以在這里我們返回字符串在前臺我們會以文本格式展示,如果是對象那么它會自動轉(zhuǎn)換成json格式返回。
整合JSP
在創(chuàng)建整合JSP的時候指定要選WAR,一定要選WAR。
引入依賴:
org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.apache.tomcat.embed tomcat-embed-jasper
然后我們只需要配置試圖解析器路徑就可以了。
- #配置試圖解析器前綴
- spring.mvc.view.prefix=/WEB-INF/views/
- #配置試圖解析器后綴
- spring.mvc.view.suffix=.jsp
整合JPA
同樣的整合JPA我們只需要啟動我們SpringBoot已經(jīng)集成好的模塊即可。
添加依賴:
org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java
啟動JPA組件后直接配置數(shù)據(jù)庫連接信息就可以使用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
實體類: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
{ - }
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
引入依賴:
org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-logging org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.2 org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java
配置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 指定我們實體類所在包位置
- mybatis.type-aliases-package=com.simple.springboot.mybatis.entity
當(dāng)然這里還有很多屬性如果想要使用可以參考官方文檔。到了這里其他就不寫了,把他當(dāng)作SSM使用就ok。
注意事項:在我們的Dao層接口中一定要在類上加上注解@Mapper否則無法掃描到。
AOP功能使用
在我們SpringBoot中使用AOP非常簡單。
- 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 {
- /**
- * 定義一個切入點(diǎn)
- * @author:SimpleWu
- * @Date:2018年10月12日
- */
- @Pointcut(value="execution(* com.simple.springboot.util.*.*(..))")
- public void aop(){}
- /**
- * 定義一個前置通知
- * @author:SimpleWu
- * @Date:2018年10月12日
- */
- @Before("aop()")
- public void aopBefore(){
- System.out.println("前置通知 SpringBootAspect....aopBefore");
- }
- /**
- * 定義一個后置通知
- * @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)集成好一個調(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)在時間:" + dateFormat.format(new Date()));
- }
- }
然后啟動的時候我們必須要在主函數(shù)類上加上注解:@EnableScheduling(翻譯過來就是開啟調(diào)度)
- /**
- * SpringBoot使用任務(wù)調(diào)度
- * @EnableScheduling標(biāo)注程序開啟任務(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);
- }
- }
整合RabbitMq
安裝RabbitMq 由于RabbitMQ依賴Erlang, 所以需要先安裝Erlang。
- sudo yum install -y make gcc gcc-c++ m4 openssl openssl-devel ncurses-devel unixODBC unixODBC-devel java java-devel
- sudo yum install epel-release
- sudo yum install erlang
- sudo yum install socat
下載RabbitMQ,并且安裝
- sudo wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el7.noarch.rpm
- sudo yum install rabbitmq-server-3.6.15-1.el7.noarch.rpm
進(jìn)入cd /etc/rabbitmq/ 創(chuàng)建vim rabbitmq.config
- [{rabbit, [{loopback_users, []}]}].
這里的意思是開放使用,rabbitmq默認(rèn)創(chuàng)建的用戶guest,密碼也是guest,這個用戶默認(rèn)只能是本機(jī)訪問,localhost或者127.0.0.1,從外部訪問需要添加上面的配置。如果沒有找到清除日志
- rm rabbit\@[email protected]
最好還是直接sudo rabbitmqctl set_user_tags root administrator ,給root用戶賦值管理員權(quán)限 RabbitMQ,基本操作
- # 添加開機(jī)啟動RabbitMQ服務(wù)
- systemctl enable rabbitmq-server.service
- # 查看服務(wù)狀態(tài)
- systemctl status rabbitmq-server.service
- # 啟動服務(wù)
- systemctl start rabbitmq-server.service
- # 停止服務(wù)
- systemctl stop rabbitmq-server.service
- # 查看當(dāng)前所有用戶
- rabbitmqctl list_users
- # 查看默認(rèn)guest用戶的權(quán)限
- rabbitmqctl list_user_permissions guest
- # 由于RabbitMQ默認(rèn)的賬號用戶名和密碼都是guest。為了安全起見, 先刪掉默認(rèn)用戶
- rabbitmqctl delete_user guest
- # 添加新用戶
- rabbitmqctl add_user username password
- # 設(shè)置用戶tag
- rabbitmqctl set_user_tags username administrator
- # 賦予用戶默認(rèn)vhost的全部操作權(quán)限
- rabbitmqctl set_permissions -p / username ".*" ".*" ".*"
- # 查看用戶的權(quán)限
- rabbitmqctl list_user_permissions username
如果只從命令行操作RabbitMQ,多少有點(diǎn)不方便。幸好RabbitMQ自帶了web管理界面,只需要啟動插件便可以使用。
- rabbitmq-plugins enable rabbitmq_management
訪問: http://服務(wù)器IP:15672
整合RabbitMq
導(dǎo)入Maven依賴
org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE 1.8 org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
設(shè)置application.properties配置文件
- spring.application.name=springboot-rabbitmq
- #RabbitMq所在服務(wù)器IP
- spring.rabbitmq.host=192.168.197.133
- #連接端口號
- spring.rabbitmq.port=5672
- #用戶名
- spring.rabbitmq.username=root
- #用戶密碼
- spring.rabbitmq.password=123456
- # 開啟發(fā)送確認(rèn)
- spring.rabbitmq.publisher-confirms=true
- # 開啟發(fā)送失敗退回
- spring.rabbitmq.publisher-returns=true
- spring.rabbitmq.virtual-host=/
創(chuàng)建RabbitMq隊列初始化類,初始化隊列
- /**
- * @author SimpleWu
- * @Date 2019-05-17
- * 該類初始化隊列
- */
- @Configuration
- public class RabbitMqInitialization {
- /**
- * 創(chuàng)建隊列 隊列名字為SayQueue
- * @return
- */
- @Bean
- public Queue SayQueue() {
- return new Queue("SayQueue");
- }
- }
創(chuàng)建生產(chǎn)者
- /**
- * @author SimpleWu
- * @Date 2019-05-17
- * 生產(chǎn)者
- */
- @Component
- public class SayProducer {
- @Autowired
- private RabbitTemplate rabbitTemplate;
- public void send(String name){
- String sendMsg = "hello: " + name + " " + new Date();
- //指定隊列
- this.rabbitTemplate.convertAndSend("SayQueue", sendMsg);
- }
- }
創(chuàng)建消費(fèi)者 @RabbitListener:當(dāng)監(jiān)聽到隊列 SayQueue 中有消息時則會進(jìn)行接收并處理 @RabbitHandler :標(biāo)注在類上面表示當(dāng)有收到消息的時候,就交給 @RabbitHandler 的方法處理,具體使用哪個方法處理,根據(jù) MessageConverter 轉(zhuǎn)換后的參數(shù)類型
- /**
- * @author SimpleWu
- * @Date 2019-05-17
- * 消費(fèi)者
- * queues 指定監(jiān)聽的隊列
- */
- @Component
- @RabbitListener(queues = "SayQueue")
- public class SayConsumer {
- @RabbitHandler
- public void process(String hello) {
- System.out.println("SayConsumer : " + hello);
- }
- }
創(chuàng)建接口進(jìn)行測試
- @RestController
- public class SayController {
- @Autowired
- private SayProducer sayProducer;
- @RequestMapping("/send/{name}")
- public String send(@PathVariable String name){
- sayProducer.send(name);
- return "Send Succcess SimpleWu";
- }
- }
啟動類就用IDEA默認(rèn)生成的就好了。http://10.192.132.22:8080/send/First 發(fā)送請求 消費(fèi)者接受消息:SayConsumer : hello: First Tue May 07 17:57:02 CST 2019 注:在傳輸對象時一定要序列化
整合郵件發(fā)送
導(dǎo)入依賴
org.springframework.boot spring-boot-starter-mail
配置Propert
當(dāng)前標(biāo)題:SpringBoot:企業(yè)常用的Starter以及實現(xiàn)
當(dāng)前地址:http://fisionsoft.com.cn/article/ccoicse.html


咨詢
建站咨詢
