我们平时写test去测试Spring一起的程序时,一般都要加载context,然后再取bean之类。
写法如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest {
@Autowired
MyService service;
...
}
现在的问题是,我要获取一个bean,但是上面这种通过annotation申明xml配置文件的方式,和我们原来的那种(如下),那光光写在annotation,那我怎么通过context的reference去获取bean呢?
解决办法
其实申明一个context的成员变量,autowired一下就ok了
@Autowired
ApplicationContext context;
package com.test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
public class SpringMain {
/**
* @param args
*/
public static void main(String[] args) {
SpringBean bean = null;
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
bean = (SpringBean)factory.getBean("test1");
bean.sayHello();
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
bean = (SpringBean)act.getBean("test1");
bean.sayHello();
factory = new XmlBeanFactory(new FileSystemResource("F:\\ccc\\workspace_zuhe\\Test\\src\\applicationContext.xml"));
bean = (SpringBean)factory.getBean("test1");
bean.sayHello();
}
}
--------------------------------------
如果有多个xml的配置文件:
@ContextConfiguration(locations = {"classpath:com/xxx/aaa/applicationContext.xml",
"classpath:com/xxx/aaa/common-config.xml",
"classpath:com/xxx/aaa/applicationContext-dao.xml",})
分享到:
相关推荐
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)//Spring的Junit测试,会在...
如何读取webroot文件下的属性文件
spring-test.jar 用在junit4以上 在类上写 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"WIN-INF/config/applicationContext.xml" })
2)spring-test的注解:@ContextConfiguration(locations={"classpath:applicationContext-test.xml"}) 5.方便svn管理,不会导致无效版本代码泛滥,没有经过测试的代码绝对不能上传 6.运行时修改配置,通过@Before...
@ContextConfiguration @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, ...
@ContextConfiguration 参数locations="classpath:spring-mybatis.xml" 指向src下的该文件 执行顺序: @BeforeClass---@Before---@Test---@After---@Before ---@Test---@After---@AfterClass junit与main方法相比的...
使用java配置时将SpringTestCase.java 文件中的@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) 注释掉 使用xml配置时。将com.test.config 下的Java文件中@Configuration 都注释掉。
使用java配置时将SpringTestCase.java 文件中的@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) 注释掉 使用xml配置时。将com.test.config 下的Java文件中@Configuration 都注释掉。
在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载applicationContext.xml @Resource(name="userService")意思是把userService注入进来 最终输出结果为:...
Mockito / JUnit4 / MockMvc / dbunit + Stub + ContextConfiguration(DI 测试也可用) 计划:将添加 Spring Batch/Task 目前:未实施 Schedule:Spring Integration(通过Rabbitmq处理node项目发来的消息,发送到...
Mockito / JUnit4 / MockMvc / dbunit + Stub + ContextConfiguration(DI 测试也可用) 计划:将添加 Spring Batch/Task js 项目 (Node.js) Backbone.js / jQuery / 下划线 用于 Node.js + RabbitMQ 的 AMQP ...
Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.... 2.... 3.... ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则...使用容器对象去获取Service对象
定义Spring配置定义Spring相关配置 @Lazy // default Lazy ...@ContextConfiguration ( classes = DemoApiSpringConfig . class) public class DemoRestApiTest extends BaseRestApiTest { } 定义Spring aop定义Spring
class)@ContextConfiguration ( classes = ExistConfiguration . class)class SimpleExistXQJRepositoryTests { @Inject private SimpleXQJRepository< Integer> existRepository; @Test void emptySequence () { ...
比如你没法像hibernate那样直接传一个对象给它让他拆分成sql并保存起来,当然这也是可以理解的,毕竟它并没有要求你去写 hbm.xml 文件所以无法知道你哪些字段要映射,哪些不要等等。又比如JdbcTemplate 可以帮忙把一...