在Spring中依賴注入(dependency injection,即DI)是指容器負責創建對象和維護對象之間的依賴關係,而不是通過對象本身實現自己的創建和解決自己的依賴。在Spring中有自動配置、Java配置和XML配置,其中Java配置更爲靈活,在Spring Boot中主要採用自動配置,在某些情況下采用java配置。下面來看一下自動配置和Java配置的代碼實現,詳細代碼如下:
一、自動配置
package com.example.demo.test.DI;
import org.springframework.stereotype.Service;
@Service
public class FunctionService {
public String sayHello(String words){
return "hello" + words + "!";
}
}
package com.example.demo.test.DI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UseFunctionService {
@Autowired
FunctionService functionService;
public String sayHello(String words){
return functionService.sayHello(words);
}
}
package com.example.demo.test.DI;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example.demo.test.DI")
public class DiConfig {
}
自動配置測試
import com.example.demo.test.DI.DiConfig;
import com.example.demo.test.DI.UseFunctionService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
System.out.println(useFunctionService.sayHello("world"));
context.close();
}
}
自動配置類也可不寫,在啓動類加入@ComponentScan("com.example.demo.test.DI")和@SpringBootApplication註解即可
二、java代碼配置
通過@Configuration和@Bean實現,其中@Configuration聲明當前類是一個配置類,相當於Spring配置的xml文件;@Bean註解在方法上,表明該方法返回一個Bean。
package com.example.demo.test.javaConfig;
public class FunctionService {
public String sayHello(String words){
return "hello " + words + "!";
}
}
FunctionService類注入
package com.example.demo.test.javaConfig;
public class UseFunctionService {
FunctionService service;
public UseFunctionService(FunctionService functionService){
service = functionService;
}
public String sayHello(String words){
return service.sayHello(words);
}
}
配置類
package com.example.demo.test.javaConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
//指定掃描範圍
@ComponentScan("com.example.demo.test.javaConfig")
public class JavaConfig {
@Bean
public FunctionService functionService(){
return new FunctionService();
}
@Bean
public UseFunctionService useFunctionService(FunctionService functionService){
return new UseFunctionService(functionService);
}
// 也可以是下面寫法
// @Bean
// public UseFunctionService useFunctionService(){
// return new UseFunctionService(functionService());
// }
}
java配置測試
package com.example.demo;
import com.example.demo.test.javaConfig.JavaConfig;
import com.example.demo.test.javaConfig.UseFunctionService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
System.out.println(useFunctionService.sayHello("world"));
context.close();
}
}
以上兩種方式的輸出結果都是Hello world! 這兩種方式也是最爲常用配置方式。