美文网首页
IoC注入的方式

IoC注入的方式

作者: 摸摸脸上的胡渣 | 来源:发表于2020-02-11 15:58 被阅读0次
  1. Setter注入
    对应的field必须有setter方法,在xml中配置或在@Configuration修饰的类中,书写相应的@Bean方法
public class AccountServiceImpl implements AccountService {

    /**
     * 需要注入的对象
     */
    private AccountDao accountDao;

    /**
     * 对应的setter方法
     */
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
 }
xml注入方式
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        ">
    <!-- 声明accountDao对象,交给spring创建 -->
    <bean name="accountDao" class="com.zejian.spring.springIoc.dao.impl.AccountDaoImpl"/>
    <!-- 声明accountService对象,交给spring创建 -->
    <bean name="accountService" class="com.zejian.spring.springIoc.service.impl.AccountServiceImpl">
          <!-- 注入accountDao对象,需要set方法-->
          <property name="accountDao" ref="accountDao"/>
    </bean>
</beans>
@Configuration注入方式

package com.zejian.spring.springIoc.conf;

import com.zejian.spring.springIoc.dao.AccountDao;
import com.zejian.spring.springIoc.dao.impl.AccountDaoImpl;
import com.zejian.spring.springIoc.service.AccountService;
import com.zejian.spring.springIoc.service.impl.AccountServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfiguration {

    @Bean
    public AccountDao accountDao(){
        return new AccountDaoImpl();
    }

    @Bean
    public AccountService accountService(){
        AccountServiceImpl bean=new AccountServiceImpl();
        //注入dao
        bean.setAccountDao(accountDao());
        return bean;
    }
}
  1. 构造方法注入
    首先需要为被注入的field,书写对应的构造方法,比如
public class AccountServiceImpl implements AccountService{
    /**
     * 需要注入的对象Dao层对象
     */
    private AccountDao accountDao;

    /**
     * 构造注入
     * @param accountDao
     */
    public AccountServiceImpl(AccountDao accountDao){
        this.accountDao=accountDao;
    }
}

其次要在xml配置文件中,使用<constructor-arg>标签,完成对被注入field的注入。

<bean name="accountDao" class="com.zejian.spring.springIoc.dao.impl.AccountDaoImpl"/>
<!-- 通过构造注入依赖 -->
<bean name="accountService" class="com.zejian.spring.springIoc.service.impl.AccountServiceImpl">
    <!-- 构造方法方式注入accountDao对象,-->
    <constructor-arg  ref="accountDao"/>
</bean>

但是要注意,使用构造方法进行注入时,有可能会出现无法解决的循环依赖,所以还是建议使用基于注解的自动注入。

  1. 自动装配
    3.1 基于xml自动装配
    不同于以上两种的xml配置,xml的自动装配,不需要使用<property>或者<constructor-arg>标签,而是需要使用"autowire"属性,属性值有
    "byType"——通过类型匹配注入,当存在相同类型多个实例时,会注入失败,可通过使用"autowire-candidate="false"属性,来对不想注入的bean进行屏蔽";
    "byName"——通过变量名注入,如果没有发现变量名一致的bean,则不会注入;
    "constructor"——,先按照类型再按照名字进行bean的匹配;
    详细可参考关于Spring IOC (DI-依赖注入)你需要知道的一切
    3.2 基于注解自动装配
    使用的注解有两个@Autowire和@Resource
    @Autowire是通过byType进行bean的匹配,也可以在同类型多实例的情况下,搭配使用@Qualifier通过byName进行匹配;
    @Resource是通过byName进行bean的匹配

以上两种自动装配的方式,只能对引用类型变量进行装配,无法对基本类型进行装配,若要对基本类型进行装配,则要使用@Value。

3.3 对基本类型的自动装配
@Value和.properties文件搭配使用,可以对基本类型变量进行装配。

相关文章

  • Spring笔记

    IOC/DI 1.Xml实现 bean 注入方式 (set注入、构造注入)bean 作用域 (singleton ...

  • IoC注入的方式

    Setter注入对应的field必须有setter方法,在xml中配置或在@Configuration修饰的类中,...

  • Spring IOC容器

    由于Spring是采用依赖注入(DI)的方式来实现IOC,所以本文将IOC和依赖注入(DI)等同看待,主要讨论依赖...

  • Spring入门(一)

    一,XML注入的两种方式 1,设值注入 2,构造注入 二,IOC 三,单元测试

  • Spring 注入方式

    Spring 通过 DI(依赖注入)实现 IOC(控制反转),常用的注入方式主要有三种:构造方法注入,setter...

  • IOC 控制反转

    以下内容只是我个人理解 依赖注入和ioc 是什么关系? 依赖注入是控制反转(ioc)的一种实现方式 如何理解控制反...

  • 关于laravel依赖注入

    关于laravel依赖注入和IoC容器的个人理解: 关于它的实现方式 我们要的最后结果就是:注入,只要注入就OK ...

  • spring的ioc和aop的原理

    IOC:依赖注入(控制反转),两种注入方式:依赖注入和设置注入,通过容器动态地将某种依赖注入到组件中,通过Spri...

  • Java面试题之Spring

    1.依赖注入的方式有哪几种?以及这些方法如何使用? Set注入 构造器注入 接口注入 2.什么是IoC和DI? 依...

  • Dagger2常用注解诠释

    依赖注入 控制反转(IoC)与依赖注入(DI)浅谈依赖注入理解依赖注入(IOC)和学习Unity Gradle配置...

网友评论

      本文标题:IoC注入的方式

      本文链接:https://www.haomeiwen.com/subject/rndxfhtx.html