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





  
  
     
     
  
  
  
  
     
  
  
  
     
     
  

测试类  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





  
  

  
  
   
 
  
  
 
  
     
  
  
   
     
     
  

 

基于@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

Spring基于XML配置方式注入bean对象和@Resource注解的使用_第1张图片

/**
 * 使用@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





    
   
 
  
  
   
 
  
  
 
     
 
 
  
   
     
     
  

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



    
    

 

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

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

end

你可能感兴趣的:(计算机的编程)