Spring基于XML配置方式注入bean对象和@Resource注解的使用

基于SpringBoot框架企业级应用系统开发全面实战(01.08_xml文件配置测试_recv.mp4)->{Demo1-11}

1、主题:
      使用注解进行bean定义和依赖的内容;
      第一步:
        传统如何配置Bean并且完成Bean直接的依赖关系的建立。
        建立三个类,Office ,Car,Boss 需要在Spring容器中配置为Bean;

Spring的IOC容器注入bean对象

一、基于XML配置bean方式

创建三个Java实体类用于测试

com.test.springoffice

Java实体类的set方法是SpringIOC注入,在使用XML配置bean时所必需。

Boss

package com.test.sprintoffice;

public class Boss {
	private Car car;
	private Office office;
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public Office getOffice() {
		return office;
	}
	public void setOffice(Office office) {
		this.office = office;
	}
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}

}

Car


public class Car {
	private String brand;
	private double price;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	public String toString(){
		return "brand:"+brand+","+"price"+price;
	}
	

}

Office

package com.test.sprintoffice;

public class Office {
	private String officeNo="001";

	public String getOfficeNo() {
		return officeNo;
	}

	public void setOfficeNo(String officeNo) {
		this.officeNo = officeNo;
	}
	public String toString(){
		return "officeNo:"+officeNo;
	}
	

}

bean.xml( applicationContext.xml )

元素 bean

标签 property

属性 name ref/value

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


  <!-- boss类 -->
  <bean id="boss" class="com.test.sprintoffice.Boss">
     <property name="car" ref="car"/>
     <property name="office" ref="office"/>
  </bean>
  
  <!-- Office类 -->
  <bean id="office" class="com.test.sprintoffice.Office">
     <property name="officeNo" value="002"/>
  </bean>
  <!-- car类 -->
  <bean id="car" class="com.test.sprintoffice.Car">
     <property name="brand" value="长安"/>
     <property name="price" value="2000"/>
  </bean>
</beans>

测试类  AnnoToCTest

/**
 * 以传统方式完成bean注入;
 * @author Administrator
 *
 */

public class AnnoToCTest {
	public static void main(String[] args) {
		//读取配置文件;
		ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
	    Boss boss=(Boss) ctx.getBean("boss");
	    System.out.println(boss+"\t");
	   /* System.out.println(boss.getCar()+"\t");
	    System.out.println(boss.getOffice()+"\t");*/
	
	}

}

二、基于@Autowired注解配置bean

bean2.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


  <!-- 自动启动起来对于标注有@Autowired的bean进行自动注入 -->
  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

  <!-- boss类 移除boss Bean的属性注入配置的信息 -->
  <bean id="boss" class="com.test2.sprintoffice.Boss"/>
   
 
  
  <!-- Office类 -->
 
  <bean id="office" class="com.test.sprintoffice.Office">
     <property name="officeNo" value="003333"/>
  </bean>
  <!-- car类 -->
   <bean id="car" class="com.test.sprintoffice.Car" scope="singleton">
     <property name="brand" value="长安111"/>
     <property name="price" value="2000111"/>
  </bean>
</beans>

基于@Autowired注解自动注入IOC,不再强制需要该实体类的set方法(情况一、情况三)

boss

一、标注在成员变量上


/**
 * 使用@Autowired注释Boss
 * @author Administrator
 *
 */

