gpt4 book ai didi

java - 没有 XML Spring ApplicationContext

转载 作者:数据小太阳 更新时间:2023-10-29 02:00:18 24 4
gpt4 key购买 nike

我正在尝试使用和不使用 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 = ?
}
}

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

最佳答案

此处的 Spring 引用指南中有很好的示例: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

你可以在 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..
}
}

关于java - 没有 XML Spring ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26050047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com