二 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);}}}

推荐阅读