没有 XML Spring ApplicationContext

2024-03-19

我正在尝试创建一个带有或不带有 spring xml 配置的项目。

首先,我像这样创建 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>

我创建了非 XML 配置(我不知道是否正确,因为我不知道如何调用它)

package br.com.caelum.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import br.com.caelum.stock.ProductManager;
import br.com.caelum.stock.dao.MemoryProductDAO;
import br.com.caelum.stock.dao.Persistable;
import br.com.caelum.stock.model.Product;

@Configuration
public class AppConfig {

@Bean
public Persistable<Product> memoryProductDAO(){
    return new MemoryProductDAO();
}

@Bean
public ProductManager memoryProductManager(){
    return new ProductManager(memoryProductDAO());
}
}

我创建了一个 JUnit 测试:

package br.com.caelum.stock;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import br.com.caelum.stock.model.Product;

public class ProductManagerTest {

private ProductManager manager;

@Test
public void testSpringXMLConfiguration() {

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");

    manager = (ProductManager) context.getBean("memoryManagerProduct");

    Product product = new Product();
    product.setDescription("[Book] Spring in Action");
    product.setQuantity(10);

    manager.add(product);

    assertThat(manager.getProducts().get(0).getDescription(), is("[Book] Spring in Action"));
}

@Test
public void testSpringWithoutXMLConfiguration() {

    ApplicationContext context = ?
}
}

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


Spring 参考指南中有很好的例子:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java-instantiating-container http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java-instantiating-container

简而言之,你会这样做:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

manager = (ProductManager) context.getBean("memoryManagerProduct");

然而,使用 Spring 进行测试的更好方法是使用 spring-test 支持,详细信息在这里 -http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#integration-testing-annotations http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#integration-testing-annotations

您可以使用 spring-test 支持执行类似的操作:

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

    @Autowired
    private ProductManager manager;

    @Test
    public void testSpringXMLConfiguration() {
    //use the productmanager in a test..
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

没有 XML Spring ApplicationContext 的相关文章

随机推荐