温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot依赖注入的三种方式是什么

发布时间:2023-04-18 10:52:12 来源:亿速云 阅读:117 作者:iii 栏目:开发技术

本文小编为大家详细介绍“SpringBoot依赖注入的三种方式是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot依赖注入的三种方式是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

SpringBoot依赖注入的三种方式

1.使用 XML 配置依赖注入

在 Spring Boot 中,使用 XML 配置依赖注入(DI)时,需要使用<bean>元素来定义 bean,并使用<property>元素来为 bean 的属性注入值或依赖对象。

以下是一个简单的示例:

  • 在src/main/resources目录下创建applicationContext.xml文件。

  • 在该文件中定义一个 testBean bean,并注入一个 String 类型的属性name和一个 UserService 类型的依赖对象。

<?xml version="1.0" encoding="UTF-8"?>
<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">

   <bean id="testBean" class="com.example.demo.TestBean">
       <property name="name" value="小明"/>
       <property name="userService" ref="userService"/>
   </bean>

   <bean id="userService" class="com.example.demo.UserService"/>

</beans>
  • 在 TestBean 类中声明一个 name 属性和一个 UserService 的依赖:

public class TestBean {
    private String name;
    private UserService userService;
    //setter和getter方法省略
}
  • 在启动类中调用 ApplicationContext 的构造函数并传入 applicationContext.xml 文件的路径,然后通过 getBean 方法获取 TestBean 实例,并访问它的属性和方法:

public class DemoApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestBean testBean = (TestBean)context.getBean("testBean");
        String name = testBean.getName();
        UserService userService = testBean.getUserService();
        //使用testBean和userService进行其他操作
    }
}

这样就完成了 Spring Boot 中使用 XML 配置依赖注入的过程。需要注意的是,在 Spring Boot 中,官方推荐使用 JavaConfig(基于 Java 类的配置方式)或注解(Annotation)来进行依赖注入,因为它们更加方便和易于维护。

2.使用 Java 配置类实现依赖注入

使用 Java Config 实现依赖注入可以通过@Configuration和@Bean注解来实现。

以下是一个简单的示例:

  • 创建一个配置类,并使用@Configuration注解标记,定义两个 Bean:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    @Bean
    public TestBean testBean() {
        return new TestBean("小明");
    }

    @Bean
    public UserService userService() {
        return new UserServiceImpl();
    }
}
  • 定义 TestBean 类,并在该类中声明一个 name 属性:

public class TestBean {
    private String name;

    public TestBean(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  • 定义 UserService 接口和它的实现类 UserServiceImpl:

public interface UserService {
    void addUser();
}

public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("Add user success");
    }
}
  • 在启动类中使用 AnnotationConfigApplicationContext 获取配置类对象,然后使用 getBean() 方法获取 TestBean 和 UserService 实例:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class DemoApplication {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        TestBean testBean = context.getBean(TestBean.class);
        String name = testBean.getName();
        UserService userService = context.getBean(UserService.class);
        userService.addUser();
    }
}

这样就完成了使用 JavaConfig 实现依赖注入的过程。需要注意的是,JavaConfig 等价于 XML 配置文件,但是 JavaConfig 更加的面向对象,更加灵活,更加易于维护。

3.使用注解来进行依赖注入

可以使用注解来进行依赖注入,常用的注解有@Autowired和@Qualifier。

以下是一个简单的示例:

  • 定义一个 Service 类,使用@Autowired注解注入TestBean:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Service {
    @Autowired
    private TestBean testBean;

    public String getName() {
        return testBean.getName();
    }
}
  • 定义 TestBean 类,测试注入是否成功:

import org.springframework.stereotype.Component;

@Component
public class TestBean {
    private String name = "小明";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  • 在启动类中使用 AnnotationConfigApplicationContext 获取配置类对象,然后使用 getBean() 方法获取 Service 实例:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class DemoApplication {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        Service service = context.getBean(Service.class);
        System.out.println(service.getName());
    }
}

运行启动类,可以看到控制台输出:

小明

这样就完成了使用注解进行依赖注入的过程。需要注意的是,使用注解可以使代码更加简洁、易于阅读和维护,但是需要注意注解的使用和作用范围。

读到这里,这篇“SpringBoot依赖注入的三种方式是什么”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI