我希望能够自动装配单个bean(foo
)
@Component
public class FooUser {
@Autowire Foo foo;
}
由另一个单身人士的方法(FooFactory.createFoo
)
@Service
public class FooFactory {
public Foo createFoo() {...}
}
使用xml只需factory-method
。我怎么能用注释来做呢?
答案 0 :(得分:13)
尝试使用Java @Configuration
:
@Configuration
public class Config {
@Bean
public FooUser fooUser() {
return new FooUser(foo());
}
@Bean
public FooFactory fooFactory() {
return new FooFactory();
}
@Bean
public Foo foo() {
return fooFactory().createFoo();
}
}
答案 1 :(得分:4)
您需要java-config - the @Bean
annotation。
将您的班级定义为@Configuration
,将您的方法定义为@Bean
答案 2 :(得分:3)
Spring Components也可以定义工厂方法。来自documentation:
的剪辑@Component
public class FactoryMethodComponent {
@Bean @Qualifier("public")
public TestBean publicInstance() {
return new TestBean("publicInstance");
}
public void doWork() {
// Component method implementation omitted
}
}
答案 3 :(得分:0)
使用FactoryBean Spring接口。那么你将能够自动使用T
编辑: BeanFactory是Spring中的一个接口,如果你实现它,你可以创建一个对象的工厂,如:
public class FooFactoryBean implements FactoryBean<Foo>{
..................
}
然后你可以初始化bean:
@Bean
public FooFactoryBean foo(){
return new FooFactoryBean();
}
然后如果你自动装配Foo它Spring会理解FooFactoryBean是所需的工厂
@Autowired
Foo foo;
答案 4 :(得分:0)
Spring Boot:工厂方法
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
enum ParsersConst {
bofa, jpm, wellsforgo
}
interface Parser {
String readFromFile(String file);
}
class JPM implements Parser {
@Override
public String readFromFile(String file) {
System.out.println("From JPM Parser");
//LOGIC to read file data
return "JPM";
}
}
class Bofa implements Parser {
@Override
public String readFromFile(String file) {
System.out.println("From Bofa Parser");
//LOGIC to read file data
return "BOFA";
}
}
class WellsForgo implements Parser {
@Override
public String readFromFile(String file) {
System.out.println("From Wellsforgo Parser");
//LOGIC to read file data
return "WellsForgo";
}
}
class ParserCreator {
private Map<ParsersConst, Parser> parserMap;
public Parser createParser(ParsersConst parsConst) {
Parser parser = parserMap.get(parsConst);
if (parserMap.get(parsConst) != null) {
return parser;
}
throw new IllegalArgumentException("Unknown Parser");
}
public void setParserMap(Map<ParsersConst, Parser> parserMap) {
this.parserMap = parserMap;
}
}
@Configuration
class ParserConfig {
@Bean
public ParserCreator parserCreatorFactory() {
ParserCreator factory = new ParserCreator();
Map<ParsersConst, Parser> map = new HashMap<ParsersConst, Parser>();
map.put(ParsersConst.bofa, new Bofa());
map.put(ParsersConst.wellsforgo, new WellsForgo());
map.put(ParsersConst.jpm, new JPM());
factory.setParserMap(map);
return factory;
}
@Bean
public Parser bofa() {
return parserCreatorFactory().createParser(ParsersConst.bofa);
}
@Bean
public Parser wellsforgo() {
return parserCreatorFactory().createParser(ParsersConst.wellsforgo);
}
@Bean
public Parser jpm() {
return parserCreatorFactory().createParser(ParsersConst.jpm);
}
}
@Component
public class StaticFacotryDemo implements CommandLineRunner {
@Autowired
private ApplicationContext context;
@Override
public void run(String... args) throws Exception {
Parser parser = (Parser) context.getBean(ParsersConst.jpm.toString());
System.out.println(parser.readFromFile("jan_stmt.pdf"));
}
}