asynchronous

  1. Add @enableAsync to the SpringBootApplication class to enable asynchrony

  2. Add @async to methods that need to be asynchronous, and SpringBoot automatically creates threads for them

mail

Import dependence

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Copy the code

Modifying a Configuration File

In MailSenderAutoConfiguration @ EnableConfigurationProperties (MailProperties. Class)

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
// Properties file
@EnableConfigurationProperties(MailProperties.class)
@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })
public class MailSenderAutoConfiguration {}Copy the code

Click on MailProperties to see @ConfigurationProperties(prefix = “spring.mail”) and some of its properties

@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {}
Copy the code

So we can set some Mail properties in application.properties

Spring.mail. [email protected] spring.mail.password= XXXXXXXXXXXXXX spring.mail.host=smtp.qq.com # encryption verification spring.mail.properties.mail.smtp.ssl.enable=trueCopy the code

The instance

class DemoApplicationTests {
	@Autowired
	JavaMailSenderImpl MailSender;
	@Test
	void contextLoads(a) {
		SimpleMailMessage sm = new SimpleMailMessage();
		sm.setTo("[email protected]");
		sm.setFrom("[email protected]");
// sm.setFrom("[email protected]");
		sm.setText("test text");
		sm.setSubject("test subject");
		MailSender.send(sm);
	}
	@Test
	void contextLoads2(a) throws MessagingException {
		// Complex message
		MimeMessage m= MailSender.createMimeMessage();
		// Assemble, true means multiple files are supported
		MimeMessageHelper helper=new MimeMessageHelper(m,true);
// MimeMailMessage sm=new MimeMailMessage(m);
		helper.setTo("[email protected]");
		helper.setFrom("[email protected]");
// helper.setFrom("[email protected]");
		//true indicates that HTML is enabled and the p tag is recognized
		helper.setText("<p style='color:red'>test text2</p>".true);
		helper.setSubject("test subject2");
		helper.addAttachment("1.png".new File("D:\\Desktop\\Springboot\\SpringBoot-10\\src\\main\\resources\\static\\1.png")); MailSender.send(m); }}Copy the code

timing

  1. Add @enablescheduling to enable the timing function
@EnableScheduling
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code
  1. Add @scheduled to the method to be recognized as a Scheduled task
@Service
public class SchedulingService {
    // seconds minute hour day month week
    // Run this command at 21:44:31 every day
    @Scheduled(cron = "30 44 21 * * ?" )
    public void hello(a){
        System.out.println("hello"); }}Copy the code