【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

相关文章

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