spring单元测试容器启动类
package com.sand.spring.util; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.beans.BeansException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StringUtils; /** * 功能说明:spring容器启动类 * 开发人员:@author liusha * 开发日期:2020/3/30 15:34 * 功能描述:用于加载spring的配置文件 */ @RunWith(BlockJUnit4ClassRunner.class) public class SpringBootStrap { private String springXmlPath; private ClassPathXmlApplicationContext context; public SpringBootStrap() { } public SpringBootStrap(String springXmlPath) { this.springXmlPath = springXmlPath; } @Before public void before() { if (StringUtils.isEmpty(springXmlPath)) { springXmlPath = "classpath*:application-context.xml"; } try { context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+")); context.start(); System.out.println("容器启动完成!"); } catch (BeansException e) { System.out.println("容器启动失败!"); e.printStackTrace(); } } @After public void after() { System.out.println("关闭容器!"); context.destroy(); } protected <T extends Object> T getBean(String name) { return (T) context.getBean(name); } protected <T extends Object> T getBean(Class<?> clz) { return (T) context.getBean(clz); } }
千万不要试图去研究 研究了很久都整不明白的东西,或许是层次不到,境界未到,也或许是从未在实际的应用场景接触过,这种情况下去研究,只会事倍功半,徒劳一番罢了。能做的就是不断的沉淀知识,保持一颗积极向上的学习心态,相信终有一天所有的困难都会迎刃而解。