二 SpringBoot - 核心配置文件( 三 )

4.3 測試類@RestControllerpublic class SpringBootConfigController {@Autowired@Qualifier("userRole2")UserRole userRole;//可以實現自定義實體加入容器@GetMapping("/testUserRole")public UserRole testUserRole(){return userRole;}}運行結果:

二 SpringBoot - 核心配置文件

文章插圖
5、激活環境5.1 多套環境配置文件激活環境 (實際開發中 , 主要有三個環境:開發環境,測試環境,生產環境(線上環境),還有一個環境 , 灰度環境,也是線上環境,叫預上限環境);
好處:可以隔離不同環境的不同配置,需要使用哪個環境 , 就直接切換核心配置文件;
application-devp.propertiesapplication-prod.propertiesapplication-test.properties5.2激活環境active: test # 指定當前的profiles值,環境是什么是通過核心配置文件名中,application-${profiles},profiles寫的是什么就是什么環境;
spring:profiles:active: test#激活測試環境6、核心配置文件加載位置優先級從高到底依次為:項目根路徑下的config目錄項目根路徑下類路徑(resource)下的類路徑(resource)下注意:模塊項目的 項目根路徑 是 父項目的根路徑;
二 SpringBoot - 核心配置文件

文章插圖
7、郵件發送 和 短信測試發送7.1 郵件發送7.1.1 依賴<!--spring-boot-starter-mail start--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><!--spring-boot-starter-mail end-->7.1.2 郵件配置信息7.1.3類里面寫配置信息配置信息直接寫在 對象里面;
@GetMapping("/sendEmail")public String sendEmail(@RequestParam(value = "https://www.huyubaike.com/biancheng/setToEmail",required = false) String setToEmail){System.out.println("--------------[mail/mailSend] start------------------");try {MimeMessage message=javaMailSender.createMimeMessage();MimeMessageHelper helper=new MimeMessageHelper(message,true);helper.setFrom("2663092414@qq.com","2663092414");helper.setTo(setToEmail);helper.setSubject("KH-96-王松—核心配置文件讀取");helper.setText("正在使用SpringBoot讀取自定義核心配置,發送郵件成功!<br/>"+studentProperties.toString(),true);javaMailSender.send(message);} catch (Exception e) {System.out.println("郵件發送失敗"+ e.getMessage());e.printStackTrace();}System.out.println("--------------[mail/mailSend] end------------------");return studentProperties.toString();}//實例化javaMailSender 并寫入配置信息private static JavaMailSenderImpl javaMailSender;static {javaMailSender = new JavaMailSenderImpl();javaMailSender.setHost("smtp.qq.com");//鏈接服務器//javaMailSender.setPort(25);//默認使用25端口發送javaMailSender.setUsername("2663092414@qq.com");//賬號javaMailSender.setPassword("dwxlbkrmdyagebhe");//授權碼javaMailSender.setDefaultEncoding("UTF-8");Properties properties = new Properties();//properties.setProperty("mail.debug", "true");//啟用調試//properties.setProperty("mail.smtp.timeout", "1000");//設置鏈接超時//設置通過ssl協議使用465端口發送、使用默認端口(25)時下面三行不需要properties.setProperty("mail.smtp.auth", "true");//開啟認證properties.setProperty("mail.smtp.socketFactory.port", "465");//設置ssl端口properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");javaMailSender.setJavaMailProperties(properties);}7.1.4 application.yaml中寫配置信息7.1.4.1application.yamlspring:mail:default-encoding: UTF-8host: smtp.qq.comport: 587username: xxxxxx@qq.compassword: 授權碼7.1.4.2 請求方法@GetMapping("/sendEmail2")public String sendEmail2(@RequestParam(value = "https://www.huyubaike.com/biancheng/setToEmail",required = false) String setToEmail){SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setFrom("xxxxxx@qq.com"); //發送郵箱mailMessage.setTo("xxxxxx@qq.com"); //目標郵箱mailMessage.setText("你好 hello world");mailMessage.setSubject("測試 Springboot 郵箱服務");mailSender.send(mailMessage);return "====完成發送!====";}7.2 短信測試發送7.2.1 依賴<!-- SMS star --><dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.9</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>tea</artifactId><version>1.1.14</version></dependency><!-- SMS end -->7.2.2 代碼其中:accessKeyId ,accessKeySecret 填寫自己的用戶 AccessKey , 最好用子用戶 AccessKey;
public class Sample {/*** 使用AK&SK初始化賬號Client** @param accessKeyId* @param accessKeySecret* @return Client* @throws Exception*/public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()// 您的 AccessKey ID.setAccessKeyId(accessKeyId)// 您的 AccessKey Secret.setAccessKeySecret(accessKeySecret);// 訪問的域名config.endpoint = "dysmsapi.aliyuncs.com";return new com.aliyun.dysmsapi20170525.Client(config);}public static void main(String[] args_) throws Exception {java.util.List<String> args = java.util.Arrays.asList(args_);com.aliyun.dysmsapi20170525.Client client = Sample.createClient("accessKeyId", "accessKeySecret");//accessKeyId ,accessKeySecret 填寫自己的用戶信息com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setSignName("阿里云短信測試").setTemplateCode("SMS_154950909").setPhoneNumbers("發送短信的手機號").setTemplateParam("{\"code\":\"131313\"}");com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();try {// 復制代碼運行請自行打印 API 的返回值SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);} catch (TeaException error) {// 如有需要 , 請打印 errorString errerMsg = Common.assertAsString(error.message);} catch (Exception _error) {TeaException error = new TeaException(_error.getMessage(), _error);// 如有需要,請打印 errorString errorMsg = Common.assertAsString(error.message);}}}

推薦閱讀