开发者

SpringBoot依赖注入的三种方式

开发者 https://www.devze.com 2023-04-18 10:48 出处:网络 作者: 左林右李02
目录SpringBoot依赖注入的三种方式js1.使用 XML 配置依赖注入2.使用 Java 配置类实现依赖注入3.使用注解来进行依赖注入SpringBoot依赖注入的三种方式
目录
  • SpringBoot依赖注入的三种方式js
    • 1.使用 XML 配置依赖注入
    • 2.使用 Java 配置类实现依赖注入
    • 3.使用注解来进行依赖注入

SpringBoot依赖注入的三种方式

1.使用 XML 配置依赖注入

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

以下是一个简单的示例:

  • 在src/main/resources目录下创建applicationContext.xml文件。
  • 在该文www.devze.com件中定义一个 testBean bean,并注入一个 String 类型的属性name和一个 UserService 类型的依赖对象。
&l开发者_开发入门t;?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 {
   php 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");
    }
}
  • 在启动类中使用 Annotatio编程客栈nConfigApplicationContext 获取配置类对象,然后使用 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() {
    http://www.devze.com    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依赖注入的三种方式的详细内容,更多关于Java SpringBoot依赖注入的资料请关注我们其它相关文章!

0

精彩评论

暂无评论...
验证码 换一张
取 消