【ActiveMQ】ActiveMQ与Spring实现发邮件

2024-08-26 22:32

本文主要是介绍【ActiveMQ】ActiveMQ与Spring实现发邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

消费者如何知道生产者的动静?当生产者发送了消息到ActiveMQ中的时候,消费者是如何轻松的获得该消息的。

这是由JMS提供的三种消息监听器实现的。我们先来了解基础概念:


1、MessageListener


这是JMS的一个接口,定义了一个用于处理接受到消息的onMessage方法,该接口仅仅一个这样的方法,

也仅仅有Message一个参数。他就是为了接受消息的。请见以下代码:

package com.consume;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;public class ConsumerMessageListener implements MessageListener{public void onMessage(Message message) {TextMessage textMessage=(TextMessage) message;System.out.println("哈哈哈哈哈");try {System.out.println("消费者:"+textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}}

2、SessionAwareMessageListener


这个是由spring为我们提供的,他不是JMS的MessageListener,这个接口仅仅有一个方法,那就是onMessage

方法,有2个参数,一个参数用来接受当前的消息message,另一个发送消息的sesson对象。其中session对象就是

用来告诉消息的提供者发送回复消息的。

请看这个接口的定义:



请见以下代码:

@Component
public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener<Message> {private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);@Autowiredprivate JmsTemplate activeMqJmsTemplate;@Autowiredprivate Destination sessionAwareQueue;@Autowiredprivate MailBiz bailBiz;public synchronized void onMessage(Message message, Session session) {try {ActiveMQTextMessage msg = (ActiveMQTextMessage) message;final String ms = msg.getText();log.info("==>receive message:" + ms);MailParam mailParam = JSONObject.parseObject(ms, MailParam.class);// 转换成相应的对象if (mailParam == null) {return;}try {bailBiz.mailSend(mailParam);} catch (Exception e) {// 发送异常,重新放回队列
//				activeMqJmsTemplate.send(sessionAwareQueue, new MessageCreator() {
//					public Message createMessage(Session session) throws JMSException {
//						return session.createTextMessage(ms);
//					}
//				});log.error("==>MailException:", e);}} catch (Exception e) {log.error("==>", e);}}
}


Spring和ActiveMQ结合,首先需要在配置文件中配置,也就是当配置文件一加载,开始实例化监听器,然后就开始监听生产者的消息了。所以需要配置<bean>.


3、MessageListenerAdapter

    由以上可知,这个接口实现了上面的2个接口,所以除了有上面的方法外,还有很多其他的方法。


 邮件发送的例子:

我们用邮件发送的例子来介绍一下生产者和消费者的行为。

首先生产者定义发送邮件的内容,和发送给谁,然后消费者运用接听来时时刻刻监听是否发送的人是自己,假如是自己,则受到消息。


首先我们来建立生产者工程


1、新建maven工程,引用spring和ActiveMQ的jar包


<dependencies><dependency><groupId>antlr</groupId><artifactId>antlr</artifactId></dependency><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><classifier>jdk15</classifier><scope>compile</scope></dependency><dependency><groupId>ognl</groupId><artifactId>ognl</artifactId></dependency><dependency><groupId>oro</groupId><artifactId>oro</artifactId></dependency><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId></dependency><dependency><groupId>commons-digester</groupId><artifactId>commons-digester</artifactId></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></dependency><dependency><groupId>commons-validator</groupId><artifactId>commons-validator</artifactId></dependency><dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId></dependency><dependency><groupId>net.sf.ezmorph</groupId><artifactId>ezmorph</artifactId></dependency><dependency><groupId>javassist</groupId><artifactId>javassist</artifactId></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><!-- Common Dependency End --><!-- Spring Dependency Begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-oxm</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId></dependency></dependencies>

       2、新建配置文件,分别是spring的配置文件和mq的配置文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!-- 采用注释的方式配置bean --><context:annotation-config /><!-- 配置要扫描的包 --><context:component-scan base-package="mq.demo" /><!-- 读入配置属性文件 --><context:property-placeholder location="classpath:mq.properties" /><!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --><aop:aspectj-autoproxy proxy-target-class="true" />	<import resource="spring-mq.xml" />
</beans>

         mq的配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><!-- ActiveMQ服务地址 --><property name="brokerURL" value="${mq.brokerURL}" /><property name="userName" value="${mq.userName}"></property><property name="password" value="${mq.password}"></property> </bean><!-- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。要依赖于 activemq-pool包--><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"><property name="connectionFactory" ref="targetConnectionFactory" /><property name="maxConnections" value="${mq.pool.maxConnections}" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><!-- 队列模板 --><bean id="activeMqJmsTemplate" class="org.springframework.jms.core.JmsTemplate">  <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  <property name="connectionFactory" ref="connectionFactory"/>  <property name="defaultDestinationName" value="${queueName}"></property></bean> </beans>


          mq.properties文件的内容:


## MQ
mq.brokerURL=tcp\://192.168.120.153\:61616
mq.userName=lifen
mq.password=lifen.123
mq.pool.maxConnections=10
#queueName
queueName=fen.email.demo


3、由于是发送邮件,新建一个邮件的封装实体类,类名为MailParam


public class MailParam {/** 发件人 **/private String from;/** 收件人 **/private String to;/** 主题 **/private String subject;/** 邮件内容 **/private String content;public MailParam() {}public MailParam(String to, String subject, String content) {this.to = to;this.subject = subject;this.content = content;}public String getFrom() {return from;}public void setFrom(String from) {this.from = from;}public String getTo() {return to;}public void setTo(String to) {this.to = to;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}
}


       4、 接下来就是MQ的生产者代码:


@Service("mqProducer")
public class MQProducer {@Autowiredprivate JmsTemplate activeMqJmsTemplate;/*** 发送消息.* @param mail */public void sendMessage(final MailParam mail) {activeMqJmsTemplate.send(new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage(JSONObject.toJSONString(mail));}});}}

5、 新建测试类,我们来测试一下:


public class MQProducerTest {private static final Log log = LogFactory.getLog(MQProducerTest.class);public static void main(String[] args) {try {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");context.start();MQProducer mqProducer = (MQProducer) context.getBean("mqProducer");// 邮件发送MailParam mail = new MailParam();mail.setTo("lf0908lf@163.com");mail.setSubject("ActiveMQ测试");mail.setContent("通过ActiveMQ异步发送邮件!一定会成功的");mqProducer.sendMessage(mail);context.stop();} catch (Exception e) {log.error("==>MQ context start error:", e);System.exit(0);} finally {log.info("===>System.exit");System.exit(0);}}
}



我们在ActiveMQ的管理页面,就可以看到一个名为fen.email.demo的队列了。


         接下来我们建立消费者的工程,

1、同样引用jar包,和生产者的jar包一样。

2、接着就是配置文件

spring-context的配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!-- 采用注释的方式配置bean --><context:annotation-config /><!-- 配置要扫描的包 --><context:component-scan base-package="mq.demo" /><!-- 读入配置属性文件 --><context:property-placeholder location="classpath:mq.properties,classpath:mail.properties" /><!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --><aop:aspectj-autoproxy proxy-target-class="true" />	<import resource="spring-mq.xml" /><import resource="spring-mail.xml" /></beans>

spring-mail的配置文件:


<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"><!-- Spring提供的发送电子邮件的高级抽象类 --><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="${mail.host}" /><property name="username" value="${mail.username}" /><property name="password" value="${mail.password}" /><property name="defaultEncoding" value="UTF-8"></property><property name="javaMailProperties"><props><prop key="mail.smtp.auth">${mail.smtp.auth}</prop><prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop></props></property></bean><bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"><property name="from"><value>${mail.default.from}</value></property></bean><!-- 配置线程池 --><bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"><!-- 线程池维护线程的最少数量 --><property name="corePoolSize" value="5" /><!-- 线程池维护线程所允许的空闲时间 --><property name="keepAliveSeconds" value="30000" /><!-- 线程池维护线程的最大数量 --><property name="maxPoolSize" value="50" /><!-- 线程池所使用的缓冲队列 --><property name="queueCapacity" value="100" /></bean></beans>

spring-mq的配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!--default-lazy-init spring 在启动的时候,会默认加载整个对象实例图,从初始化ACTION配置,到service再到dao配置,乃至到数据库的连接。事务等等,当设置为false的时候,会大大减少初始化的时间,减少spring的启动速度。在开发阶段,可以设置为false但是到了生产环境中,设置为true--><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><!-- ActiveMQ服务地址 --><property name="brokerURL" value="${mq.brokerURL}" /><property name="userName" value="${mq.userName}"></property><property name="password" value="${mq.password}"></property> </bean><!-- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。要依赖于 activemq-pool包--><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"><property name="connectionFactory" ref="targetConnectionFactory" /><property name="maxConnections" value="${mq.pool.maxConnections}" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><!-- 队列模板 --><bean id="activeMqJmsTemplate" class="org.springframework.jms.core.JmsTemplate">  <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  <property name="connectionFactory" ref="connectionFactory"/>  <property name="defaultDestinationName" value="${queueName}"></property></bean> <!--这个是sessionAwareQueue目的地 --><bean id="sessionAwareQueue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg><value>${queueName}</value></constructor-arg></bean><!-- 可以获取session的MessageListener --><bean id="consumerSessionAwareMessageListener" class="mq.demo.mqtest.listener.ConsumerSessionAwareMessageListener"></bean><bean id="sessionAwareListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="sessionAwareQueue" /><property name="messageListener" ref="consumerSessionAwareMessageListener" /></bean></beans>

下面看一下文本文件:


mail.properties


#SMTP服务配置
#mail.host=smtp.qq.com
mail.host=smtp.163.com
mail.username=&&&&&   #邮箱的账号
#mail.port=25
mail.password=&&&&&  #邮箱号的密码
mail.smtp.auth=true
mail.smtp.timeout=30000
mail.default.from=&&&&  #邮箱的账号

mq.properties

## MQ
mq.brokerURL=tcp\://192.168.120.153\:61616
mq.userName=lifen
mq.password=lifen.123
mq.pool.maxConnections=10
#queueName
queueName=fen.email.demo



3、同时也有一个封装邮件参数的实体,和生产者一样,不再叙述。

4、队列监听器,时时刻刻监听者生产端:


@Component
public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener<Message> {private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);@Autowiredprivate JmsTemplate activeMqJmsTemplate;@Autowiredprivate Destination sessionAwareQueue;@Autowiredprivate MailBiz bailBiz;public synchronized void onMessage(Message message, Session session) {try {ActiveMQTextMessage msg = (ActiveMQTextMessage) message;final String ms = msg.getText();log.info("==>receive message:" + ms);MailParam mailParam = JSONObject.parseObject(ms, MailParam.class);// 转换成相应的对象if (mailParam == null) {return;}try {bailBiz.mailSend(mailParam);} catch (Exception e) {// 发送异常,重新放回队列
//				activeMqJmsTemplate.send(sessionAwareQueue, new MessageCreator() {
//					public Message createMessage(Session session) throws JMSException {
//						return session.createTextMessage(ms);
//					}
//				});log.error("==>MailException:", e);}} catch (Exception e) {log.error("==>", e);}}
}


5、邮件的业务逻辑类:


@Component("mailBiz")
public class MailBiz {@Autowiredprivate JavaMailSender mailSender;// spring配置中定义@Autowiredprivate SimpleMailMessage simpleMailMessage;// spring配置中定义@Autowiredprivate ThreadPoolTaskExecutor threadPool;/*** 发送模板邮件* * @param mailParamTemp需要设置四个参数*            templateName,toMail,subject,mapModel* @throws Exception* */public void mailSend(final MailParam mailParam) {threadPool.execute(new Runnable() {public void run() {try {simpleMailMessage.setFrom(simpleMailMessage.getFrom()); // 发送人,从配置文件中取得simpleMailMessage.setTo(mailParam.getTo()); // 接收人simpleMailMessage.setSubject(mailParam.getSubject());simpleMailMessage.setText(mailParam.getContent());mailSender.send(simpleMailMessage);} catch (MailException e) {throw e;}}});}
}


6、我们测试一下消费者,由于是加载配置文件的时候自动启动监听机制,开始监听,所以我们仅仅需要加载配置文件即可:


public class MQConsumerTest {private static final Log log = LogFactory.getLog(MQConsumer.class);public static void main(String[] args) {try {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");context.start();} catch (Exception e) {log.error("==>MQ context start error:", e);System.exit(0);}}
}


如图:

                          

提醒:

在发送邮件的时候,需要开启以下的服务,要不然会报认证失败的错误,同时也要保证上面的用户名和密码都是正确的。

                 


                ActiveMQ的例子还是比较简单的。

这篇关于【ActiveMQ】ActiveMQ与Spring实现发邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1109860

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows