1.spring boot簡介:
1)spring boot是基於spring開發的,提供了一種快速使用spring的方式,無需xml配置文件
2)解決的問題:
大量配置
依賴問題
重復性配置
3)springboot思想:
自動配置
起步依賴:需要什么功能,就引入需要的庫
命令行頁面
actuator
2.入門使用:
1)創建一個jar工程 //使用spring創建web工程時,打包方式選擇jar,因為spring boot內置了tomcat
2)起步依賴: //原理:依賴傳遞
通過spring boot 啟動器,進行依賴的快速添加,如一個web工程只需要引入兩個依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> //定義父啟動器
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> //導入web啟動器
</dependency>
</dependencies>
3)變更jdk版本 //默認為jdk1.6
只需要在pom.xml中配置一個常量即可:
<properties>
<java.version>1.7</java.version>
</properties>
4)引導類:用於啟動spring boot工程
1)只需要創建一個類Application,編寫main方法,在main方法中添加一個run方法即可,在類上添加注解:@SpringBootApplication
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); //第一個參數為本類的.class,第二個args對應main方法的參數
}
}
2)直接運行這個main方法即可啟動spring boot工程
5)helloWorldController類的編寫: //就是一個springmvc的Controller:
@RestController
public class HelloWorldController {
@RequestMapping("/info")
public String info(){
return "HelloWorld";
}
}
6)直接啟動引導類,即可啟動這個工程:在瀏覽器地址欄輸入 http://localhost:8080/info 即可看到運行結果
原理:引導類的main方法上的@SpringBootApplication會對工程進行默認的配置。會掃描與引導類在同一個包下的類上的注解
7)spring boot工程默認端口為8080,修改端口的方式:
1)在 src/main/resources 下創建 application.properties //這個文件的名稱固定
2)在文件中配置端口的常量: //重新啟動工程,即可生效
server.port=9001
3)也可以在這個配置文件中自定義自己需要的屬性:
url=www.baidu.com
8)配置文件application.properties中的屬性值的獲取
1)在需要獲取屬性的類中注入一個Environment對象:
@Autowired
private Environment env; //注意這個Environment所在的包:...env包下
2)直接調用Environment的getProperty()即可
env.getProperty("url");
@RestController
public class HelloWorldController {
@Autowired
private Environment env;
@RequestMapping("/info")
public String info(){
return "HelloWorld~~"+env.getProperty("url");
}
}
9)spring boot工程的熱部署:
我們在開發中反復修改類、頁面等資源,每次修改后都是需要重新啟動才生效,這樣每次啟動都很麻煩,浪費了大量的時間。可以在pom.xml 中添加如下配置實現spring boot工程的熱部署:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> //添加這個依賴后,修改配置文件或類中內容時,不需要重新啟動即可生效
</dependency>
3.sprign boot 和activemq的整合:
1)加入activemq的啟動依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2)消息生產者的編寫
@RestController
public class QueueController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate; //注入模板
@RequestMapping("/send")
public void send(String text){
jmsMessagingTemplate.convertAndSend("itcast", text); //通過模板發送消息,第一個參數為消息名稱,第二個參數為消息內容
}
}
注意:也可直接發送一個Map對象的消息,將上面方法中的text改為需要發送的Map對象即可
3)消息消費者:
@Component
public class Consumer {
@JmsListener(destination="itcast") //添加注解監聽"itcast"消息
public void readMessage(String text){ //方法的參數即為接收到的消息,參數類型與消息類型對應 //如果接收的是Map,則參數要為Map類型
System.out.println("接收到消息:"+text);
}
}
注意:上面使用的activemq使用的是spring boot 內嵌的服務。使用外部的activemq的方法:
在application.properties中配置一個常量即可:
spring.activemq.broker-url=tcp://192.168.25.135:61616 //常量名稱固定,ip為外部activemq的IP
4.短信微服務:業務輕量級,獨立實現某個功能
1)創建工程,導入依賴:
2)配置jdk版本為1.7
3)spring-boot-starter-parent //啟動器父包
4)spring-boot-starter-web //web工程包
5)spring-boot-starter-activemq //activemq包
6)aliyun-java-sdk-dysmsapi //阿里大於發短信的包
7)aliyun-java-sdk-core //阿里大於發短信的包
2)創建引導類 //同上面的引導類
3)創建配置文件 application.properties
server.port=9003 //配置端口
spring.activemq.broker-url=tcp://192.168.25.135:61616 //配置連接activemq的ip端口
accessKeyId=不告訴你 //配置發短信的id
accessKeySecret=不告訴你 //配置發短信的密鑰
4)創建發短信的工具類: //參考阿里大於發短信的工具類
@Component //需要加入
public class SmsUtil { //對應阿里大於中的
@Autowired
private Environment env; //注入Environment獲取配置文件信息
public SendSmsResponse sendSms(String mobile,String template_code,String //方法中的需要的參數放在形參中動態獲取
sign_name,String param) throws ClientException {
5)編寫消息監聽類 //消息消費者
@Component
public class SmsListener {
@Autowired
private SmsUtil smsUtil;
@JmsListener(destination="sms")
public void sendSms(Map<String,String> map){
try {
SendSmsResponse response = smsUtil.sendSms(
map.get("mobile"),
map.get("template_code"),
map.get("sign_name"),
map.get("param") );
System.out.println("Code=" + response.getCode());
System.out.println("Message=" + response.getMessage());
System.out.println("RequestId=" + response.getRequestId());
System.out.println("BizId=" + response.getBizId());
} catch (ClientException e) {
e.printStackTrace();
}
}
}