验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

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

阅读:307 来源:乙速云 作者:代码code

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

SpringBoot依赖注入的三种方式

1.使用 XML 配置依赖注入

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

以下是一个简单的示例:

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

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




   
       
       
   

   

  • 在 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());
    }
}

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

小明

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

分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>