文章目录
前言
跟随尚硅谷学习Spring5
IOC容器
一、IOC 操作 Bean 管理(xml注入集合属性)
注入数组、List、Map类型属性
创建类,定义属性和对应的 set 方法
package com.atguigu.spring5.dependencyInjection.collections;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
// 1 - Array数组类型属性
private String[] studentName;
private String[] courses;
// 2 - List集合类型属性
private List<String> list;
// 3 - Map集合类型属性
private Map<String, String> maps;
// 4 - Set集合类型属性
private Set<String> sets;
public void setStudentName(String[] studentName) {
this.studentName = studentName;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
@Override
public String toString() {
return "Student{" +
"studentName=" + Arrays.toString(studentName) +
", courses=" + Arrays.toString(courses) +
", list=" + list +
", maps=" + maps +
", sets=" + sets +
'}';
}
}
在 spring 配置文件配置对象创建,配置属性注入
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置User对象创建-->
<bean id="student" class="com.atguigu.spring5.dependencyInjection.collections.Student">
<!-- 1 - String[]类型 -->
<!--list写法 - 与Array通用-->
<property name="studentName">
<list>
<value>王五</value>
<value>小五</value>
</list>
</property>
<!--Array写法 - 与list通用-->
<property name="courses">
<array>
<value>java</value>
<value>python</value>
</array>
</property>
<!-- 2 - List<String>类型 -->
<property name="list">
<!--方式一-->
<!-- <array>-->
<!-- <value>list-1</value>-->
<!-- <value>list-2</value>-->
<!-- </array>-->
<!--方式二-->
<list>
<value>list-3</value>
<value>list-4</value>
</list>
</property>
<!-- 3 - Map<String, String>类型 -->
<property name="maps">
<map>
<entry key="java" value="v1"/>
<entry key="c++" value="v2"/>
<entry key="pyhton" value="v3"/>
</map>
</property>
<!-- 4 - Set<String>类型 -->
<property name="sets">
<set>
<value>set-1</value>
<value>set-2</value>
</set>
</property>
</bean>
</beans>
测试:
package com.atguigu.sprint5;
import com.atguigu.spring5.dependencyInjection.BookSetterInjection;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BookTest {
@Test
public void testBookSetterInjection() {
ApplicationContext context = new ClassPathXmlApplicationContext("bookSetterInjection.xml");
BookSetterInjection book = context.getBean("book", BookSetterInjection.class);
System.out.println(book);
}
}
在集合里面设置对象类型值
之前设置的字符串
创建类,定义属性和对应的 set 方法
package com.atguigu.spring5.dependencyInjection.collections;
public class Course {
private String courseName;
public void setCourseName(String courseName) {
this.courseName = courseName;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
'}';
}
}
在 spring 配置文件中进行配置
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置User对象创建-->
<bean id="student" class="com.atguigu.spring5.dependencyInjection.collections.Student">
<!-- 5 - List<Course>类型 -->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!-- 5 - List<Course>类型: Course的bean-->
<bean id="course1" class="com.atguigu.spring5.dependencyInjection.collections.Course">
<property name="courseName" value="spring"/>
</bean>
<bean id="course2" class="com.atguigu.spring5.dependencyInjection.collections.Course">
<property name="courseName" value="redis"/>
</bean>
</beans>
测试:
package com.atguigu.sprint5;
import com.atguigu.spring5.dependencyInjection.collections.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest {
@Test
public void testStudent() {
ApplicationContext context = new ClassPathXmlApplicationContext("Student.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student);
}
}
把集合注入部分提取出来
创建类,定义属性和对应的 set 方法
package com.atguigu.spring5.dependencyInjection.collections;
import java.util.List;
public class Book {
private List<String> bookList;
public void setBookList(List<String> bookList) {
this.bookList = bookList;
}
@Override
public String toString() {
return "Book{" +
"bookList=" + bookList +
'}';
}
}
在 spring 配置文件中引入名称空间 util,把beans改成util
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!-- 把集合注入部分提取出来, 使用xsi:util 命名空间 -->
<!--提取list集合类型属性注入-->
<util:list id="utilList">
<value>cjjs神经经</value>
<value>九阳神功</value>
<value>九阴真经</value>
</util:list>
<bean id="book" class="com.atguigu.spring5.dependencyInjection.collections.Book">
<!--ref util的bean id完成list集合注入-->
<property name="bookList" ref="utilList"/>
</bean>
</beans>
测试:
package com.atguigu.sprint5;
import com.atguigu.spring5.dependencyInjection.collections.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BookTest {
@Test
public void testBook() {
ApplicationContext context = new ClassPathXmlApplicationContext("Book.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
}
}
二、IOC 操作 Bean 管理(FactoryBean)
- Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)
- 普通 bean:在配置文件中定义 bean 类型就是返回类型
- 工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
- 普通bean操作步骤
创建类->编写配置文件->测试 - 工厂bean操作步骤
创建类,让这个类作为工厂 bean,实现接口 FactoryBean
package com.atguigu.spring5.dependencyInjection.factoryBean;
import org.springframework.beans.factory.FactoryBean;
// 定义数据MyBean类对象,而返回类型使用Course类对象,重写接口Factory的getObject()方法
public class MyBean implements FactoryBean<Course> {
public Course getObject() throws Exception {
// 手动设置一个course值后返回
Course course = new Course();
course.setCourseName("abc");
return course;
}
public Class<?> getObjectType() {
return null;
}
public boolean isSingleton() {
return false;
}
}
在 spring 配置文件中进行配置
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--MyBean类,该类实现FactoryBean接口,更改返回类型为Course-->
<bean id="myBean" class="com.atguigu.spring5.dependencyInjection.factoryBean.MyBean"/>
</beans>
测试:
package com.atguigu.sprint5;
import com.atguigu.spring5.dependencyInjection.factoryBean.Course;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FactoryBeanTest {
@Test
public void testFactoryBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("FactoryBean.xml");
// expected to be of type 'com.atguigu.spring5.dependencyInjection.factoryBean.MyBean' but was actually of type 'com.atguigu.spring5.dependencyInjection.factoryBean.Course'
// MyBean myBean = context.getBean("myBean", MyBean.class);
// System.out.println(myBean);
// 返回类型为Course,bean类为MyBean。
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
}
三、IOC 操作 Bean 管理(bean 作用域)
- 在 Spring 里面,设置创建 bean 实例是单实例还是多实例
- 在 Spring 里面,默认情况下, bean 是单实例对象
- 如何设置单实例还是多实例
- 在 spring 配置文件 bean 标签里面有属性( scope)用于设置单实例还是多实例
- scope 属性值
第一个值 默认值, singleton,表示是单实例对象
第二个值 prototype,表示是多实例对象 - singleton 和 prototype 区别
第一 singleton 单实例, prototype 多实例
第二 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象
设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建对象,在调用getBean方法时候创建多实例对象
创建类
package com.atguigu.spring5.dependencyInjection.beanScope;
public class Book {
}
在 spring 配置文件中进行配置
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookScopeDefault" class="com.atguigu.spring5.dependencyInjection.beanScope.Book"/>
<bean id="bookScopeSingleton" class="com.atguigu.spring5.dependencyInjection.beanScope.Book" scope="singleton"/>
<bean id="bookScopePrototype" class="com.atguigu.spring5.dependencyInjection.beanScope.Book" scope="prototype"/>
</beans>
测试:
package com.atguigu.sprint5;
import com.atguigu.spring5.dependencyInjection.beanScope.Book;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanScopeTest {
@Test
public void testBeanScopeDefault() {
ApplicationContext context = new ClassPathXmlApplicationContext("beanScope.xml");
Book book1 = context.getBean("bookScopeDefault", Book.class);
Book book2 = context.getBean("bookScopeDefault", Book.class);
System.out.println(book1);
System.out.println(book2);
Assert.assertEquals("检查book1和book2是否单例同一个对象", book1, book2);
}
@Test
public void testBeanScopeSingleton() {
ApplicationContext context = new ClassPathXmlApplicationContext("beanScope.xml");
Book book1 = context.getBean("bookScopeSingleton", Book.class);
Book book2 = context.getBean("bookScopeSingleton", Book.class);
System.out.println(book1);
System.out.println(book2);
Assert.assertEquals("检查book1和book2是否单例同一个对象", book1, book2);
}
@Test
public void testBeanScopePrototype() {
ApplicationContext context = new ClassPathXmlApplicationContext("beanScope.xml");
Book book1 = context.getBean("bookScopePrototype", Book.class);
Book book2 = context.getBean("bookScopePrototype", Book.class);
System.out.println(book1);
System.out.println(book2);
Assert.assertNotEquals("检查book1和book2是否多实例不同对象", book1, book2);
}
}