12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package cn.nosum.support.config;
- import cn.nosum.support.proxy.InvocationHandlerFactory;
- import cn.nosum.support.proxy.Target;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.FactoryBean;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.util.StringUtils;
- /**
- * Dynamic client factory bean.
- *
- * @author Young
- */
- public class DynamicClientFactoryBean implements FactoryBean<Object>, ApplicationContextAware {
- private ApplicationContext applicationContext;
- private String name;
- private Class<?> type;
- private String refName;
- private Class<?> refType;
- public void setName(String name) {
- this.name = name;
- }
- public void setType(Class<?> type) {
- this.type = type;
- }
- public void setRefName(String refName) {
- this.refName = refName;
- }
- public void setRefType(Class<?> refType) {
- this.refType = refType;
- }
- @Override
- public Object getObject() throws Exception {
- return getTarget();
- }
- <T> T getTarget() {
- Targeter targeter = applicationContext.getBean(Targeter.class);
- InvocationHandlerFactory handlerFactory = applicationContext.getBean(InvocationHandlerFactory.class);
- if (!StringUtils.hasText(this.refName)){
- String[] beanNamesForType = applicationContext.getBeanNamesForType(this.refType);
- this.refName = beanNamesForType[0];
- }
- return (T) targeter.target(this, handlerFactory,new Target.DefaultTarget(this.name, this.type, this.refName, this.refType) );
- }
- @Override
- public Class<?> getObjectType() {
- return this.type;
- }
- @Override
- public boolean isSingleton() {
- return true;
- }
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- this.applicationContext = applicationContext;
- }
- }
|