欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring 三种依赖注入方式详解

程序员文章站 2022-05-24 23:44:33
...
-- pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>
-- web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>
-- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:component-scan base-package="cywen.demo.fw" />
    <mvc:annotation-driven />
</beans>
  • field通过@Autowired注入,没有循环依赖的问题
@Component
public class IndexService1 {
    @Autowired
    private IndexService2 indexService2;
}

@Component
public class IndexService2 {
    @Autowired
    private IndexService1 indexService1;
}
  • setter通过@Autowired注入,没有循环依赖的问题
@Component
public class IndexService1 {
    private IndexService2 indexService2;
    @Autowired
    public void setIndexService2(IndexService2 indexService2) {
        this.indexService2 = indexService2;
    }
}

@Component
public class IndexService2 {
    private IndexService1 indexService1;
    @Autowired
    public void setIndexService1(IndexService1 indexService1){
        this.indexService1 = indexService1;
    }
}

– constructor注入依赖,下例中项目启动失败,抛出循环依赖的异常

@Component
public class IndexService1 {
    private IndexService2 indexService2;
    @Autowired
    public IndexService1(IndexService2 indexService2) {
        this.indexService2 = indexService2;
    }
}

@Component
public class IndexService2 {
    private IndexService1 indexService1;
    @Autowired
    public IndexService2(IndexService1 indexService1) {
        this.indexService1 = indexService1;
    }
}
  • 总结

    如果使用构造器注入,在spring项目启动的时候,就会抛出:BeanCurrentlyInCreationException:Requested bean is currently in creation: Is there an unresolvable circular reference?从而提醒你避免循环依赖

    所以,综合考虑,还是属性上添加@Autowired更好一些,主要原因是代码简洁