Spring基礎知識之依賴註入
Spring框架的四大原則:
1)使用POJO進行輕量級和最小侵入式的開發。
2)通過依賴註入和基於接口編程實現松耦合。
3)通過AOP和默認習慣進行聲明式編程。
4)使用AOP和模板(template)減少模式化代碼。
1.1依賴註入:
控制反轉(Inversion of Control - IOC)與依賴註入(dependency injection - DI)在Spring環境下是同等的概念。
控制反轉是通過依賴註入實現的。所謂的控制反轉指的是:容器負責創建對象和維護對象間的依賴關系。
依賴註入的主要目的是實現解耦,添加一個功能一般有兩種方式:繼承父類、組合另一個具有該功能的類。
組合相對於繼承父類來說,其耦合度是較低的。
SpringIoC容器(ApplicationContext)負責創建Bean,並通過容器將功能類Bean註入到需要的Bean中去。Spring通過XML、註解、java配置實現IoC(DI)。
XML、註解、java配置都是配置元數據。
元數據:用來描述數據的數據。
元數據不具備任何可執行能力,只能通過外界代碼來對元數據進行解析然後賦予特定的功能。Spring容器解析這些配置元數據後進行Bean的初始化、配置和管理依賴。
聲明Bean的註解:
@Component:沒有明確的角色
@Service: 在業務邏輯層使用
@Repository:在數據訪問層(dao)使用
@Controller:在MVC層使用
註:上面四個註解功能是一樣的,不過為了業務區分采用了不同的表現。
註入Bean的註解:
@Autowired :Spring提供的註解
@Inject :JSR-330提供的註解
@Resource :JSR-330提供的註解
上面三個註解一般可用於set方法或者屬性上,但是一般習慣寫法是用在屬性上。
基於註解的Bean的初始化與依賴註入,Spring容器選用AnnotationConfigApplicationContext。
功能類Bean:
@Service
public class FunService {
public String sayHello(String word) {
return "Hello " + word + " !";
}
}
註:@Service聲明當前FunService類是一個Spring管理的Bean,其中使用@Component、@Service、@Repository、@Controller是等效的。
使用功能類的Bean:
@Service
public class UseFunService {
@Autowired
private FunService FunService;
public String sayHello(String word) {
return FunService.sayHello(word);
}
}
註:@Service聲明當前類是Spring管理的Bean
@Autowired將FunService的實體註入到UseFunService中,使其能夠使用其中的方法。
配置類:
@ComponentScan("learn.learn.spring.ioc")
@Configuration
public class Diconfig {
}
註:@ComponentScan,自動掃描指定包下的所有使用@Component、@Controller、@Service、@Repository的類,並註冊為Bean。
運行:
public class App
{
public static void main( String[] args ) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Diconfig.class);//1
UseFunService UseFunService =
context.getBean(UseFunService.class);//2
System.out.println(UseFunService.sayHello("di"));
context.close();
}
}
註:1)使用AnnotationConfigApplicationContext作為Spring的容器,接受一個配置類作為參數。
2)獲取聲明配置的UseFunService的Bean。
結果:
1.2java配置:
Java配置是Spring4.x推薦使用的配置方式,可以完全取代XML配置;java配置也是springboot中推薦使用的配置方式。
java配置是通過@Configuration和@Bean實現的。
@configuration聲明當前類是一個配置類,相當於Spring配置的XML文件。
@Bean註解在方法上,聲明當前方法返回值為一個Bean。
註:實際工作中java配置的方式往往與XML實現的方式一樣,主要是用於數據庫的配置等一些常用的數據配置,當然你也可以根據業務需要來做自己需要的配置方式。
一般配置原則:全局配置使用java配置(如數據庫,MVC等配置);業務Bean使用註解配置(@Component、@Controller、@Service、@Repository)。
示例:
功能類的Bean:
public class FunctionService {
public String say(String word) {
return "Hello" + word + " !";
}
}
註:此處沒有使用@Service聲明Bean
使用功能類Bean:
//1
public class UseFunctionService {
//2
FunService FunService;
public void set(FunService FunService) {
this.FunService = FunService;
}
public String say(String word) {
return FunService.sayHello(word);
}
}
註:1:此處沒有使用@Service聲明Bean
2:此處沒有使用@Autowired註解註入Bean
配置類:
@Configuration//1
public class DiConfig {
@Bean
public FunctionService functionService() {
return new FunctionService();
}
@Bean
public UseFunctionService useFunctionService() {
return new UseFunctionService();
}
}
註:1:@Configuration表明此類是一個Spring配置類,類似於XML文件。
運行:
public class App {
public static void main( String[] args ) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(DiConfig.class);//1
UseFunctionService useFunctionService =
context.getBean(UseFunctionService.class);
System.out.println(useFunctionService.say("di"));
context.close();
}
}
結果:
Spring基礎知識之依賴註入