spring(一)---bean的学习上篇

来源:互联网 发布:淘宝巴拉巴拉羽绒服 编辑:程序博客网 时间:2024/06/07 15:33

一、IOC容器装配bean(xml配置)

1、bean的三种实例化

a、构造器实例化对象

package top.einino.bean;

public class Blog {

}

b、工厂调用静态方法创建实例

package top.einino.bean;

public class FactoryBlog {

public Blog getBlog(){

return new Blog();

}

}

c、工厂调用实例方法创建实例

package top.einino.bean;

public class StaticFactoryBlog {

public static Blog getBlog(){

return new Blog();

}

}

applicationContext.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- 在配置文件 引入约束 xsd schema 约束 -->
<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">

<!-- bean的三种实例方法 -->
<!-- 使用构造器(无参)构造方法 -->
<bean id="blog" class="top.einino.bean.Blog"></bean>
<!-- 使用工厂调用静态方法创建实例 -->
<bean id="staticFactoryBlog" class="top.einino.bean.StaticFactoryBlog" factory-method="getBlog"></bean>
<!-- 使用工厂调用实例方法创建实例 -->
<bean id="factory" class="top.einino.bean.FactoryBlog"></bean>
<bean id="factoryBlog" factory-bean="factory" factory-method="getBlog"></bean>
</beans>

测试:

package top.einino.junit;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import top.einino.bean.Blog;

public class TestBean {

private ApplicationContext applicationContext;
@Before
public void init(){

applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

}
//测试创建blog实例
@Test
public void testBean(){

//测试构造方法创建实例
Blog blog = (Blog) applicationContext.getBean("blog");
System.out.println(blog);
//工厂调用静态方法创建实例
Blog staticFactoryBlog = (Blog)applicationContext.getBean("staticFactoryBlog");
System.out.println(staticFactoryBlog);
//工厂调用实例方法创建实例
Blog factoryBlog = (Blog)applicationContext.getBean("factoryBlog");
System.out.println(factoryBlog);

}

}

 

2、bean的生命周期

a、初始化

b、销毁

applicationContext配置如下

<!-- bean的生命周期初始化和销毁  定义bean的初始化方法和销毁方法、当配置文件被加载后,就会自动初始化bean-->
<bean id="blogLife" class="top.einino.life.BlogLife" init-method="setup" destroy-method="teardown"></bean>

代码类:

package top.einino.life;

public class BlogLife {

public BlogLife(){
System.out.println("Blog构造");
}

public void setup(){
System.out.println("Blog初始化");
}

public void teardown(){
System.out.println("Blog销毁");
}
}

测试方法:

//测试bean的生命周期
@Test
public void testBeanLife(){

//当配置文件被加载后,就会进行初始化
//  BlogLife blogLife = (BlogLife) applicationContext.getBean("blogLife");
//手动调用application容器销毁的方法,在tomcat中会自动调用容器销毁
((AbstractApplicationContext) applicationContext).close();
}

小结:bean的初始化方法是在构造方法之后进行,主要是用来执行对象复杂的初始化过程,bean的销毁发生在窗容器销毁之后。

3、bean的作用域

五种作用域:

singleton:在一个BeanFactory对象中,引用唯一的一个目标实例

Prototype (多例): 每次通过工厂执行getBean时,返回不同实例对象

Request (请求范围) : 创建对象保存在request范围,如果request销毁,对象销毁

Session (会话范围): 创建对象保存Session中, 如果session销毁,对象销毁

* globalSession (全局会话 ) :分布式系统,全局会话的概念, 一次登录,应用多个系统

这里只讨论主要的前两种作用域

a、singleton

<!-- bean单例 默认为singleton,所以不配置也是可以的-->
<bean id="blogSingleton" class="top.einino.scope.BlogSingleton" scope="singleton"></bean>

package top.einino.scope;

public class BlogSingleton {

}

b、prototype

<!-- bean多例 -->
<bean id="blogPrototype" class="top.einino.scope.BlogPrototype" scope="prototype"></bean>

public class BlogPrototype {

}

测试:

//测试bean的作用域
@Test
public void testBeanScope(){
//测试多例
BlogPrototype blogPrototype1 = (BlogPrototype) applicationContext.getBean("blogPrototype");
BlogPrototype blogPrototype2 = (BlogPrototype) applicationContext.getBean("blogPrototype");
System.out.println(blogPrototype1==blogPrototype2);//false
//测试单例
BlogSingleton blogSingleton1 = (BlogSingleton) applicationContext.getBean("blogSingleton");
BlogSingleton blogSingleton2 = (BlogSingleton) applicationContext.getBean("blogSingleton");
System.out.println(blogSingleton1==blogSingleton2);//true
}


