SpringBoot 项目以jar 包方式获取ApplicationContext 和SpringBoot项目以war 包方式获取ApplicationContext解析

你猜 阅读:229 2021-03-31 12:54:34 评论:0

问题场景:今天遇到ApplicationContext 获取Bean 对象总是为null 的问题,我在本地启动SpringBoot 项目正常访问,但是将SpringBoot 项目打包成war 包部署到Tomcat的服务器获取Bean对象总是提示为null。

项目结构关系:springboot-provider 为服务提供者(采用war 包方式部署在tomcat中)

                         springboot-consumer为服务消费者(采用jar 包方式启动)   备注:两者的服务调用是通过alibaba dubobo 协议实现

                         springboot-import 为xml 转实体对象工具jar,其中有两个类对象,需要依赖后台接口服务(查询字典数据)

 

springboot-provider  程序入口(未修改之前)

 
 
import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
 
import org.mybatis.spring.annotation.MapperScan; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.ApplicationContext; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.context.support.WebApplicationContextUtils; 
 
 
/** 
 * 系统服务提供者 
 *  
 * @author zzg 
 * 
 */ 
@SpringBootApplication 
@EnableTransactionManagement 
@MapperScan("com.digipower.ucas.mapper") 
public class Provider extends SpringBootServletInitializer { 
 
	public static void main(String[] args) { 
		ApplicationContext applicationContext = SpringApplication.run(Provider.class, args); 
		SpringContextUtil.setApplicationContext(applicationContext); 
		System.out.println("============= SpringBoot gcxt-system-provider Service Start Success ============="); 
	} 
 
	@Override 
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
		// 注意这里要指向原先用main方法执行的Application启动类 
		return builder.sources(Provider.class); 
	} 
 
 
} 

springboot-provider  程序入口(修改后)

 
import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
 
import org.mybatis.spring.annotation.MapperScan; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.ApplicationContext; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.context.support.WebApplicationContextUtils; 
 
 
/** 
 * 系统服务提供者 
 *  
 * @author zzg 
 * 
 */ 
@SpringBootApplication 
@EnableTransactionManagement 
@MapperScan("com.digipower.ucas.mapper") 
public class Provider extends SpringBootServletInitializer { 
 
    // springboot 本地启动初始化applicationContext 
	public static void main(String[] args) { 
		ApplicationContext applicationContext = SpringApplication.run(Provider.class, args); 
		SpringContextUtil.setApplicationContext(applicationContext); 
		System.out.println("============= SpringBoot gcxt-system-provider Service Start Success ============="); 
	} 
 
	@Override 
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
		// 注意这里要指向原先用main方法执行的Application启动类 
		return builder.sources(Provider.class); 
	} 
 
     // tomcat方式启动初始化applicationContext 
	@Override 
	public void onStartup(ServletContext servletContext) throws ServletException { 
		super.onStartup(servletContext); 
		SpringContextUtil 
				.setApplicationContext(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)); 
	} 
 
} 

 springboot-import  [email protected] 注解标签初始化化相关Bean

 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
 
import ****.field.DataDictFieldProcess; 
import ****..field.PersonCategoryFieldProcess; 
 
 
/** 
 * 电子包接收配置 
 * @author zzg 
 * 
 */ 
@Configuration 
@ComponentScan({"***.impexp.field"}) //集成第三方jar 包推荐使用 
public class ReceiveConfig { 
	@Autowired 
	private SysDataDictService sysDataDictService; 
	 
	@Bean("PersonCategory") 
	public PersonCategoryFieldProcess personCategory(){ 
		return new PersonCategoryFieldProcess(); 
	} 
	 
	@Bean("DataDict") 
	public DataDictFieldProcess dataDict(){ 
		return new DataDictFieldProcess(sysDataDictService); 
	} 
} 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们