你真正了解Spring自动装配吗

XML配置里的Bean自动装配

一、Spring IOC容器自动装配Bean

< bean>的autowire属性里自动自动装配的模式:

  • byType(根据类型自动装配)若IOC容器中有多个与目标Bean类型一致的Bean在这种情况下Spring将无法判定哪个Bean最合适该属性,所以不能执行自动装配
  • byName(根据名称自动装配)必须将目标Bean的名称和属性名设置完全相同根据bean的名字和当前bean和setter风格的属性名进行自动装配
  • constructor(通过构造器自动装配)当Bean中存在多个构造器时,此种自动装配方式将会很复杂,不推荐

二、使用bean的scope 属性来配置bean的作用域

  • singleton:默认值:容器初始化时创建bean实例,在整个容器的生命周期内只创建一个bean单例的prototype原型的容器初始化不创建bean的实例,而在每次请求时都创建一个新的bean实例,并返回

三、注解注入

  • 组件扫描(classpath中):
    spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件包括:
    @Component:基本注解,标识了一个手Spring管理的组件
    @Responsitory:标识持久层组件
    @Service:标识服务层(业务层)组件
    @Controller:标识表现层组件
    对于扫描的组件:Spring有默认的命名策略使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称
  • 添加扫描器 Spring配置文件 声明 < context:component-scan>
  • 其中:base-package 属性指定一个扫描的基类包 (Spring容器将会扫描这个基类包下的所有被注解的类)注:扫描多个包可以用逗号隔开
  • 需要扫描特定类可使用resource-pattern属性过滤特定的类
    < context:component-scan base-package=“com.XXX.XXX…”
    resource-pattern=“autowrie/*.class”/>
< context:include-filter> 子节点表示要包含的目标类
< context:exclude-filter> 子节点表示要排除在外的目标类
< context:compontent-scan> 可以拥有若干个< context:include-filter>和< context:exclude-filter>子节点

四、使用@Autowired自动装配Bean

  1. @Autowired注解自动装配具有兼容类型的单个Bean属性构造器,普通字段(即使是非public)一切具有参数的方法都可以应用@Autowired注解
  2. 默认情况下,所以使用@Autowired注解的属性都需要被设置,当Spring找不到匹配装配属性是,会抛出异常,若某个属性不允许设置,可以设置@Autowired注解的require属性为false
  3. 当IOC容器里存在多个类型兼容的Bean时,通过类型的自动装配将无法工作,此时可以在@Qualifier注解里提供Bean的名称,Spring允许对方法的入参标注,
    @Qualifiter已指定注入Bean的名称
    @Qualifier(“UserImpl”)
    @Authwired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配
    @Authwired注解也可以应用在集合属性上,此时Spring读取集合的类型信息,然后自动装配所有与之兼容的Bean
    @Authwired注解用在Java.util.Map上时,若该Map的键值为String,那么Spring将自动装配与之Map值类型兼容的Bean,此时Bean的名称作为键值
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("method")
public class Method {
    
    
	@Autowired
	Teacher t;
	private int id = 1;
	private String name = "zs";

	public void setT(Teacher t) {
    
    
		this.t = t;
	}

	
	public void show() {
    
    
		t.setId(id);
		t.setName(name);
		t.show();
	}

	public void add() {
    
    
		System.out.println("添加");
	}

}

XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan
		base-package="com.qst.Test"></context:component-scan>
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		ApplicationContext con = new ClassPathXmlApplicationContext("Applicaiton.xml");
		Method m = (Method) con.getBean("method");

		m.show();
		

	}
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44763595/article/details/107976822
今日推荐