1、Spring依赖注入-DI
《Java EE开发的颠覆者 Spring Boot实战》笔记
一、笔记
1、控制反转IOC和依赖注入DI
IOC:Inversion Of Control
DI:Dependency Injection
依赖注入实现控制反转
依赖注入:容器负责创建个维护对象间的依赖关系,而不是通过对象本身负责自己创建和解决依赖关系
依赖注入目的:解耦,组合理念
2、Bean的创建和注入方式
xml、注解、Java配置、groovy配置
3、声明Bean的注解
@Component:组件,没有明确的角色
@Service:在业务逻辑层使用
@Repository:在数据访问层使用
@Controller:在展现层使用
上面4个是等效的
4、注入Bean的注解
@Autowired:Spring注解
@Resource:
@Inject
上面3个等效
1 @Service 2 public class UseFunServiceImpl implements UseFunService { 3 @Autowired 4 private FunService funService; 5 6 @Override 7 public String printString(String str) { 8 return funService.print(str); 9 } 10 11 }
@Autowired含义:将FunService的实体Bean注入到UseFunServiceImpl中,使UseFunServiceImpl具备FunService的功能
此处,就是一个简单的“组合”思想
5、配置类
@Configuration:声明当前类是一个配置类,可以作为Spring容器的启动参数
@ComponentScan:组件(Bean)扫描,自动扫描包名下所有使用@Component、@Service、@Repository、@Controller的类,并注册为Bean
6、运行
1 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 2 3 import com.ning.config.Config; 4 import com.ning.service.UseFunService; 5 import com.ning.service.impl.FunService; 6 7 public class TestApplication { 8 public static void main(String[] args) { 9 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( 10 Config.class); 11 UseFunService useFunService = context.getBean(UseFunService.class); 12 FunService funService = context.getBean(FunService.class); 13 System.out.println(funService.print("funService")); 14 System.out.println(useFunService.printString("hello word")); 15 context.close(); 16 } 17 }
AnnotationConfigApplicationContext作为Spring容器,配置类作为入参
context.getBean():获得Bean,并执行相应的代码
二、实例
1、代码目录结构
DiConfig.java
1 package com.ning.ch1.di.config; 2 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.Configuration; 5 6 @Configuration 7 @ComponentScan("com.ning.ch1.*") 8 public class DiConfig { 9 10 }
FunService.java
1 package com.ning.ch1.di.service; 2 3 import org.springframework.stereotype.Service; 4 5 @Service 6 public class FunService { 7 public String printInputString(String str) { 8 return "FunService print : " + str; 9 } 10 }
UseFunService.java
1 package com.ning.ch1.di.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 6 @Service 7 public class UseFunService { 8 @Autowired 9 private FunService funService; 10 11 public String printInputString(String str) { 12 return funService.printInputString(str); 13 } 14 }
DiApplication.java
1 package com.ning.ch1.di; 2 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 5 import com.ning.ch1.di.config.DiConfig; 6 import com.ning.ch1.di.service.UseFunService; 7 8 public class DiApplication { 9 10 public static void main(String[] args) { 11 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( 12 DiConfig.class); 13 14 UseFunService useFunService = context.getBean(UseFunService.class); 15 String outPut = useFunService.printInputString("hello word"); 16 System.out.println(outPut); 17 context.close(); 18 } 19 20 }
运行结果:
本例pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.ning</groupId> 4 <artifactId>spring4xx</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 7 <properties> 8 <java.version>1.8</java.version> 9 </properties> 10 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework</groupId> 14 <artifactId>spring-context</artifactId> 15 <version>4.1.6.RELEASE</version> 16 </dependency> 17 </dependencies> 18 19 <build> 20 <plugins> 21 <plugin> 22 <groupId>org.apache.maven.plugins</groupId> 23 <artifactId>maven-compiler-plugin</artifactId> 24 <version>2.3.2</version> 25 <configuration> 26 <source>${java.version}</source> 27 <target>%{java.version}</target> 28 </configuration> 29 </plugin> 30 </plugins> 31 </build> 32 </project>