Day17——Spring使用XML方式进行自动装配

一. 储备知识

本文章项目源码已上传到本博客的“资源”处,可自行前往免费下载

1.1 自动装配的概念

  1. 手动装配:以value或ref明确指定属性值
  2. 自动装配:根据指定的装配规则,不需要明确指定,Spring自动将匹配的属性值注入bean中,<bean>标签中使用autowire属性指定装配模式

1.2 装配模式

  1. 根据类型自动装配(byType):将类型匹配的bean注入到另一个bean中。若IOC容器中有多个与目标bean类型一致的bean,Spring将无法判定哪个bean最适合该属性,所以不能执行自动装配。
  2. 根据名称自动装配(byName):必须将目标bean的名称和属性名设置的完全相同。

1.3 选用建议

相对于使用注解的方式自动装配,在XML文件中进行自动装配略显笨拙,在项目中更多的是使用注解的方式实现。

二. 例子

使用spring的自动装配并输出信息

Address.java

package com.atguigu.spring.autowire;

public class Address {
       
	private String province;
	
	private String city;

	public Address() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Override
	public String toString() {
		return "Address [province=" + province + ", city=" + city + "]";
	}
	
}

Car.java

package com.atguigu.spring.autowire;

public class Car {
        private String brand;
        
        private Double price;

		public Car() {
			super();
			// TODO Auto-generated constructor stub
		}

		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;
		}

		@Override
		public String toString() {
			return "Car [brand=" + brand + ", price=" + price + "]";
		}
}

Person.java

package com.atguigu.spring.autowire;

public class Person {
       private String name;
       
       private Address address;
       
       private Car car;

       public Person() {
    	   super();
    	   // TODO Auto-generated constructor stub
       }

		public String getName() {
			return name;
		}
	
		public void setName(String name) {
			this.name = name;
		}
	
		public Address getAddress() {
			return address;
		}
	
		public void setAddress(Address address) {
			this.address = address;
		}
	
		public Car getCar() {
			return car;
		}
	
		public void setCar(Car car) {
			this.car = car;
		}
	
		@Override
		public String toString() {
			return "Person [name=" + name + ", address=" + address + ", car=" + car + "]";
		}
}

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

    <bean id="address" class="com.atguigu.spring.autowire.Address">
         <property name="province" value="广东省"></property>
         <property name="city" value="广州市"></property>
    </bean>
    
    <bean id="car" class="com.atguigu.spring.autowire.Car">
         <property name="brand" value="宝马"></property>
         <property name="price" value="10000"></property>
    </bean>
    
    <!-- 通过名字自动装配 -->
   <!--  <bean id="person" class="com.atguigu.spring.autowire.Person" autowire="byName">
         <property name="name" value="老马"></property>
    </bean> -->
    
    <!-- 通过类型自动装配 -->
    <bean id="person" class="com.atguigu.spring.autowire.Person" autowire="byType">
         <property name="name" value="老马"></property>
    </bean>
</beans>

Main.java

package com.atguigu.spring.autowire;

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

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        ApplicationContext context = 
        		new ClassPathXmlApplicationContext("spring-autowire.xml");
        Person person = context.getBean("person", Person.class);
        System.out.println(person);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值