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


文章插图
4、*@Configuration配置类的用法,可以实现自定义组件加入容器4.1 实体@Data@Builder@AllArgsConstructor@NoArgsConstructorpublic class UserRole {//角色private String roleId;//角色名称private String roleName;}4.2 UserRoleConfig 配置类@Configuration //凡是被此注解修饰的类,就是一个配置类,在项目启动是,自动加载,功能跟spring的核心配置文件xml文件是同等的public class UserRoleConfig {//手动添加自定义对象,放入容器中以前spring框架,通过xml配置文件,添加<bean id="xx" class="xx">...</bran>@Bean //标注的方法,会自动将当前方法返回的实例对象放入容器中,默认的bean的id值就是方法名public UserRole userRole1(){return UserRole.builder().roleId("R001").roleName("admin").build();}@Beanpublic UserRole userRole2(){return UserRole.builder().roleId("R002").roleName("admin").build();}}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);}

经验总结扩展阅读