文章目录
- 1.web.xml(重点)
- 2.springmvc.xml
- 2.1springmvc.xml(雷哥)
- 3.spring-dao.xml(重点)
- 3.1spring-dao.xml(雷哥)
- 4.spring-service.xml
- 4.1spring-service.xml(雷哥)
- 5.db.properties
- 6.applicationContext.xml
- 7.common.properties
- 8.spring-dubbo-provider.xml(一般在service层)
- 9.spring-dubbo-consumer.xml(一般在web层)
- 10.spring-mq-consumer.xml(一般在service层)
- 11.spring-mq-producer.xml(一般在web层)
- 12.spring-redis.xml(一般在web层)
- 13.springmvc 里面
- 14.mq的监听实现
- web项目的基本依赖
1.web.xml(重点)
1加载ioc 容器
2 加载springmvc
3 加载编码器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 1 加载ioc -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置前端控制器开始 -->
<!-- 2 加载springmvc 需要springmvc.xml -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--用来标记是否在项目启动时就加在此Servlet,0或正数表示容器在应用启动时就加载这个Servlet, 当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载.正数值越小启动优先值越高 -->
<load-on-startup>0</load-on-startup>
</servlet>
<!-- 引用mvc --><!--为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置前端控制器结束 -->
<!-- 3 加载编码器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- 引用编码器 只对post 请求有效,get 无效-->
<filter-mapping>
<filter-name>encoding</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping>
</web-app>
2.springmvc.xml
1 包扫描
2 驱动开发
3 视图解析器
4 文件上传解析器
5 拦截器
6 静态资源
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 1 包扫描 -->
<!-- 2 注解开发 -->
<!-- 3 视图解析器 -->
<!-- 4 文件上传解析器 -->
<!-- 5 拦截器 -->
<!-- 6 静态资源 -->
<!-- 配置映射器和适配器 -->
<context:component-scan
base-package="com.sxt.controller" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 使用的是字节 50M -->
<property name="maxUploadSize" value="52428800"></property>
<!-- 可以设置一个临时的目录 uploadTempDir,但是注意,我们以上文件不放在tomcat ,需要放在文件服务器里面,我们以后不需要设置这个值 -->
</bean>
<!-- <mvc:interceptors></mvc:interceptors> -->
<!-- 4 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="loginInterceptor" class="com.sxt.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<mvc:resources location="/WEB-INF/statics/" mapping="/**"></mvc:resources>
</beans>
2.1springmvc.xml(雷哥)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 扫描控制器 -->
<context:component-scan base-package="com.sxt.controller"></context:component-scan>
<!-- 启动配置注解的映射器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置转发地址的前缀 -->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- 配置转发地址的后缀缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 过滤放行静态资源文件 -->
<mvc:default-servlet-handler/>
<!-- 文件上传的支持 -->
<!-- 配置对文件上传的支持 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!--设置文件上传的最大在小 -->
<property name="maxUploadSize" value="21474836480"></property>
<!--设置临时目录 -->
<property name="uploadTempDir" value="/upload/temp"></property>
</bean>
<!-- 拦截器 -->
<!-- 4 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="loginInterceptor" class="com.sxt.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
3.spring-dao.xml(重点)
1 数据源
2 sqlSessionFactory
3 MapperScan
<!-- 数据源 -->
<!-- sqlSessionFactory -->
<!-- mapperScan -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="url" value="${db.url}"></property>
<property name="driverClassName"
value="${db.driverClassName}"></property>
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.cfg"></property>
</bean>
<bean id="mapperScan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sxt.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
3.1spring-dao.xml(雷哥)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 加载db.properties -->
<context:property-placeholder location="classpath:db.properties"
system-properties-mode="FALLBACK" />
<!-- 声明数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init">
<!-- 注入数据库的连接属性 -->
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 监控数据库 -->
<property name="filters" value="${filters}" />
</bean>
<!-- 声明mybatis的配置 -->
<bean id="configuration" class="org.apache.ibatis.session.Configuration">
<!-- 设置日志输出形式 -->
<property name="logImpl" value="org.apache.ibatis.logging.log4j.Log4jImpl"></property>
</bean>
<!-- 声明sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置configration configLocation这两个属性不能同时存在 -->
<property name="configuration" ref="configuration"></property>
<!-- 配置插件 -->
<property name="plugins">
<!-- 分页插件 -->
<array>
<bean class="com.github.pagehelper.PageInterceptor"></bean>
</array>
</property>
<!-- 配置映射文件 -->
<property name="mapperLocations">
<array>
<value>classpath:mapper/*Mapper.xml</value>
</array>
</property>
</bean>
<!-- 配置mapper的扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入扫描的包 -->
<property name="basePackage">
<value>com.sxt.mapper</value>
</property>
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
4.spring-service.xml
配置事务时,需要spring-jdbc的依赖
1 包扫描
2 事务配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan
base-package="com.sxt.service.impl"></context:component-scan>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.1spring-service.xml(雷哥)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 扫描 -->
<context:component-scan base-package="com.sxt.service.impl"></context:component-scan>
<!-- 声明一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的传播特性-->
<tx:advice id="myAdvise" transaction-manager="transactionManager">
<!-- 配置方法的传播特性 -->
<tx:attributes>
<!--
propagation 的属性值 说明
REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。 [不推荐]
MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。 [不推荐]
REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。 [不推荐]
NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 [不推荐]
NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。 [不推荐]
NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与REQUIRED类 似的操作 【可能用到】
read-only="true",只读事务,对数据库只能是查询操
timeout :设置事务的超时时间 -1代表没有超时时间 500 如果service的方法500毫秒还执行完成,那么就回滚事务
-->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="mod*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="reset*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 进行AOP织入 -->
<aop:config>
<!-- 声明切面 -->
<aop:pointcut expression="execution(* com.sxt.service.impl.*.*(..))" id="pc1"/>
<!-- 织入 -->
<aop:advisor advice-ref="myAdvise" pointcut-ref="pc1"/>
</aop:config>
</beans>
5.db.properties
db.user=root
db.password=123456
db.url=jdbc:mysql://192.168.201.139:3306/ego
db.driverClassName=com.mysql.jdbc.Driver
6.applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder
location="classpath:properties/db.properties,classpath:properties/common.properties" />
<import resource="classpath:spring-dao.xml" />
<import resource="classpath:spring-service.xml" />
<import resource="classpath:spring-aop.xml" />
<import resource="classpath:spring-dubbo-provider.xml" />
<!-- <import resource="classpath:spring-mq-consumer.xml" /> -->
</beans>
7.common.properties
zk.url=192.168.201.139:2181
app.port=6666
mq.url=tcp://192.168.201.139:61616
mq.size=10
8.spring-dubbo-provider.xml(一般在service层)
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 1 应用名称 -->
<dubbo:application name="order-service"></dubbo:application>
<!-- 2 注册中心 -->
<dubbo:registry protocol="zookeeper" address="${zk.url}"
client="zkclient"></dubbo:registry>
<!-- 3 应用端口 -->
<dubbo:protocol port="${app.port}"></dubbo:protocol>
<!-- 4 包扫描 -->
<dubbo:annotation package="com.sxt.service.impl" />
</beans>
9.spring-dubbo-consumer.xml(一般在web层)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:property-placeholder location="classpath:properties/common.properties"/>
<!-- 1 应用名称 -->
<dubbo:application name="order-web"></dubbo:application>
<!-- 2 注册中心 -->
<dubbo:registry protocol="zookeeper" address="${zk.url}" client="zkclient"></dubbo:registry>
<!-- 3 包扫描 -->
<dubbo:annotation package="com.sxt.controller"/>
</beans>
10.spring-mq-consumer.xml(一般在service层)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 单独的连接工厂 -->
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="${mq.url}"></constructor-arg>
<property name="trustAllPackages" value="true"></property>
</bean>
<!-- 池特性的连接工厂 -->
<bean id="cacheConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg name="targetConnectionFactory"
ref="connectionFactory"></constructor-arg>
<property name="sessionCacheSize" value="${mq.size}"></property>
</bean>
<!-- 1 消费的方式有2 个,一个xml配置文件 1 -->
<!-- <jms:listener-container acknowledge="client" -->
<!-- connection-factory="cacheConnectionFactory" destination-type="topic" -->
<!-- concurrency="3-5"> -->
<!-- <jms:listener destination="ego.user.login" ref="监听器" /> -->
<!-- </jms:listener-container> -->
<!-- 2注解的形式 -->
<bean id="containerFactory"
class="org.springframework.jms.config.SimpleJmsListenerContainerFactory">
<property name="connectionFactory" ref="cacheConnectionFactory"></property>
<property name="sessionAcknowledgeMode" value="2"></property>
<property name="pubSubDomain" value="false"></property>
</bean>
<jms:annotation-driven container-factory="containerFactory" />
<context:component-scan
base-package="com.sxt.listener"></context:component-scan>
</beans>
11.spring-mq-producer.xml(一般在web层)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 单独的连接工厂 -->
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="${mq.url}"></constructor-arg>
</bean>
<!-- 池特性的连接工厂 -->
<bean id="cacheConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg name="targetConnectionFactory"
ref="connectionFactory"></constructor-arg>
<property name="sessionCacheSize" value="${mq.size}"></property>
</bean>
<!-- 消息的发送者,生产者 JmsTempalte -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<constructor-arg name="connectionFactory"
ref="cacheConnectionFactory"></constructor-arg>
<!-- 主题模式 -->
<property name="pubSubDomain" value="false"></property>
</bean>
</beans>
12.spring-redis.xml(一般在web层)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 池的配置对象 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.max.total}"></property>
<property name="maxIdle" value="${redis.max.idle}"></property>
<property name="minIdle" value="${redis.min.idle}"></property>
</bean>
<!-- 连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis.port}"></constructor-arg>
</bean>
</beans>
13.springmvc 里面
<!-- 在mvc里面聚合dubbo的容器 -->
<import resource="classpath:spring-dubbo-consumer.xml" />
<context:component-scan
base-package="com.sxt.aspect.web"></context:component-scan>
<import resource="classpath:spring-aop.xml" />
14.mq的监听实现
api
public interface MessageService {
/**
* token是接口调用的唯一凭证,2小时过期,需要定时任务来说刷新
*
*/
void refreshToekn();
void sendMessage(WechatMessage message);
}
listener
package com.sxt.listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import com.sxt.domain.WechatMessage;
import com.sxt.service.MessageService;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.HashMap;
import java.util.Map;
@Component
public class CarOrderLockListener implements MessageListener {
@Autowired
private MessageService messageService;
@Override
@JmsListener(destination="CAR.ORDER.LOCK")
public void onMessage(Message message) {
System.out.println("锁车监听成功");
TextMessage textMessage = (TextMessage) message;
try {
String token = textMessage.getText();
// token 通过token 可以换用户
WechatMessage wehcatMessage = new WechatMessage();
wehcatMessage.setTemplateId("6bGx4ezg5MYFySDPF_F_DrYkWRQ0xr2KjlIkf5EqJIk");
wehcatMessage.setToUser("okGGy5-ZPMz_j6woHkXYKNeGT7dI");
wehcatMessage.setUrl("https://www.sso.ego.com");
Map<String, Map<String, String>> data = new HashMap<String, Map<String, String>>();
data.put("user", WechatMessage.getMap("si", ""));
data.put("type", WechatMessage.getMap("pc端", ""));
data.put("username", WechatMessage.getMap("司天宏", ""));
data.put("time", WechatMessage.getMap("2010-06-01 17:52", ""));
data.put("network", WechatMessage.getMap("单车", ""));
data.put("url", WechatMessage.getMap("www.manager.com", ""));
wehcatMessage.setData(data);
messageService.sendMessage(wehcatMessage);
message.acknowledge();
System.out.println("请查看微信--------------锁定车了--------------------");
} catch (JMSException e) {
e.printStackTrace();
}
}
}
serviceImpl
package com.sxt.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.sxt.domain.AccessToken;
import com.sxt.domain.WechatMessage;
import com.sxt.domain.WechatResult;
import com.sxt.service.MessageService;
@Service // 不需要远程调用,所有的调用,使用mq 的机制完成
public class MessageServiceImpl implements MessageService {
@Autowired
private RestTemplate restTemplate;
private String accessToken;
@Value("${wechat.app.id}")
private String appId;
@Value("${wechat.secret}")
private String secret;
private static final int RETRYNUM = 3;
private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static String MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
@Scheduled(initialDelay=10,fixedRate=7100*1000)// 2 个小时刷新一次
@Override
public void refreshToekn() {
/**
* 会自动将json 转换为对象
*/
ACCESS_TOKEN_URL = ACCESS_TOKEN_URL.replaceAll("APPID", appId).replaceAll("APPSECRET", secret);
AccessToken token = restTemplate.getForObject(ACCESS_TOKEN_URL, AccessToken.class);
accessToken = token.getAccessToken() ;
System.out.println("token 获取成功"+accessToken);
}
@Override
public void sendMessage(WechatMessage message) {
int i = 1;
while(i <= RETRYNUM) { // 重试发送的机制
boolean ok = sendWechatMessage(message);
if(ok) { // 发送成功
System.out.println("微信消息发送成功");
return ;
}else {
System.out.println("微信消息发送失败");
i ++ ;
}
}
}
public boolean sendWechatMessage(WechatMessage message) {
MESSAGE_URL = MESSAGE_URL.replaceAll("ACCESS_TOKEN", accessToken);
// 将message - > json
WechatResult result = restTemplate.postForObject(MESSAGE_URL, message, WechatResult.class);
if(result.getErrCode()==0) {
return true;
}else { // 发送失败?重试发送n次
return false;
}
}
}
启动
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
app.start();
try {
System.out.println("carmessage 已经准备就绪");
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
web项目的基本依赖
<!--
Jsp 编译使用
https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- org/apache/commons/fileupload/FileItemFactory 因为springmvc 里面的文件上传解析器需要fileUpload
这个依赖 -->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<!-- 需要commons-io的依赖 -->
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--
对象->json
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>