欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot依賴注入的三種方式

 更新時(shí)間:2023年04月18日 09:29:16   作者:左林右李02  
本文將通過代碼示例詳細(xì)介紹SpringBoot依賴注入的三種方式,對(duì)學(xué)習(xí)依賴注入有一定的參考價(jià)值,需要的朋友可以參考一下

SpringBoot依賴注入的三種方式

1.使用 XML 配置依賴注入

在 Spring Boot 中,使用 XML 配置依賴注入(DI)時(shí),需要使用<bean>元素來定義 bean,并使用<property>元素來為 bean 的屬性注入值或依賴對(duì)象。

以下是一個(gè)簡(jiǎn)單的示例:

  • 在src/main/resources目錄下創(chuàng)建applicationContext.xml文件。
  • 在該文件中定義一個(gè) testBean bean,并注入一個(gè) String 類型的屬性name和一個(gè) UserService 類型的依賴對(duì)象。
<?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 類中聲明一個(gè) name 屬性和一個(gè) UserService 的依賴:
public class TestBean {
    private String name;
    private UserService userService;
    //setter和getter方法省略
}
  • 在啟動(dòng)類中調(diào)用 ApplicationContext 的構(gòu)造函數(shù)并傳入 applicationContext.xml 文件的路徑,然后通過 getBean 方法獲取 TestBean 實(shí)例,并訪問它的屬性和方法:
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進(jìn)行其他操作
    }
}

這樣就完成了 Spring Boot 中使用 XML 配置依賴注入的過程。需要注意的是,在 Spring Boot 中,官方推薦使用 JavaConfig(基于 Java 類的配置方式)或注解(Annotation)來進(jìn)行依賴注入,因?yàn)樗鼈兏臃奖愫鸵子诰S護(hù)。

2.使用 Java 配置類實(shí)現(xiàn)依賴注入

使用 Java Config 實(shí)現(xiàn)依賴注入可以通過@Configuration和@Bean注解來實(shí)現(xiàn)。

以下是一個(gè)簡(jiǎn)單的示例:

  • 創(chuàng)建一個(gè)配置類,并使用@Configuration注解標(biāo)記,定義兩個(gè) 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 類,并在該類中聲明一個(gè) 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 接口和它的實(shí)現(xiàn)類 UserServiceImpl:
public interface UserService {
    void addUser();
}

public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("Add user success");
    }
}
  • 在啟動(dòng)類中使用 AnnotationConfigApplicationContext 獲取配置類對(duì)象,然后使用 getBean() 方法獲取 TestBean 和 UserService 實(shí)例:
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 實(shí)現(xiàn)依賴注入的過程。需要注意的是,JavaConfig 等價(jià)于 XML 配置文件,但是 JavaConfig 更加的面向?qū)ο?,更加靈活,更加易于維護(hù)。

3.使用注解來進(jìn)行依賴注入

可以使用注解來進(jìn)行依賴注入,常用的注解有@Autowired和@Qualifier。

以下是一個(gè)簡(jiǎn)單的示例:

  • 定義一個(gè) 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 類,測(cè)試注入是否成功:
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;
    }
}
  • 在啟動(dòng)類中使用 AnnotationConfigApplicationContext 獲取配置類對(duì)象,然后使用 getBean() 方法獲取 Service 實(shí)例:
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());
    }
}

運(yùn)行啟動(dòng)類,可以看到控制臺(tái)輸出:

小明

這樣就完成了使用注解進(jìn)行依賴注入的過程。需要注意的是,使用注解可以使代碼更加簡(jiǎn)潔、易于閱讀和維護(hù),但是需要注意注解的使用和作用范圍。

以上就是SpringBoot依賴注入的三種方式的詳細(xì)內(nèi)容,更多關(guān)于Java SpringBoot依賴注入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論