junit单元测试service,手动加载spring配置文件

mac2024-05-09  41

 

//手动加载spring配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"/spring/spring-mvc.xml","/spring/spring-redis.xml","/spring/spring-mybatis.xml","/spring/spring-main.xml"}); //获取bean,getBean("commandService")中的commandService指的是@Service("commandService") CommandMapper commandService = (CommandMapper) applicationContext.getBean("commandService"); @Service("commandService") public class CommandServiceImpl implements CommandMapper{ ... ... }

1.加载多个配置文件

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"/spring/spring-mvc.xml","/spring/spring-redis.xml","/spring/spring-mybatis.xml","/spring/spring-main.xml"});

2.获取bean方式

@Service("commandService") public class CommandServiceImpl implements CommandMapper{ ... ... } CommandMapper a=(CommandMapper) applicationContext.getBean("commandService");

 

最新回复(0)