文章目录
- 1. pom依赖
- 2. xml配置文件
- 3. 获取参数工具类
- 4. 根据beanId从SpringBootIOC获取类实例工具类
- 5. 测试类
- 6. 浏览器+控制台验证
1. pom依赖
<properties><!--全局版本控制--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.version>4.3.10.RELEASE</spring.version><lombok.version>1.18.8</lombok.version></properties><!-- Spring MVC support Start--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.2.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><!-- Spring MVC support End-->
2. xml配置文件
applicationContext-wscontainer.xml
web.xml中记得写上,通配符*.xml可跳过
这里面constructor-arg标签就是参数,根据需要定义需要的参数即可
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--纽约交易所接口 保单查询 --><bean id="beanId" class="com.gblfy.order.utils.WSContainer" scope="singleton"><constructor-arg value="http://192.168.0.27:8080/services/webservice?wsdl"/><constructor-arg value="NamespaceURI"/><constructor-arg value="Method"/><constructor-arg value="QUEUE_NAME"/><constructor-arg value="QUEUE_TYPE"/><constructor-arg value="QUEUE_DESC"/></bean>
</beans>
3. 获取参数工具类
package com.gblfy.order.utils;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class WSContainer {private String url;//请求地址private String namespaceURI;//方法的命名空间private String method;//调用的方法private String queueName; //队列名称private String queueType;//队列类型private String queueDesc;//队列描述
}
4. 根据beanId从SpringBootIOC获取类实例工具类
SpringContextUtils
package com.gblfy.order.utils;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/*** @author gblfy 2020-04-29* @apiNote spring上下文工具类,用于普通类调用springIOC中的对象*/
@Component
public class SpringContextUtils implements ApplicationContextAware {private static ApplicationContext applicationContext = null;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {if (SpringContextUtils.applicationContext == null) {SpringContextUtils.applicationContext = applicationContext;}}/*** @apiNote 获取applicationContext*/public static ApplicationContext getApplicationContext() {return applicationContext;}/*** @apiNote 通过name获取 Bean.*/public static Object getBean(String beanId) {return getApplicationContext().getBean(beanId);}/*** @apiNote 通过class获取Bean.*/public static <T> T getBean(Class<T> clazz) {return getApplicationContext().getBean(clazz);}/*** @apiNote 通过name, 以及Clazz返回指定的Bean*/public static <T> T getBean(String name, Class<T> clazz) {return getApplicationContext().getBean(name, clazz);}
}
5. 测试类
package com.gblfy.order.controller;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gblfy.order.confirm.ConfirmCallBackListener;
import com.gblfy.order.utils.SpringContextUtils;
import com.gblfy.order.utils.WSContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;/*** 项目启动,将接口配置文件加载到spring的IOC容器中,* 通过接口的BeanId从配置文件中获取指定接口的信息配置参数*/
@Controller
public class WSContainerController {//日志输出private final static Logger log = LoggerFactory.getLogger(ConfirmCallBackListener.class);//引入json工具类private static final ObjectMapper MAPPER = new ObjectMapper();@RequestMapping("/getWSParams")@ResponseBodypublic String getWSParams() throws JsonProcessingException {//这个beanId 建议是接口名 能够标识区分是哪一支接口String serviceBeanId = "beanId";/*** 1.在xml文件中声明接口的beanId* 2.把需要的参数配置到xml文件中* 3.通过接口的beanId获取指定接口的配置信息*/WSContainer wsContainer = (WSContainer) SpringContextUtils.getBean(serviceBeanId);log.info("接收请求的Url : {}", wsContainer.getUrl());log.info("接收请求的命名空间: {}", wsContainer.getNamespaceURI());log.info("接收请求的方法名称: {}", wsContainer.getMethod());log.info("接收请求的队列名称: {}", wsContainer.getQueueName());log.info("接收请求的队列类型: {}", wsContainer.getQueueType());log.info("接收请求的队列描述: {}", wsContainer.getQueueDesc());
// Gson gson= new Gson();//把对象转换成 jsonStr 类型便与解析return MAPPER.writeValueAsString(wsContainer);}
}
6. 浏览器+控制台验证