`
ducaijun
  • 浏览: 155410 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring注入的几种方式

阅读更多

1、setter方式(最常用)

  就是在bean中的属性必须有setter方法 然后在xml在配置

2、构造方法(不常用)

参考官方api的例子:

 

<beans>
    <bean name="foo" class="x.y.Foo">
        <constructor-arg>
            <bean class="x.y.Bar"/><!--相当于new了一个Bar对象-->
        </constructor-arg>
        <constructor-arg>
            <bean class="x.y.Baz"/>
        </constructor-arg>
    </bean>
</beans>

 

 如果构造方法需要多个参数 则可以按照参数索引赋值或类型赋值(参数中没有相同类型 )

a.按参数类型传给构造方法注入[ Constructor Argument Type Matching ]

 

<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg type="int" value="7500000"/>
  <constructor-arg type="java.lang.String" value="42"/>
</bean>

  b.按参数索引传给构造方法注入(索引从0开始)[ Constructor Argument Index ]

 

 

<bean id="exampleBean" class="examples.ExampleBean">
  <constructor-arg index="0" value="7500000"/>
  <constructor-arg index="1" value="42"/>
</bean>

 

Some examples

 

<bean id="exampleBean" class="examples.ExampleBean">

  <!-- setter injection using the nested <ref/> element -->
  <property name="beanOne"><ref bean="anotherExampleBean"/></property>

  <!-- setter injection using the neater 'ref' attribute -->
  <property name="beanTwo" ref="yetAnotherBean"/>
  <property name="integerProperty" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

 

 

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public void setBeanOne(AnotherBean beanOne) {
        this.beanOne = beanOne;
    }

    public void setBeanTwo(YetAnotherBean beanTwo) {
        this.beanTwo = beanTwo;
    }

    public void setIntegerProperty(int i) {
        this.i = i;
    }    
}
 

 

<bean id="exampleBean" class="examples.ExampleBean">

  <!-- constructor injection using the nested <ref/> element -->
  <constructor-arg>
    <ref bean="anotherExampleBean"/>
  </constructor-arg>
  
  <!-- constructor injection using the neater 'ref' attribute -->
  <constructor-arg ref="yetAnotherBean"/>
  
  <constructor-arg type="int" value="1"/>
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
 

 

public class ExampleBean {

    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;
    
    public ExampleBean(
        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        this.beanOne = anotherBean;
        this.beanTwo = yetAnotherBean;
        this.i = i;
    }
}

 

 

<bean id="exampleBean" class="examples.ExampleBean"
      factory-method="createInstance">
  <constructor-arg ref="anotherExampleBean"/>
  <constructor-arg ref="yetAnotherBean"/>
  <constructor-arg value="1"/> 
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
 

 

public class ExampleBean {

    // a private constructor
    private ExampleBean(...) {
      ...
    }
    
    // a static factory method; the arguments to this method can be
    // considered the dependencies of the bean that is returned,
    // regardless of how those arguments are actually used.
    public static ExampleBean createInstance (
            AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {

        ExampleBean eb = new ExampleBean (...);
        // some other operations...
        return eb;
    }
}
 

 

 

3、接口注入(不常用)

网上说会增加耦合 不建议使用 我也没有用过 暂且先不谈 会了的话在更新^_^

 

 

 

分享到:
评论

相关推荐

    关于spring boot中几种注入方法的一些个人看法

    主要给大家介绍了关于spring boot中几种注入方法的一些个人看法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    spring学习:依赖注入的几种方式讨论

    NULL 博文链接:https://shmilyaw-hotmail-com.iteye.com/blog/2169569

    Spring三种注入方式(三)

    NULL 博文链接:https://gary0416.iteye.com/blog/890689

    Spring定义bean的三种方式和自动注入

    bean加入spring容器管理的方式,bean加入applicationcontext容器的方式

    spring依赖注入的几种方式

    spring依赖注入的几种方式

    详解Spring中bean的几种注入方式

    主要介绍了详解Spring中bean的几种注入方式,主要介绍了4种注入,主要有属性注入、构造函数注入、工厂方法注入,非常具有实用价值,需要的朋友可以参考下

    java巩固练习Spring 的bean注入方式有几种demo例子

    IOC注入bean有哪几种,写了几种Spring的bean注入方式demo练习例子,欢迎您观看和留言。免费共享浏览、下载。

    Spring 依赖注入的几种方式详解

    本篇文章主要介绍了Spring 依赖注入的几种方式详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring中属性注入的几种方式以及复杂属性的注入详解

    主要介绍了Spring中属性注入的几种方式以及复杂属性的注入详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Spring为IOC容器注入Bean的五种方式详解

    主要介绍了Spring为IOC容器注入Bean的五种方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Spring注入方式有哪些

    你知道Spring的注入方式有哪几种吗?这篇文章主要为大家详细介绍了Spring的注入方式,感兴趣的小伙伴们可以参考一下

    Spring Bean三种注入方式详解

    本篇文章主要介绍了Spring Bean三种注入方式详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring开发指南

    依赖注入的几种实现类型 Type1 接口注入 Type2 设值注入 Type3 构造子注入 几种依赖注入模式的对比总结 Spring Bean封装机制 Bean Wrapper Bean Factory ApplicationContext Web Context Spring 高级...

    25个经典的Spring面试问答

    Spring有几种配置方式 如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner ...

    Spring注入Date类型的三种方法总结

    主要介绍了Spring注入Date类型的三种方法总结的相关资料,希望通过本文能帮助到大家,让大家掌握这几种方法,需要的朋友可以参考下

    详解Spring通过@Value注解注入属性的几种方式

    本篇文章主要介绍了详解Spring通过@Value注解注入属性的几种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

    Spring依赖注入的三种方式实例详解

    Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的。 首先我们需要以下几个类: 接口 Logic.java 接口实现类 ...

    吴天雄--Spring笔记.doc

    IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性详解(包含单例设计模式),DI详解,Spring的几种注入方式,利用Spring简化Mybatis;第二天内容:AOP(AOP常用概念、Spring的三种aop实现方式、代理...

    java面试Spring.pdf

    1. Spring 介绍 1.1 Spring 的优点 ...说一下Spring基于xml注入bean的几种方式? Spring如何解决循环依赖问题? Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?

Global site tag (gtag.js) - Google Analytics