Spring boot兼容旧Spring项目的方式

存在一些业务场景,之前一些比较老的项目时用Spring写的,但是想升级成Spring boot的代价又太高,那怎么办呢?

通过使用 @ImportResource 注解导入旧配置文件(注解写在启动类),方式如下:


@SpringBootApplication
@ImportResource(locations = {"classpath:spring.xml"})
public class Application{
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

测试

1.在配置文件加入测试bean:

   <bean id="testService" class="com.zsy.service.TestService"></bean>

2.测试类代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest{
	  
	@Autowired
	ApplicationContext ioc;
//判断容器中是否有testService实现类
	@Test
	public void testHelloService(){
		boolean b=ioc.containsBean("testService");
		System.out.println("容器中是否含有bean:"+b);
	}
}

3.运行日志打印:

容器是否含有bean:true

通过这种方式Spring boot能很好地兼容Spring项目,能节约大量的开发成本

猜你喜欢

转载自blog.csdn.net/syilt/article/details/92217652