Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 没有XML Spring应用程序上下文_Java_Xml_Spring_Spring Mvc - Fatal编程技术网

Java 没有XML Spring应用程序上下文

Java 没有XML Spring应用程序上下文,java,xml,spring,spring-mvc,Java,Xml,Spring,Spring Mvc,我正在尝试创建一个包含和不包含SpringXML配置的项目 首先,我创建xml配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.or

我正在尝试创建一个包含和不包含SpringXML配置的项目

首先,我创建xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<bean id="memoryManagerProduct" class="br.com.caelum.stock.ProductManager">
    <constructor-arg ref="memoryProductDAO"/>
</bean>

<bean id="memoryProductDAO" class="br.com.caelum.stock.dao.MemoryProductDAO"/>

</beans>

如何注入AppConfig中的配置以执行与testSpringXMLConfiguration中类似的测试?

在Spring参考指南中有一些很好的示例:

简而言之,您可以这样做:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

manager = (ProductManager) context.getBean("memoryManagerProduct");
然而,使用Spring进行测试的更好方法是使用Spring测试支持,详情如下-

通过spring测试支持,您可以执行以下操作:

@ContextConfiguration(classes = AppConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductManagerTest {

    @Autowired
    private ProductManager manager;

    @Test
    public void testSpringXMLConfiguration() {
    //use the productmanager in a test..
    }
}

你能帮我写这篇文章吗?你能帮我理解为什么我们需要真正的注射吗。我们可以使用@Mock获得mocking bean吗?
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

manager = (ProductManager) context.getBean("memoryManagerProduct");
@ContextConfiguration(classes = AppConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductManagerTest {

    @Autowired
    private ProductManager manager;

    @Test
    public void testSpringXMLConfiguration() {
    //use the productmanager in a test..
    }
}