`

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这个属性。

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Spring的三种注入方式

    2.autowire=“byName”  3.autowire=bytype    详细解析注入方式  例如:有如下两个类需要注入  第一个类: 1.package org.jia;  2.  3. public class Order {  4. private String orderNum;...

    spring-autowire.zip

    在idea2020 下写的spring5-autowired 小例子。person,cat dog ,给大家一个参考。

    spring-autowire-demo.zip

    以Spring5.3.6为演示基础,以多个范例显示演示了autowire多种形式的应用 default、byName、byType、constructor 并对相关使用做了一定的对比

    spring示例代码好又全.rar

    内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...

    storm-spring-autowire:使storm支持spring的注入功能

    storm-spring-autowire 使storm支持spring的注入功能 由于storm的生命周期如下 1.在提交了一个topology之后(是在nimbus所在的机器么?), 创建spout/bolt实例(spout/bolt在storm中统称为component)并进行序列化. 2.将...

    多线程Autowire注入null的解决方案.docx

    多线程Autowire注入null的解决方案.docx

    Spring实战之@Autowire注解用法详解

    主要介绍了Spring实战之@Autowire注解用法,结合实例形式详细分析了Spring @Autowire注解具体实现步骤与相关使用技巧,需要的朋友可以参考下

    Spring2.0+quartz1.8定时执行任务内含Cron表达式生成器

    &lt;bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;ref bean="runTime" /&gt; &lt;/bean&gt;

    基于SpringJDBC的BaseDAO

    实现了简单的ORM增删改查。 ... abstract="false" lazy-init="default" autowire="default" dependency-check="default"&gt; &lt;property name="dataSource"&gt; &lt;ref local="dataSource" /&gt; &lt;/bean&gt;

    Spring3.2.4+Quartz2.2.0 Demo

    &lt;bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;ref bean="myJobTrigger" /&gt; ...

    spring2.5学习PPT 传智博客

    07_编码剖析Spring依赖注入的原理 08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类型的属性 10_使用构造器装配属性 11_用@Resource注解完成属性装配 12_编码剖析@Resource注解的实现原理 ...

    spring4示例代码

    spring的生命周期及BeanPostProcessor的使用,注解方式创建bean 及使用Autowried标签关联类的属性 ,泛型依赖注入的使用 spring-3 演示使用动态代理模式实现面向切面编程 使用注解方式进行AOP编程及使用配置xml方式...

    Autowire是Python的轻量级和简单的依赖注入库

    Autowire是Python的轻量级和简单的依赖注入库

    Spring的学习笔记

    (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:annotation方式Spring 16 一、 ...

    封装通用的Spring3+Struts2+MyBatis3的CRUD+条件分页查询,Spring+Quartz调度,FunctionCharts图像化工具

    &lt;bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;ref bean="getPolicyTime"/&gt; &lt;/beans&gt;

    spring源码15: 属性填充

    #spring属性注入分4种: 不开启自动注入,即xml自己配置property 通过名称自动注入 通过类型自动注入 @Autowire自动注入 本篇我们将介绍前三种,@Autowire应该是百分之99的开发者选择的的注入方式,它通过属性填充...

    维生药业小项目 SSH简单学习项目

    default-lazy-init="false" default-autowire="byName"&gt; &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt; &lt;property name="driverClass" value=...

    spring 的parent属性示例

    spring框架中bean标签parent属性的各类使用范例 总计12个范例,涵盖了class、init-method、 factory-method、lazy-init、 depends-on、autowire 多种属性的组合使用

    JAVA spring 系列案例50个和学习资料

    Spring系列第7篇:依赖注入之手动注入Spring系列第8篇:自动注入(autowire)详解,高手在于坚持Spring系列第9篇:depend-on到底是干什么的?Spring系列第10篇:primary可以解决什么问题?Spring系列第11篇:bean中...

Global site tag (gtag.js) - Google Analytics