Spring框架花式创建Bean的n种方法(小结)

常用的从容器中获取bean实例使用这样的方式:

@Test
	public void test() {
		Persion p = (Persion) ioc.getBean("p1");
		System.out.println(p);
	}

常用的在容器中配置组件使用这样的方式:

	
		
		
		
		
	

下面的实验介绍一些Spring容器中注册组件对象的其他方法。

实验1:根据bean的类型从ioc容器中获取实例

@Test
	public void test01() {
		Persion p = ioc.getBean(Persion.class);
		System.out.println(p);
	}

这种方法查找的好处是不需要类型转换,但是如果ioc容器中要找的bean有多个,使用这种方法查找就会报错。可以改用下面的方式:

	@Test
	public void test01() {
		Persion p = ioc.getBean("p1", Persion.class);
		System.out.println(p);
	}

实验2:通过有参构造器为bean的属性赋值

需要提前在bean中添加有参构造器,才能进行下面的测试。

	
		
		
		
		
	

使用这种有参构造器为bean的属性赋值,可以省略name,但是value的顺序必须与bean中的顺序一致。(若再使用index和type进行索引,可以不按顺序)

通过名称空间为bean赋值:

添加p命名空间标签头:xmlns:p=“http://www.springframework.org/schema/p”

实验3:为各种属性赋值

引用类型、集合类型、级联类型。

如题,给出一个赋值的Bean对象,为其在容器中注册。此时所有复杂的赋值都在property标签体内。

public class Persion {
	private String name;
	private String gender;
	private Integer age;
	private String email;

	private Car car;

	private List book;

	private Map maps;

	private Properties properties;
	//省略setter与getter方法
}

普通属性赋值:


		
		
		
		
			
		

引用类型赋值:

	
		
		
		
		
			
				
				
				
			
		
	
	
	
		
		
		
	

集合类型赋值:

list


		
			
				
				
			
		
	
	
	
		
		
	

map


			
				
				
				
				
					
						
						
						
					
				
			
		

properties

	
			
				root
				123456
			
		

实验4:通过继承实现bean配置信息的重用

下面的代码中p4继承了p3,需要改动的属性在property标签中修改即可,其余的全部原样继承。


		
		
		
		
	

	
		
	

实验5:单实例singleton和多实例prototype

单实例singleton 多实例prototype
容器启动时创建好对象并保存在容器中 获取Bean时才会创建这个对象
②调用初始化方法 ②调用初始化方法
容器关闭时调用销毁方法 容器销毁时不调用销毁方法
任何时间获取都是获取之前创建好的那个对象 每次获取都会创建一个新的对象

详情可参照博客:通过工厂创建Bean的三种方式

实验6:创建带有生命周期的Bean

ioc容器中注册的组件:

  • 单实例:容器启动的时候就会创建好,容器关闭也会销毁创建的bean。
  • 多实例:获取的时候才创建。

可以为bean自定义一些生命周期方法,spring在创建或销毁的时候就会调用指定的方法。

(1)单实例Singleton测试

在Book类中创建方法:

package com.gql.bean;
public class Book {
	private String bookName;
	private String author;

	public void myInit() {
		System.out.println("Book的初始化方法...");
	}

	public void myDestory() {
		System.out.println("Book的销毁方法...");
	}

	public Book() {
		super();
		// TODO Auto-generated constructor stub
		System.out.println("Book创建...");
	}
}

在xml中注册组件:

测试:

在ApplicationContext中没有close方法,需要将容器类型转换为ConfigurableApplicationContext 。

public class IoCTest {
	ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("ApplicationContext.xml");

	@Test
	public void test10() {
		System.out.println("容器关闭了");
		ioc.close();
	}
}

Spring框架花式创建Bean的n种方法(小结)_第1张图片

(2)多实例prototype测试

只需要改变xml中注册组件为多实例:

仍然使用上面的方法进行测试:

Spring框架花式创建Bean的n种方法(小结)_第2张图片

可以看到容器的创建销毁一系列都没有进行,这是因为多实例在获取bean的时候才创建实例。

多实例测试中增加获取bean:

	@Test
	public void test10() {
		Object bean = ioc.getBean("book01");
		System.out.println(bean);
		System.out.println("容器关闭了");
		ioc.close();
	}

Spring框架花式创建Bean的n种方法(小结)_第3张图片

测试结果中,成功创建了实例,但是容器关闭并没有销毁Bean。

实验7:测试Bean的后置处理器

后置处理器有一点代理对象的意思,使用后置处理器,Bean的生命周期变成下面的样子:

容器启动—>后置处理器Before—>初始化方法—>后置处理器After—>容器关闭(调用销毁方法)

不管有没有初始化方法,后置处理器都会默认其有,继续工作。

后置处理器:

package com.gql.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
 * 1.编写后置处理器 
 * 2.将后置处理器注册在配置文件
 * @author guoqianliang
 *
 */
public class MyBeanPostProcessor implements BeanPostProcessor {

	/**
	 * 初始化前调用
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("Before:" + beanName + "将要调用初始化方法了..." + bean);
		return bean;
	}

	/**
	 * 初始化后调用
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("After:" + beanName + "初始化方法调用完了" + bean);
		return bean;
	}
}

将后置处理器注册在配置文件:



	
	
	
	
	

测试:

@Test
	public void test11() {
		Object bean = ioc.getBean("book01");
		System.out.println("容器关闭了");
		ioc.close();
	}

Spring框架花式创建Bean的n种方法(小结)_第4张图片

实验8:引用外部文件

在Spring中bean默认都是单实例的,而数据库作为单实例是最好不过的,一个项目就是一个连接池,连接池里面管理很多连接,连接是直接从连接池中拿。可以让Spring帮我们创建连接池对象,管理连接池。

注册连接池第一代

在配置中注册连接池:


		
		
		
		
	

测试:

@Test
	public void test12() throws SQLException {
		// 从容器中拿到连接
		// DataSource bean = (DataSource) ioc.getBean("dataSource");
		DataSource bean2 = ioc.getBean(DataSource.class);
		System.out.println(bean2.getConnection());
	}

成功获取到了这个连接:

注册连接池第二代

在config包下创建一个dbconfig.properties用来保存数据库连接信息。

为了防止配置文件中的key与Spring自己的关键字冲突。可以为key加一个前缀,业内通用的做法是使用jabc.xxx

jdbc.username:root
jdbc.password:Hudie
jdbc.jdbcUrl:jdbc:mysql://localhost:3306/test
jdbc.driverClass:com.mysql.jdbc.Driver

注册数据库连接池:

	
		
		
		
		
	

测试:

@Test
	public void test12() throws SQLException {
		DataSource bean2 = ioc.getBean(DataSource.class);
		System.out.println(bean2.getConnection());
	}

到此这篇关于Spring框架花式创建Bean的n种方法的文章就介绍到这了,更多相关Spring 创建Bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Spring框架花式创建Bean的n种方法(小结))