4、bean的后处理器

a、自定义后处理器实现BeanPostFactory,并实现其中两个方法

package top.einino.post;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor{

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {

if(beanName.equals("blogLife")){
System.out.println("blogLife初始化前拦截");
}
return bean;

}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {

if(beanName.equals("blogLife")){
System.out.println("blogLife初始后前拦截");
}
return bean;

}

}

b、测试后处理器

当blogLife进行setup初始化前会被后处理器拦截,当进行setup初始化还会被后处理器进行拦截

public void testBeanLife(){

//当配置文件被加载后,就会进行初始化
//  BlogLife blogLife = (BlogLife) applicationContext.getBean("blogLife");
//手动调用application容器销毁的方法,在tomcat中会自动调用容器销毁
((AbstractApplicationContext) applicationContext).close();
}


5、bean的两种依赖注入

a、构造参数属性注入

<!-- bean的构造方法属性注入 -->
<bean id="person" class="top.einino.di.Person">
<constructor-arg index="0"type="java.lang.String" value="郑先生"></constructor-arg>
<constructor-arg index="1" type="int" value="13"></constructor-arg>
</bean>

package top.einino.di;

public class Person {

private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}

b、setter方法属性注入

<!-- bean的setter方法属性注入 -->
<bean id="person1" class="top.einino.di.Person1">
<property name="name" value="郑先生"></property>
<property name="age" value="1"></property>
<!-- 注入复杂对象 -->
<property name="person" ref="person"></property>
</bean>

package top.einino.di;

public class Person1 {

private String name;
private int age;
private Person person;

public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
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;
}

}

6、bean属性注入深入

a、利用p命名空间注入属性

<!-- 利用p命名空间进行属性注入 -->
<bean id="person2" class="top.einino.di.Person1"
p:name="郑先生" p:age="12" p:person-ref="person"></bean>

b、集合类型属性注入
<!-- bean的集合属性注入 -->
<bean id="collectionBean" class="top.einino.di.CollectionBean">
<property name="list">
<!-- list集合注入,如果注入的是复杂对象,则引用ref -->
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<!-- set集合注入 -->
<property name="set">
<set>
<value>1</value>
<value>3</value>
<value>2</value>
</set>
</property>
<!-- map集合注入 -->
<property name="map">
<map>
<entry key="age" value="1"></entry>
<entry key="number" value="2"></entry>
<entry key="id" value="3"></entry>
</map>
</property>
<!-- properties注入 -->
<property name="properties">
<props>
<prop key="driver">com.jdbc.mysql.Driver</prop>
<prop key="url">jdbc:mysql:///spring_bean</prop>
</props>
</property>
</bean>

代码:

package top.einino.di;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionBean {
private List<String> list;
private Set<Integer> set;
private Map<String, Integer> map;
private Properties properties;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<Integer> getSet() {
return set;
}
public void setSet(Set<Integer> set) {
this.set = set;
}
public Map<String, Integer> getMap() {
return map;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}

}

属性注入测试方法:
//测试bean的属性注入
@Test
public void testBeanField(){

//测试构造参数注入属性
Person person = (Person) applicationContext.getBean("person");
System.out.println(person.getName());
System.out.println(person.getAge());
//测试setter方法注入属性
Person1 person1 = (Person1) applicationContext.getBean("person1");
System.out.println(person1.getName());
System.out.println(person1.getAge());
System.out.println(person1.getPerson().getName());
System.out.println(person1.getPerson().getAge());
//测试p命名空间注入属性
Person1 person2 = (Person1) applicationContext.getBean("person2");
System.out.println(person2.getName());
System.out.println(person2.getAge());
System.out.println(person2.getPerson().getName());
System.out.println(person2.getPerson().getAge());
//测试集合
CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
//测试list
List<String> list = collectionBean.getList();
for(String s : list){
System.out.println(s);
}
//测试set
Set<Integer> set = collectionBean.getSet();
for(Integer s : set){
System.out.println(s);
}
//测试map
Map<String, Integer> map = collectionBean.getMap();
Set<Entry<String, Integer>> entrySet = map.entrySet();
for(Entry<String,Integer> entry : entrySet){
System.out.println(entry.getKey()+":"+entry.getValue());
}
//测试properties
Properties properties = collectionBean.getProperties();
System.out.println(properties.getProperty("driver"));
System.out.println(properties.getProperty("url"));

}


二、小结

该博文介绍了bean的三种实例构造方法、bean的生命周期、作用域、后处理器、两种依赖注入。



如果有疑问或者对该博文有何看法或建议或有问题的,欢迎评论,恳请指正!

0 0
原创粉丝点击