01-依赖注入DI

231 阅读2分钟

Spring-01-依赖注入DI

一、介绍

依赖注入:Spring容器负责创建和维护对象之间的依赖关系。

其过程指的是:SpringIoC容器会创建一个Bean,并通过该容器将功能类Bean注入到你所需要的Bean中。

二、需求

演示基于注解方式Bean的初始化和依赖注入。

三、演示

1、新建一个Maven项目

右键maven选择Update Maven Project选择OK。

右键Properties,修改Java Build Path,将JDK选择(我的是1.8)、Java Compiler选择1.8、Project Facets选择1.8.总之要统一。

<!-- 定义变量 -->
	<properties>
		<java.version>1.7</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2、创建功能类Bean

package com.eleven.di;

import org.springframework.stereotype.Service;

@Service // @Service注解声明当前ElevenService类是Spring管理的一个Bean
public class ElevenService {

	public String sayLoveMe(String eleven) {
		return "Love " + eleven + " !";
	}

}

3、使用功能类Bean

package com.eleven.di;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service // @Service该注解声明当前UseFunctionService类是Spring管理的一个Bean
public class UseFunctionService {
	@Autowired // @Autowired该注解表示:将ElevenService注入到UseFunctionService类中
	private ElevenService elevenService;

	public String SayLoveEleven(String eleven) {
		return elevenService.sayLoveMe(eleven);
	}

}

4、配置类

package com.eleven.di;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;

@Configurable // 该注解声明当前类是一个配置类
@ComponentScan("com.eleven") // 自动扫描包下的所有注解,并注册为Bean
public class DiConfig {

}

5、运行输出

package com.eleven.di;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		// 使用AnnotationConfigApplicationContext作为Spring的容器,接收输入一个DiConfig配置类作为参数
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
		// 拿到声明为Bean的ElevenService类
		ElevenService elevenService = context.getBean(ElevenService.class);
		// 传入参数"伊莱文",并打印输出
		System.out.println(elevenService.sayLoveMe("伊莱文"));
		// 关闭容器的连接
		context.close();
	}

}

四、基于Java配置的方式(推荐)

1、创建一个功能类Bean

package com.eleven.javaconfig;

// 此处没有写注解@Service声明Bean
public class ElevenService {

	public String SayLoveMe(String eleven) {
		return "Love " + eleven + " !";
	}

}

2、使用功能类Bean

package com.eleven.javaconfig;

// 此处依旧没有使用注解@Service声明Bean
public class UseElevenService {
	// 此处没有使用注解@Autowired注入Bean
	ElevenService elevenService;

	public void setEleveService(ElevenService elevenService) {
		this.elevenService = elevenService;
	}

	public String SayLoveMe(String eleven) {
		return elevenService.SayLoveMe(eleven);
	}
}

3、配置类

package com.eleven.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration // 声明当前类是一个配置类
public class JavaConfig {
	@Bean // 使用该注解声明当前类ElevenService的返回值类型是一个Bean,Bean的名称就是方法名
	public ElevenService elevenService() {
		return new ElevenService();
	}

	@Bean
	public UseElevenService useElevenService() {
		UseElevenService useElevenService = new UseElevenService(); //
		useElevenService.setEleveService(elevenService()); // 注入UseElevenService的时候,直接调用UseElevenService方法
		return useElevenService;
	}

}

4、运行输出

package com.eleven.javaconfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
		UseElevenService useElevenService = context.getBean(UseElevenService.class);
		System.out.println(useElevenService.SayLoveMe("伊莱文"));
		context.close();
	}

}