public class Boss {
	//移除setter方法;
	/*@Autowired
	private Car car;
	@Autowired
	private Office office;
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}
	
}

二、注释标注在setter上

	//注释标注在setter上
	private Car car;
	private Office office;
	@Autowired
	public void setCar(Car car) {
		this.car = car;
	}
	@Autowired
	public void setOffice(@Qualifier("office2")Office office) {
		this.office = office;
	}
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}

三、标注在构造函数上

	//标注在构造函数上
	
	private Car car;
	private Office office;
	@Autowired(required=false)
	public Boss(Car car, @Qualifier("office2")Office office) {
	
		this.car = car;
		this.office = office;
	}
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}

当候选Bean数目不为1时应对方法

  问题:
   当候选Bean数目不为1时应对方法;
   @Autowired
   BeanCreationException

不强制必须存在匹配的一个bean

	//标注在构造函数上
	
	private Car car;
	private Office office;
	@Autowired(required=false)
	public Boss(Car car, @Qualifier("office2")Office office) {
	
		this.car = car;
		this.office = office;
	}
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}

@Autowired(required=false) 这种写法,不能应用于:二、注释标注在setter上

当配置相同类型多个id值的bean

/**
 * 使用@Autowired注释Boss
 * @author Administrator
 *
 */

public class Boss {
	//移除setter方法;
	/*@Autowired(required=false)
	private Car car;
	@Autowired(required=false)
	@Qualifier("office")
	private Office office;
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}*/
	
	
	
	//注释标注在setter上;
	/*private Car car;
	private Office office;
	@Autowired
	public void setCar(Car car) {
		this.car = car;
	}
	@Autowired
	public void setOffice(@Qualifier("office2")Office office) {
		this.office = office;
	}
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}*/
	
	//标注在构造函数上
	
	private Car car;
	private Office office;
	@Autowired(required=false)
	public Boss(Car car, @Qualifier("office2")Office office) {
	
		this.car = car;
		this.office = office;
	}
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}
		

}

  主题:
JSR-250注释;
    @Resource(byName) name=Bean  type=Bean的类型 ==@Autowired(byType) 

生命周期;接口:initializingBean/DisposableBean  bean元素指定:init-method/destory/method
    @PostConstruct 类实例化之后调用;
    @PreDestroy 类销毁之前调用;
    @Component 组件层
    @Service 业务层
    @Controller 控制层

@Resource(byName)的注解

bean2.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <!-- 自动启动起来对于标注有@Autowired的bean进行自动注入 -->
   <!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
 -->
 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
  <!-- boss类 移除boss Bean的属性注入配置的信息 -->
  <bean id="boss" class="com.test2.sprintoffice.Boss"/>
   
 
  
  <!-- Office类 -->
 <bean id="office" class="com.test.sprintoffice.Office">
     <property name="officeNo" value="0011"/>
 </bean>
 
  <!-- car类 -->
   <bean id="car" class="com.test.sprintoffice.Car" scope="singleton">
     <property name="brand" value="长安111"/>
     <property name="price" value="2000111"/>
  </bean>
</beans>

Boos

/**
 * 使用@Resource注释Boss
 * @author Administrator
 *
 */

public class Boss {
	//移除setter方法;
	@Resource
	private Car car;
    @Resource(name="office")
	private Office office;
    @PostConstruct
	public void postConstruct1(){
		System.out.println("postConstruct1");
	}
	@PreDestroy
	public void preDestory1(){
		System.out.println("preDestory1");
	}
	public String toString(){
		return "car:"+car+"\n"+"office:"+office;
	}

}

测试类  AnnoToCTest

/**
 * 以传统方式完成bean注入;
 * @author Administrator
 *
 */

public class AnnoToCTest {
	public static void main(String[] args) {
		//读取配置文件;
		ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("bean2.xml");
	    Boss boss=(Boss) ctx.getBean("boss");
	    System.out.println(boss+"\t");
	   ctx.destroy();//关闭spring容器,来触发Bean销毁方法的执行;
	   /* System.out.println(boss.getCar()+"\t");
	    System.out.println(boss.getOffice()+"\t");*/
	
	}

}

使用注解@Component配置注入bean

@Repository、@Service、@Controller == @Component

位置要放在类的定义 public class XXX 的定义行之上

bean.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:component-scan base-package="com.test"/>
</beans>

=======================================================================

参考资料:@Autowired @Resource @Qualifier的区别

end

发布了95 篇原创文章 · 获赞 20 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_40993412/article/details/103194014