spring autowire="byName" 注入属性

byName 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自
动装配。例如,在bean定义中将 autowire设置为by name,而该bean包含master属性(同时提供
setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。

比如配置文件里面有如下配置

<bean id="userInfoService" class="com.mobile.base.core.thrift.client.ThriftClientFactoryBean" autowire="byName">
        <property name="className" value="com.thrift.live.UserInfoService"/>
    </bean>

 

ThriftClientFactoryBean 类如下:

 

package com.mobile.base.core.thrift.client;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class ThriftClientFactoryBean implements FactoryBean, InitializingBean {
    private Class ifaceClass;
    private Class clientClass;

    private String className;
    private ThriftClient thriftClient;

    public void setClassName(String className) {
        this.className = className;
    }

    public void setThriftClient(ThriftClient thriftClient) {
        this.thriftClient = thriftClient;
    }

    @Override
    public Object getObject() throws Exception {
        return Proxy.newProxyInstance(clientClass.getClassLoader(), clientClass.getInterfaces(), new ThriftProxy(thriftClient, className));
    }

    @Override
    public Class<?> getObjectType() {
        return ifaceClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ifaceClass = Class.forName(className + "$Iface");
        clientClass = Class.forName(className + "$Client");
    }

    class ThriftProxy implements InvocationHandler {
        private ThriftClient thriftClient;
        private String className;

        public ThriftProxy(ThriftClient thriftClient, String className) {
            this.thriftClient = thriftClient;
            this.className = className;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //modified by jesseling at 2015-3-12 to add detail of exception
            try {
                return method.invoke(thriftClient.get(className), args);
            } catch (InvocationTargetException e) {
                throw e.getCause();
            } catch (Exception e){
                throw e.getCause();
            } 
        }
    }
}

 有个属性叫thriftClient,并且有setThriftClient方法。

Spring 容器在实例化com.mobile.base.core.thrift.client.ThriftClientFactoryBean的时候,bean名字叫userInfoService,因为有autowire="byName",所以spring 会在容器里面找一个叫thriftClient的 bean,并把它注入到ThriftClientFactoryBean的属性里面(类似@Autowired),前提是ThriftClientFactoryBean有setThriftClient方法,并且spring容器里面有个bean叫ThriftClient,如下:

 

<bean id="thriftClient" class="com.mobile.base.core.thrift.client.ThriftClient" >
   	</bean>

 

如果把autowire="byName" 去掉,或者没有setThriftClient方法,spring在实例化ThriftClientFactoryBean的时候,都不会注入thriftClient这个属性。

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值