Spring框架学习笔记——配置bean
Posted 灼眼的健
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring框架学习笔记——配置bean相关的知识,希望对你有一定的参考价值。
1、属性注入
(1)根据setter方法属性注入,这里使用的是property标签。需要bean属性提供对应的setter方法,比如笔记(1)里的 HelloWorld使用的就是这种方法。
<!-- 根据setter方法属性注入 --> <bean id="helloworld" class="com.broadtext.beans.Helloworld"> <property name="name" value="Helloworld"></property> </bean>
(2)根据构造器属性注入,这里使用的是constructor-arg标签。可以不提供setter方法,但是要提供对应的构造器,如果缺少构造器或者构造器里缺少属性就会报错。可以指定参数的位置(index)和类型(type)以区分参数。特殊字符可以使用<![CDATA[<Shanghai>]]>
<!-- 根据构造器属性注入,可以指定参数的位置和类型以区分参数 --> <bean id="car" class="com.broadtext.beans.Car"> <constructor-arg index="0" value="Audi"></constructor-arg> <constructor-arg value="Shanghai"></constructor-arg> <constructor-arg type="double" value="300000"></constructor-arg> </bean>
<bean id="car2" class="com.broadtext.beans.Car"> <constructor-arg value="BMW"></constructor-arg> <constructor-arg> <value><![CDATA[<Shanghai>]]></value> </constructor-arg> <constructor-arg type="int" value="240"></constructor-arg> </bean>
2、通过ref属性建立bean之间的关系,这里在创建person bean的时候会将配置好的car2作为属性注入到person bean里,如果ref指向一个不存在的bean会报undefine错误。
<!-- ref属性建立bean之间的关系 --> <bean id="person" class="com.broadtext.beans.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <property name="car" ref="car2"></property> </bean>
3、配置内部bean
<!-- 内部bean不能被外部使用 --> <bean id="person1" class="com.broadtext.beans.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <property name="car"> <bean class="com.broadtext.beans.Car"> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="Shanghai"></constructor-arg> <constructor-arg type="double" value="300000"></constructor-arg> </bean> </property> </bean>
4、null值的注入
<!-- 测试null值和构造注入 --> <bean id="person2" class="com.broadtext.beans.Person"> <constructor-arg value="Jreey"></constructor-arg> <constructor-arg value="24"></constructor-arg> <constructor-arg><null></null></constructor-arg> </bean>
5、为级联属性注入
<!-- 级联属性赋值 --> <bean id="person3" class="com.broadtext.beans.Person"> <constructor-arg value="Jreey"></constructor-arg> <constructor-arg value="24"></constructor-arg> <!-- 要先初始化才可以为级联属性赋值,否则会报null错误 --> <constructor-arg ref="car"></constructor-arg> <property name="car.maxSpeed" value="250"></property> </bean>
6、Car.java、Person.java、Main.java
Car.java
package com.broadtext.beans; public class Car { private String brand; private String corp; private double price; private int maxSpeed; public Car(String brand, String corp, double price) { super(); this.brand = brand; this.corp = corp; this.price = price; } public Car(String brand, String corp, int maxSpeed) { super(); this.brand = brand; this.corp = corp; this.maxSpeed = maxSpeed; } @Override public String toString() { return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]"; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getCorp() { return corp; } public void setCorp(String corp) { this.corp = corp; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } }
Person.java
package com.broadtext.beans; public class Person { private String name; private int age; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", car=" + car + "]"; } public Person() { } public Person(String name, int age, Car car) { super(); this.name = name; this.age = age; this.car = car; } }
Main.java
package com.broadtext.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //ApplicationContext IOC容器 //ClassPathXmlApplicationContext IOC容器的实现类 //走完这一步就会创建出XML里面配的bean ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //根据配置bean的id获取 Helloworld helloworld = (Helloworld) ctx.getBean("helloworld"); System.out.println(helloworld.getName()); //根据配置bean的类型获取 helloworld = ctx.getBean(Helloworld.class); System.out.println(helloworld.getName()); //构造器属性注入 Car car = (Car) ctx.getBean("car"); System.out.println(car.toString()); car = (Car) ctx.getBean("car2"); System.out.println(car.toString()); //ref Person person = (Person) ctx.getBean("person"); System.out.println(person.toString()); //内部bean person = (Person) ctx.getBean("person1"); System.out.println(person.toString()); person = (Person) ctx.getBean("person2"); System.out.println(person.toString()); person = (Person) ctx.getBean("person3"); System.out.println(person.toString()); } }
以上是关于Spring框架学习笔记——配置bean的主要内容,如果未能解决你的问题,请参考以下文章
Java框架spring Boot学习笔记:bean管理(注解和配置文件混合使用)