SpringBean装配------自动装配

Spring配置的可选方案
1. 隐式的bean发现机制和自动配置
2. 在Java中进行显式配置
3. 在XML中进行显式配置

自动化装配bean
spring从两个角度来实现自动化装配

  1. 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean
  2. 自动装配(autowiring):Spring自动满足bean之间的依赖
public interface UserService{
	void goHome();
}

@Component
public class UserServiceImpl implements UserService{
	public void goHome(){
		System.out.println("");
	}
}

@Component 注解表明该类作为组件类,并告知Spring要为这个类创建bean。没有必要显式配置 UserServiceImpl bean,因为这个类使用了@Component 注解,所以spring会为你把事情处理妥当。
不过,组件扫描默认是不开启的。我们还需要显式配置一下Spring,从而命令它去寻找带有@Component注解的类,并为其创建bean。

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

@Configuration
@ComponentScan
public class UserConfig{
}

@ComponentScan 默认会扫描与配置类相同的包,通过扫描这个包以及这个包下面的所有的子包,查找带有@Component注解的类,并执行操作。
如果更倾向于XML来启用组件扫描的话,那么可以使用Spring context 命名空间的 < context:component-scan > 元素

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="" />
</beans>

为组建扫描的bean命名
Spring应用上下文所有的bean都会给定一个ID,上面的例子虽然没有指定,但spring会根据类名指定(将首字母变为小写),如果我们想的为一个bean设置不同的ID,那么我们可以将代码改为

@Component("driver")
public class UserServiceImpl implements UserService{
 '''
}

好吧,其实我们也可以不用@Component注解,而是用Java依赖注入规范中提供的@Named注解为bean设置ID

import javax.inject.Named;
@Named("driver")
public class UserServiceImpl implements UserService{
 '''
}

设置组件扫描的基础包
目前为止我们对@ComponentScan 设置任何属性,这意味着,按照默认规则,会以配置类所在的包作为基础类(base package)来扫描组件。

@Configuration
@ComponentScan("basePackage")
public class UserConfig{
}

小扩展
@ComponentScan() 可以设置value或basePackages属性吗,两个都是数组

	@AliasFor("basePackages")
	String[] value() default {};
	
	@AliasFor("value")
	String[] basePackages() default {};
	//安全的注入需要bean的Class
	Class<?>[] basePackageClasses() default {};

通过为bean添加注解实现自动装配
自动装配就是让Spring自动满足bean依赖的一种方法,在满足依赖的过程中,会在应用上下文中寻找匹配某个需求的其他bean,我们可以利用Spring的@Autowired注解完成。

@Component
public class EduFactory{

	private UserService userService;
	@Autowired
	public EduFactory (UserService  userService){
	this.userService = userService;
	}
	'''
}

在Spring初始化bean之后它会尽可能满足bean依赖,@Autowired可以用在类方法上,假设我们没把该注解在Setter方法上,而放在insertUs方法上,那么该注解发挥的作用还是完全一样的。

@Autowired
public void insertUs(UserService  userService){
	this.userService = userService;
	}

不管是构造器还是Setter方法,Spring都会尝试满足方法参数上所声明的依赖, 如果有就封装起来,未发现则会跑出异常,通过**@Autowired(required = false)**注解可以防止报错,但是在代码中要对其判断处理,以防止出现NullPointerExcepton异常。 (预留匹配多个的小坑)
@Autowired是Spring特有注解,我们还可以使用@Inject方法来进行装配

import javax.inject.Inject;
import javax.inject.Named;

@Named("driver")
public class EduFactory UserService{
@Inject
public UserServiceImpl (UserService userService){
	this.userService = userService;
}
 '''
}

你可能感兴趣的:(Spring)