概要
- 创建实现FactoryBean的工厂
- 在xml配置bean
- 使用bean
有时我们需要创建一些特定功能的bean,该如何做呢?
1.创建实现FactoryBean的工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class ProxyFactoryBean<T> implements FactoryBean<T> { private Class<T> proxyInterface; @Autowired private QrpcProxy qrpcProxy;
public ProxyFactoryBean() { }
public ProxyFactoryBean(Class<T> proxyInterface) { this.proxyInterface = proxyInterface; }
@Override public T getObject() { return qrpcProxy.create(proxyInterface); }
@Override public Class<?> getObjectType() { return this.proxyInterface; }
@Override public boolean isSingleton() { return true; } }
|
2.在xml配置bean
1 2 3
| <bean id="testService" class="com.qrpc.server.utils.ProxyFactoryBean"> <constructor-arg value="com.qrpc.api.facade.TestService"/> </bean>
|
3.使用
1 2 3 4 5 6 7 8 9 10 11 12
| @RestController public class TestController { @Autowired private TestService testService;
@GetMapping(BASE_GETSTRING) public String getString() { return testService.getString(); }
}
|