Spring 配置Bean 详解

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/85098726

Car类(里面有属性有无参构造函数,记住一定要有无参的构造函数,不然会报错)

这个里面有构造函数的重载方法

package cn.com.day01;

public class Car {
	/*author:命运的信徒
	 * date:2018/12/19
	 * arm:Spring 配置bean详解
	 */
private String name;
private double price;
private int speed;
public void setName(String name) {
	this.name = name;
}
public void setPrice(double price) {
	this.price = price;
}
public void setSpeed(int speed) {
	this.speed = speed;
}
@Override
public String toString() {
	return "Car [name=" + name + ", price=" + price + ", speed=" + speed + "]";
}
//必须有一个无参的构造器,不然就会报错
public Car() {
	// TODO Auto-generated constructor stub
}
public Car(String name, double price, int speed) {
	super();
	this.name = name;
	this.price = price;
	this.speed = speed;
}
public Car(String name,  int speed) {
	super();
	this.name = name;
	this.speed = speed;
}public Car(String name, double price) {
	super();
	this.name = name;
	this.price = price;
}
}

测试类

1.可以通过bean的id获取相应的bean配置,这个是唯一确认的(需要类型转化)

2.可以通过类class来获取,但是要求配置文件里面这个类型的bean是唯一的(好处就是不用类型转化,不适用)

package cn.com.day01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCar {
public static void main(String[] args) {
	//1.创建IOC容器
	ApplicationContext ioc=new  ClassPathXmlApplicationContext("applicationContext.xml");
	//2.1通过id来获取bean
	Car c=(Car) ioc.getBean("car");
	//2.2通过class来获取bean,但是通过class获取的bean的前提是在这个配置文件里面这个类型的bean是唯一的
	//Car c1=ioc.getBean(Car.class);
	System.out.println(c.toString());
}
}

在测试类被运行的时候,找到配置文件,根据id找到相应的bean配置,再根据bean给相应的属性赋值


bean配置如下

1.属性注入

<property name="属性名" value="属性值"></property>

2.构造器注入

<constructor-arg name="属性名" value="属性值" index="0" type="java.lang.String"></constructor-arg>

如果涉及到构造器的重载的话,可以通过index和type来匹配想要的构造器

<?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.1.xsd">
<!-- class就是Spring要替换的对象的全路径,id就是类名(第一个字母小写) -->
<!-- <bean id="helloWorld" class="cn.com.day01.HelloWorld">
name是setName的set+名称,这个名称的第一个字母小写 
<property name="name" value="Spring"></property>
</bean> -->


<bean id="car" class="cn.com.day01.Car" >
<!-- 通过属性注入的方式 -->
<!-- <property name="name" value="Spring"></property>
<property name="price" value="90" ></property>
<property name="speed" value="100" ></property> -->
<!-- 通过构造函数注入 -->
<!-- <constructor-arg name="name" value="aodi" ></constructor-arg>
<constructor-arg name="price" value="9000"></constructor-arg>
<constructor-arg name="speed" value="120"></constructor-arg> -->
<!-- 对于重载的构造器怎么办呢?通过类型和顺序,二者可以混搭
 -->
 <constructor-arg name="name" value="aodi" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg name="price" value="9000" type="double"></constructor-arg>
</bean>

</beans>

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/85098726