持续学习&持续更新中…
守破离
【雷丰阳SSM基础】【Spring】【03】IoC容器_配置bean—上
实验01:HelloWorld
<?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 class="programmer.lp.domain.Person" id="person">
<property name="name" value="lp"/>
<property name="age" value="22"/>
</bean>
</beans>
public class BeanTest {
private ApplicationContext ctx = new ClassPathXmlApplicationContext(
"ioc.xml");
// 实验1:通过IoC容器创建对象,并为属性赋值
@Test
public void test01() {
Object bean01 = ctx.getBean("person");
Person bean02 = (Person) ctx.getBean("person");
System.out.println(((Person)bean01).getName());
System.out.println(bean02);
}
}
实验02:通过bean的类型获取bean实例
<?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 class="programmer.lp.domain.Person" id="person01">
<property name="name" value="lp"/>
<property name="age" value="22"/>
</bean>
<bean class="programmer.lp.domain.Person" id="person02">
<property name="name" value="lpruoyu"/>
<property name="age" value="18"/>
</bean>
</beans>
// 实验2:根据bean的类型从IoC容器中获取bean的实例
@Test
public void test02() {
// 如果IoC容器中这个类型的bean有多个,那么只使用类型就会出现异常
// Person person = ctx.getBean(Person.class);
// System.out.println(person);
// 推荐
Person person01 = ctx.getBean("person01", Person.class);
Person person02 = ctx.getBean("person02", Person.class);
System.out.println(person01);
System.out.println(person02);
}
实验03:通过构造器为bean赋值、名称空间p
public class Person {
private String name;
private Integer age;
public Person() {
}
// @ConstructorProperties({"name", "age"})
// public Person(String n, Integer a) {
// name = n;
// age = a;
// }
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
// getter setter
}
<?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 class="programmer.lp.domain.Person" id="person01">
<!-- 通过bean对象的setter赋值 -->
<property name="name" value="lpruoyu" />
<property name="age" value="11" />
</bean>
<bean class="programmer.lp.domain.Person" id="person02">
<!-- 通过有参构造器 public Person(String name, Integer age) 进行创建对象并赋值 -->
<!-- <constructor-arg type="java.lang.String" value="lpruoyu" />
<constructor-arg type="java.lang.Integer" value="22" /> -->
<constructor-arg type="java.lang.Integer" value="22" />
<constructor-arg type="java.lang.String" value="lpruoyu" />
</bean>
<bean class="programmer.lp.domain.Person" id="person03">
<constructor-arg index="0" value="lpruoyu" />
<constructor-arg index="1" value="33" />
<!-- <constructor-arg index="1" value="33" />
<constructor-arg index="0" value="lpruoyu" /> -->
</bean>
<!-- 如果有参构造器有重载,那么可以将index、type搭配着一起使用 -->
<bean class="programmer.lp.domain.Person" id="person04">
<!-- 必须和有参构造器Person(String name, Integer age)的参数顺序一致 -->
<constructor-arg value="lpruoyu" />
<constructor-arg value="44" />
</bean>
<!-- 推荐使用name属性指定构造器的参数 -->
<bean class="programmer.lp.domain.Person" id="person05">
<!-- <constructor-arg name="age" value="55" />
<constructor-arg name="name" value="lpruoyu" /> -->
<constructor-arg name="name" value="lpruoyu" />
<constructor-arg name="age" value="55" />
</bean>
</beans>
/*
实验3:
通过构造器为bean赋值(index、type、name属性介绍)
*/
@Test
public void test03() {
System.out.println(ctx.getBean("person01", Person.class));
System.out.println(ctx.getBean("person02", Person.class));
System.out.println(ctx.getBean("person03", Person.class));
System.out.println(ctx.getBean("person04", Person.class));
System.out.println(ctx.getBean("person05", Person.class));
}
名称空间p:
public class Person {
private String name;
private Integer age;
// getter setter constructor
}
public class Student {
private Person person;
// getter setter constructor
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!--
名称空间:在XML中,名称空间(xmlns)是用来防止 标签/属性 重复的。
-->
<!-- 通过p名称空间为bean的属性赋值 -->
<bean class="programmer.lp.domain.Person" id="person"
p:name="lpruoyu" p:age="22"/>
<bean class="programmer.lp.domain.Student" id="student"
p:person-ref="person"/>
</beans>
/*
实验3:
通过p名称空间为bean的属性赋值
*/
@Test
public void test03_2() {
System.out.println(ctx.getBean("person", Person.class));
System.out.println(ctx.getBean("student", Student.class));
}
实验04:正确的为各种类型的属性赋值
被Spring的IoC容器管理(创建)的类,我们称之为bean
bean:
public class Book {
private String name;
private String author;
public Book() {
System.out.println("public Book()");
}
// getter setter toString
}
public class Car {
private String name;
private Double price;
public Car() {
System.out.println("public Car()");
}
// getter setter toString
}
public class Human {
private String name = "张三";
private Integer age;
private Date birthday;
private Car car;
private List<Book> books;
private Map<String, Object> map;
private Properties properties;
public Human() {
System.out.println("public Human()");
}
// getter setter toString
}
常见类型:
<bean id="human" class="programmer.lp.domain.Human">
<property name="name" value="lpruoyu"/>
<property name="age" value="22"/>
<property name="birthday" value="2000/07/20"/>
</bean>
赋值为null:
<bean id="human" class="programmer.lp.domain.Human">
<property name="name">
<!-- 在property标签里面进行复杂的赋值 -->
<null/>
</property>
</bean>
引用类型(自定义类型):
<?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.xsd">
<bean class="programmer.lp.domain.Car" id="car">
<property name="name" value="BMW"/>
<property name="price" value="10"/>
</bean>
<bean id="human" class="programmer.lp.domain.Human">
<!--
相当于:car = getBean("car");
<property name="car" ref="car"/>
-->
<property name="car">
<!--
相当于:car = new Car();
<bean class="programmer.lp.domain.Car" id="car">
<property name="name" value="Benz"/>
<property name="price" value="11"/>
</bean>
-->
<!-- 相当于:car = getBean("car"); -->
<ref bean="car"/>
</property>
</bean>
</beans>
集合类型(List、Map、Properties):
<?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.xsd">
<bean id="shz" class="programmer.lp.domain.Book" p:name="水浒传" p:author="施耐庵"/>
<bean class="programmer.lp.domain.Human" id="human">
<property name="books">
<list>
<bean class="programmer.lp.domain.Book">
<property name="name" value="红楼梦"/>
<property name="author" value="曹雪芹"/>
</bean>
<bean class="programmer.lp.domain.Book" p:name="三国演义" p:author="罗贯中"/>
<!-- 就算给bean加上id,也不能被IoC容器通过getBean(id)的方式获取使用,所以一般不写 -->
<bean id="innerCarId" class="programmer.lp.domain.Book" p:name="西游记" p:author="吴承恩"/>
<ref bean="shz"/>
</list>
</property>
<property name="map">
<!-- 在标签体中进行复杂的赋值 -->
<map>
<entry key="int" value="10" value-type="java.lang.Integer"/>
<!-- value默认是String类型 -->
<entry key="string" value="str—coding"/>
<entry key="str">
<value>str—programming</value>
</entry>
<entry key="object">
<bean class="programmer.lp.domain.Student"/>
</entry>
<entry key="book" value-ref="shz"/>
</map>
</property>
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">root123</prop>
</props>
</property>
</bean>
</beans>
使用util名称空间创建集合类型的bean,方便别人引用:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean class="programmer.lp.domain.Car" id="car">
<property name="name" value="宝马"/>
<property name="price" value="10"/>
</bean>
<util:map id="commonMap">
<entry key="int" value="10" value-type="java.lang.Integer"/>
<entry key="string" value="string"/>
<entry key="str">
<value>str</value>
</entry>
<entry key="object">
<!-- SpEL表达式 :#{} -->
<bean class="programmer.lp.domain.Student"
p:person="#{new programmer.lp.domain.Person()}"/>
</entry>
<!-- <entry key="car" value-ref="car"/> -->
<entry key="car">
<ref bean="car"/>
</entry>
</util:map>
<bean class="programmer.lp.domain.Human" id="human01">
<property name="map" ref="commonMap"/>
</bean>
<bean class="programmer.lp.domain.Human" id="human02">
<property name="map" ref="commonMap"/>
</bean>
</beans>
@Test
public void test04() {
Human human01 = ctx.getBean("human01", Human.class);
Human human02 = ctx.getBean("human02", Human.class);
System.out.println(human01);
System.out.println(human02);
System.out.println(ctx.getBean("commonMap"));
System.out.println(human01.getMap() == human02.getMap()); // true
System.out.println(human01.getMap() == ctx.getBean("commonMap")); // true
}
级联属性赋值(某个bean对象的属性的属性就叫做级联属性):
<?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 class="programmer.lp.domain.Car" id="car">
<property name="name" value="宝马"/>
<property name="price" value="10"/>
</bean>
<bean class="programmer.lp.domain.Human" id="human">
<!--
级联属性赋值:
为该bean对象的car属性赋值的时候,
改变掉car属性的某个属性的值,
比如说改变car的price属性
-->
<property name="car" ref="car"/>
<property name="car.price" value="20"/>
</bean>
</beans>
@Test
public void test04() {
Human human = ctx.getBean("human", Human.class);
System.out.println(ctx.getBean("car")); // car会被修改
System.out.println(human.getCar());
}
实验05:工厂方式创建bean
之前的bean,都是些简单的bean,Spring可以直接利用反射机制new出来。
但是有些复杂的bean并不能直接new,那么这时就需要使用工厂的方式来创建bean了。
工厂模式:让工厂帮助我们创建对象,我们不需要知道那些复杂对象创建的细节。
静态工厂:工厂本身不用创建对象,调用工厂的静态方法。
实例工厂:工厂本身需要创建对象,调用工厂对象的实例方法。
静态工厂:
静态工厂实现方式一:
db.properties(直接放在src目录下):
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/lp_resume
username=root
password=root
public final class ConnectionStaticFactory {
private ConnectionStaticFactory() {
}
// private static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
// private static final String URL = "jdbc:mysql://127.0.0.1:3306/lp_resume";
// private static final String USERNAME = "root";
// private static final String PASSWORD = "root";
private static String DRIVER_CLASS_NAME;
private static String URL;
private static String USERNAME;
private static String PASSWORD;
static {
InputStream is = null;
try {
is = ConnectionStaticFactory.class.getClassLoader().getResourceAsStream(
"db.properties");
Properties properties = new Properties();
properties.load(is);
DRIVER_CLASS_NAME = properties.getProperty("driverClassName");
URL = properties.getProperty("url");
USERNAME = properties.getProperty("username");
PASSWORD = properties.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
Class.forName(DRIVER_CLASS_NAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接获取失败!");
return null;
}
}
}
<!-- 静态工厂: -->
<bean id="connection" scope="prototype"
class="programmer.lp.factory.ConnectionStaticFactory"
factory-method="getConnection"/>
静态工厂实现方式二:
public final class ConnectionStaticFactory2 {
private ConnectionStaticFactory2() {
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
public static Connection getConnection(String url, String username,
String password) {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接获取失败!");
return null;
}
}
}
<?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="connection" scope="prototype"
class="programmer.lp.factory.ConnectionStaticFactory2" factory-method="getConnection">
<!-- 使用constructor-arg可以为方法传递参数 -->
<constructor-arg name="url"
value="jdbc:mysql://127.0.0.1:3306/lp_resume" />
<constructor-arg name="username" value="root" />
<constructor-arg name="password" value="root" />
</bean>
</beans>
@Test
public void test05() {
System.out.println(ctx.getBean("connection"));
}
实例工厂:
实例工厂实现方式一:
public final class ConnectionInstanceFactory {
private String driverClassName;
private String url;
private String username;
private String password;
public ConnectionInstanceFactory(String driverClassName, String url,
String username, String password) {
this.driverClassName = driverClassName;
this.url = url;
this.username = username;
this.password = password;
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
public Connection getConnection() {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接获取失败!");
return null;
}
}
}
<?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">
<!--
工厂模式:让工厂帮助我们创建对象,我们不需要知道那些复杂对象创建的细节。
静态工厂:工厂本身不用创建对象,调用工厂的静态方法。
实例工厂:工厂本身需要创建对象,调用工厂对象的实例方法。
-->
<!-- 实例工厂: -->
<!-- scope默认为singleton,单实例 -->
<bean id="cif" class="programmer.lp.factory.ConnectionInstanceFactory">
<constructor-arg name="driverClassName" value="com.mysql.jdbc.Driver"/>
<constructor-arg name="url" value="jdbc:mysql://127.0.0.1:3306/lp_resume"/>
<constructor-arg name="username" value="root"/>
<constructor-arg name="password" value="root"/>
</bean>
<bean id="connection" scope="prototype"
factory-bean="cif" factory-method="getConnection"/>
</beans>
实例工厂实现方式二:
public final class ConnectionInstanceFactory2 {
private String url;
private String username;
private String password;
public ConnectionInstanceFactory2() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public Connection getConnection() {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接获取失败!");
return null;
}
}
}
<?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="instanceFactory2" class="programmer.lp.factory.ConnectionInstanceFactory2">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/lp_resume" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 实例工厂 -->
<bean id="connection" scope="prototype"
factory-bean="instanceFactory2"
factory-method="getConnection" />
</beans>
实例工厂实现方法三:
public final class ConnectionInstanceFactory3 {
public ConnectionInstanceFactory3() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
public Connection getConnection(String url, String username, String password) {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接获取失败!");
return null;
}
}
}
<?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="instanceFactory3" class="programmer.lp.factory.ConnectionInstanceFactory3"/>
<!-- 实例工厂 -->
<bean id="connection" scope="prototype"
factory-bean="instanceFactory3"
factory-method="getConnection">
<!-- 使用constructor-arg可以为方法传递参数 -->
<constructor-arg name="url" value="jdbc:mysql://127.0.0.1:3306/lp_resume" />
<constructor-arg name="username" value="root" />
<constructor-arg name="password" value="root" />
</bean>
</beans>
FactoryBean创建复杂bean实例:
public class ConnectionFactoryBean implements FactoryBean<Connection> {
private String url;
private String username;
private String password;
public ConnectionFactoryBean(String driverClassName, String url,
String username, String password) {
this.url = url;
this.username = username;
this.password = password;
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载失败!");
}
}
@Override
public Connection getObject() throws Exception {
System.out.println("ConnectionFactoryBean");
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接获取失败!");
return null;
}
}
@Override
public Class<?> getObjectType() {
return Connection.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
<!-- 通过实现FactoryBean接口创建的对象,
无论isSingleton为true还是false,都不会在容器启动时就被创建出来
而是在getBean时才被创建 -->
<bean id="connection" class="programmer.lp.factory.ConnectionFactoryBean">
<constructor-arg name="driverClassName" value="com.mysql.jdbc.Driver" />
<constructor-arg name="url" value="jdbc:mysql://127.0.0.1:3306/lp_resume" />
<constructor-arg name="username" value="root" />
<constructor-arg name="password" value="root" />
</bean>
@Test
public void test05() {
System.out.println(ctx.getBean("connection"));
}
实验06:通过继承实现bean配置信息的重用
<?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.xsd">
<bean id="human01" class="programmer.lp.domain.Human">
<property name="name" value="lpruoyu"/>
<property name="age" value="22"/>
<property name="birthday" value="2000/07/20"/>
<property name="car">
<bean class="programmer.lp.domain.Car" p:name="BMW" p:price="10"/>
</property>
<property name="books">
<list>
<bean class="programmer.lp.domain.Book">
<property name="name" value="红楼梦"/>
<property name="author" value="曹雪芹"/>
</bean>
<bean class="programmer.lp.domain.Book" p:name="三国演义" p:author="罗贯中"/>
<bean class="programmer.lp.domain.Book" p:name="西游记" p:author="吴承恩"/>
<bean class="programmer.lp.domain.Book" p:name="水浒传" p:author="施耐庵"/>
</list>
</property>
<property name="map">
<map>
<entry key="int" value="10" value-type="java.lang.Integer"/>
<entry key="string" value="coding programming"/>
<entry key="object">
<bean class="programmer.lp.domain.Student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">root123</prop>
</props>
</property>
</bean>
<!-- 通过继承实现bean配置信息的重用 -->
<!-- parent:只是指定当前bean的配置信息继承于哪一个bean(重用/借用一下parent的配置信息),
与那个bean并不是父子关系 -->
<!-- 使用parent属性时,class属性可以省略 -->
<!-- 假设这个Human只有名字和上面的Human不一样 -->
<bean id="human02" class="programmer.lp.domain.Human"
parent="human01">
<property name="name" value="LP"/>
</bean>
</beans>
实验07:通过abstract属性创建一个抽象的模板bean
<?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.xsd">
<!-- abstract="true":代表该bean的配置是抽象的,不能被获取实例,只能被其他bean作为parent -->
<bean id="humanAbstract" class="programmer.lp.domain.Human" abstract="true">
<property name="name" value="lpruoyu"/>
<property name="age" value="22"/>
<property name="birthday" value="2000/07/20"/>
</bean>
<bean id="humanInstance01" class="programmer.lp.domain.Human"
parent="humanAbstract">
<property name="name" value="LP"/>
</bean>
<bean id="humanInstance02"
parent="humanAbstract">
<property name="name" value="RUOYU"/>
</bean>
</beans>
/*
* 实验7:通过abstract属性创建一个模板bean
*/
@Test
public void test07() {
// System.out.println(ctx.getBean("humanAbstract")); // 出现异常
System.out.println(ctx.getBean("humanInstance01", Human.class));
System.out.println(ctx.getBean("humanInstance02", Human.class));
}
实验08:bean之间的依赖(更改bean的创建顺序)
<?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的创建顺序就是按照配置的顺序,从上往下 依次创建 -->
<!--
<bean id="person" class="programmer.lp.domain.Person"/>
<bean id="human" class="programmer.lp.domain.Human"/>
<bean id="student" class="programmer.lp.domain.Student"/>
-->
<!--
<bean id="human" class="programmer.lp.domain.Human"/>
<bean id="person" class="programmer.lp.domain.Person"/>
<bean id="student" class="programmer.lp.domain.Student"/>
-->
<!--
<bean id="student" class="programmer.lp.domain.Student"/>
<bean id="person" class="programmer.lp.domain.Person"/>
<bean id="human" class="programmer.lp.domain.Human"/>
-->
<!-- bean之间的依赖(只改变创建顺序) -->
<!--
<bean id="student" class="programmer.lp.domain.Student"
depends-on="person,human"/>
-->
<!--
<bean id="student" class="programmer.lp.domain.Student"
depends-on="human,person"/>
-->
<bean id="student" class="programmer.lp.domain.Student"
depends-on="person"/>
<bean id="person" class="programmer.lp.domain.Person"
depends-on="human"/>
<bean id="human" class="programmer.lp.domain.Human"/>
</beans>
实验09:测试bean的作用域、分别创建单实例和多实例的bean
<?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">
<!-- 实验09:测试bean的作用域、分别创建单实例和多实例的bean -->
<!--
bean的作用域:指的是bean在容器中是单实例还是多实例的
singleton:单实例
1)默认scope="singleton"
2)单实例的bean在容器创建启动完成之前就已经被创建好,
保存(管理)在容器中了。
3)同一个id、同一个容器,使用ctx.getBean()获取到的单实例bean实例永远都是同一个
prototype:多实例
1)容器创建、启动时不会去创建多实例bean的实例
2)容器每getBean一次,多实例bean就会被创建一次
session:在web环境下,同一个会话(session)创建同一个实例 【没用】
request:在web环境下,同一次请求(request)创建同一个实例 【没用】
-->
<bean id="human" class="programmer.lp.domain.Human"
scope="prototype"/>
<bean id="car" class="programmer.lp.domain.Car"/>
</beans>
/*
* 实验9:测试bean的作用域
* 分别创建单实例和多实例的bean★
*/
@Test
public void test09() {
System.out.println("容器启动完成");
ctx.getBean("human");
ctx.getBean("human");
Human h1 = ctx.getBean("human", Human.class);
Human h2 = ctx.getBean("human", Human.class);
ctx.getBean("car");
ctx.getBean("car");
Car c1 = ctx.getBean("car", Car.class);
Car c2 = ctx.getBean("car", Car.class);
System.out.println(h1 == h2);
System.out.println(c1 == c2);
}
参考
雷丰阳: 雷神的Spring、Spring MVC、MyBatis课程.
本文完,感谢您的关注支持!