使用xml管理Bean的几种方式
1.无参构造的类注入方法:
首先我们需要创建一个无参构造的类:
public class Book {
public Book(){
}
}
xml文件编写:
<!-- beans.xml -->
<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相当于这个bean的一个唯一id,class是类的名字(包名+类名)-->
<bean id="myBean" class="com.example.Book" />
</beans>
在上述示例中,我们定义了一个名为 myBean 的 Bean,并且指定了它的类为 com.example.Book。由于没有显式地定义任何构造函数参数,Spring 将会使用类的无参构造函数来实例化这个 Bean。
2.有参构造的类注入方法:
在 Spring 中,基于 XML 的 Bean 的有参构造方式可以通过 <constructor-arg> 元素来实现。
首先创建一个有参构造的类:
public class Book {
private String name;
private String author;
public Book(String name,String author){
this.name=name;
this.author=authoor;
}
public void getAll(){
System.out.print("书名:"+name+"====作者:"+author);
}
}
xml文件编写:
<!-- beans.xml -->
<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 -->
<bean id="Book1" class="com.example.Book" >
<!-- 通过<constructor-arg>标签来实现,其中name是指属性名字,value就是属性值-->
<constructor-arg name="name" value="西游记"/>
<constructor-arg name="author" value="吴承恩"/>
</bean>
<bean id="Book2" class="com.example.Book">
<!--也可以使用index来表示有参构造器参数的位置,如这里的0就是类里面有参构造器的第一个参数:name -->
<constructor-arg index="0" value="三国演义"/>
<constructor-arg index="1" value="罗贯中"/>
</bean>
</beans>
测试代码效果:
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");
Book book1 = applicationContext.getBean("Book1", Book.class);
Book book2 = applicationContext.getBean("Book2", Book.class);
book1.getAll();
book2.getAll();
}
}
效果如下:
3.使用set方法注入属性:
在 Spring 中,基于 XML 的 Bean 的set方法设置属性的方式可以通过 <property> 元素来实现。
book类:
public class Book {
private String name;
private String author;
public void setAuthor(String author) {
this.author = author;
}
public void setName(String name) {
this.name = name;
}
public void getAll(){
System.out.println("书名:"+name+"====作者:"+author);
}
}
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="book" class="com.example.Book">
<property name="name" value="红楼梦" />
<property name="author" value="曹雪芹" />
</bean>
</beans>
实现效果:
4.注入空值的情况
注入空值的情况value中不能直接写在value里,需要使用双标签的方式来实现:
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="book" class="com.example.Book">
<property name="name" value="红楼梦" />
<property name="author">
<null/>
</property>
</bean>
</beans>
效果如下:
5.为对象类型属性赋值
当一个类中有对象类型的属性时,也就是其中有一个属性是类,这时候就需要为这个类对象赋值后再注入,有三种方法可以实现,一种是内部注入,一种是外部注入,还有一种是级联注入。
类结构:
Book类:
public class Book {
private String name;
private String author;
public void setAuthor(String author) {
this.author = author;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public String getName() {
return name;
}
public void getAll(){
System.out.println("书名:"+name+"====作者:"+author);
}
}
Classics类:
public class Classics {
private String country;
private Book book;
public void setCountry(String country) {
this.country = country;
}
public void setBook(Book book) {
this.book = book;
}
public void getAll(){
System.out.println("书名:"+book.getName()+"====作者:"+book.getName()+"====国家:"+country);
}
}
5.1外部注入方法
xml文件编写:
<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="book" class="com.example.Book">
<property name="name" value="红楼梦" />
<property name="author" value="施耐庵"></property>
</bean>
<!--外部注入的方法,使用ref属性 -->
<bean id="classics" class="com.example.Classics">
<property name="book" ref="book"/>
<property name="country" value="中国"/>
</bean>
</beans>
测试结果:
5.2内部注入
xml文件编写:
<bean id="classics" class="com.example.Classics">
<property name="book">
<!--内部注入就是将property写成双标签的方式,然后将这个对象转化为bean注入 -->
<bean id="inside" class="com.example.Book">
<property name="name" value="西游记"/>
<property name="author" value="吴承恩"/>
</bean>
</property>
<property name="country" value="中国"/>
</bean>
测试结果:
5.3级联注入
xml文件编写:
<bean id="book" class="com.example.Book">
</bean>
<bean id="classics" class="com.example.Classics">
<property name="country" value="中国"/>
<property name="book" ref="book"/>
<property name="book.name" value="三国演义"/>
<property name="book.author" value="罗贯中"/>
</bean>
效果:
注意:如果使用内敛的方法,需要在Classics类中编写getBook的方法,不然报错
public Book getBook() {
return book;
}
6.为数组类型属性赋值
创建一个有数组属性的类:
package com.example;
public class Student {
private Integer id;
private String name;
private String[] hobbies;
public Student() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public void getAll(){
System.out.println("姓名:"+name+"=====学号:"+id+"=====爱好如下");
for (String hobby : hobbies) {
System.out.print(hobby+" ");
}
}
}
xml文件编写:
<bean id="student1" class="com.example.Student">
<property name="name" value="张三"/>
<property name="id" value="123"/>
<property name="hobbies">
<!-- 使用array标签来插入数组内的值-->
<array>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</array>
</property>
</bean>
测试结果如下:
7.List集合类型属性赋值
这时候我们需要创建一个包含List集合的一个对象,这里就修稿Classics类
Classics类:
package com.example;
import java.util.List;
public class Classics {
private String country;
private List<Book> book;
public void setCountry(String country) {
this.country = country;
}
public void setBook(List<Book> book) {
this.book = book;
}
public void getAll(){
System.out.println("国家:"+country);
book.forEach(item->{
item.getAll();
});
}
}
xml文件编写:
<bean id="book1" class="com.example.Book">
<property name="name" value="西游记"/>
<property name="author" value="吴承恩"/>
</bean>
<bean id="book2" class="com.example.Book">
<property name="name" value="红楼梦"/>
<property name="author" value="施耐庵"/>
</bean>
<bean id="book3" class="com.example.Book">
<property name="name" value="三国演义"/>
<property name="author" value="罗贯中"/>
</bean>
<bean id="classics" class="com.example.Classics">
<property name="country" value="中国"/>
<property name="book">
<!-- 使用list标签将外部bean注入进来 -->
<list>
<ref bean="book1"/>
<ref bean="book2"/>
<ref bean="book3"/>
</list>
</property>
</bean>
效果:
8.为Map集合类型属性赋值
重新编写Student类,添加成绩属性:
package com.example;
import java.util.Map;
import java.util.Set;
public class Student {
private Integer id;
private String name;
private String[] hobbies;
private Map<String,Integer> achievement;
public Student() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public Map<String, Integer> getAchievement() {
return achievement;
}
public void setAchievement(Map<String, Integer> achievement) {
this.achievement = achievement;
}
public void getAll(){
System.out.println("姓名:"+name+"=====学号:"+id+"=====爱好如下");
for (String hobby : hobbies) {
System.out.print(hobby+" ");
}
System.out.println();
System.out.println("成绩如下");
Set<String> strings = achievement.keySet();
for (String string : strings) {
System.out.println(string+":"+achievement.get(string));
}
}
}
xml文件编写:
<bean id="student1" class="com.example.Student">
<property name="name" value="张三"/>
<property name="id" value="123"/>
<property name="hobbies">
<array>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</array>
</property>
<property name="achievement">
<!--使用map标签注入map属性 -->
<map>
<!--一个entry就相当于一个kv对 -->
<entry key="语文" value="99"/>
<entry key="数学" value="98"/>
<entry key="英语" value="97"/>
</map>
</property>
</bean>
效果如下: