BAEL-453: How to use the Spring FactoryBean - use custom bean name (#866)

* custom bean name

* @Autowire
This commit is contained in:
Diane Duan 2016-11-30 00:41:30 +08:00 committed by Grzegorz Piwowarek
parent b6403fc1f0
commit 7676697c4c
3 changed files with 7 additions and 9 deletions

View File

@ -6,16 +6,11 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class FactoryBeanAppConfig {
@Bean
@Bean(name = "tool")
public ToolFactory toolFactory() {
ToolFactory factory = new ToolFactory();
factory.setFactoryId(7070);
factory.setToolId(2);
return factory;
}
@Bean
public Tool tool() throws Exception {
return toolFactory().getObject();
}
}

View File

@ -7,6 +7,8 @@ import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -14,9 +16,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(classes = FactoryBeanAppConfig.class)
public class FactoryBeanJavaConfigTest {
@Resource
@Autowired
private Tool tool;
@Resource(name = "&toolFactory")
@Resource(name = "&tool")
private ToolFactory toolFactory;
@Test

View File

@ -7,6 +7,7 @@ import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -14,7 +15,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations = { "classpath:factorybean-spring-ctx.xml" })
public class FactoryBeanXmlConfigTest {
@Resource
@Autowired
private Tool tool;
@Resource(name = "&tool")
private ToolFactory toolFactory;