2014-09-26 65 views
3

我想創建一個項目,並且沒有使用spring xml配置。沒有XML Spring ApplicationContext

首先,在所有的,我創造我的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 = ? 
} 
} 

我怎麼能注入configura在我的AppConfig中進行類似於我的testSpringXMLConfiguration的測試嗎?

回答

10

有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"); 

然而,隨着彈簧試驗的一個更好的辦法是使用的彈簧測試支持,詳情請看這裏 - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#integration-testing-annotations

你可以做這樣的事情,彈簧試驗支持:

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

    @Autowired 
    private ProductManager manager; 

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

能否請你幫我解決這個問題http://stackoverflow.com/questions/26231165/improve-performance-on-sending-bulk-emails-through-spring-mail/26233308#26233308 – 2014-10-07 10:37:43