您的位置:首页 > 编程语言 > Java开发

Spring中bean用法详解

2016-06-28 22:57 597 查看

获取bean的方法

1.从ApplicationContex应用上下文容器中获取bean和从bean工厂容器中获取bean
具体案例:
从ApplicationContext中取bean
ApplicationContextac=new ClassPathXmlApplicationContext("com/hsp/ioc/beans.xml");
当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是singleton)从bean中取出 ,如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.
BeanFactory factory = new XmlBeanFactory(newClassPathResource("com/hsp/ioc/beans.xml"));
factory.getBean("student");

2.结论
a.如果使用ApplicationContext,则配置的bean如果是 singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)
b.如果是BeanFactory ,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度)
c.规定: 一般没有特殊要求,应当使用ApplicatioContext完成(90%)

bean 的 配置细节



配置文件
<!-- 配置bean的scope生命周期-->
<bean id="student" scope="singleton" class="com.cloud.ioc.Student">
<propertyname="name" value="张三"/>
</bean>
测试代码
//获取两个student
Students1=(Student) ac.getBean("student");
Students2=(Student) ac.getBean("student");
System.out.println(s1+""+s2);

这里singleton生命周期理解为:在配置文件中,Student类只配置一个bean,也就只对应一个对象,Student s1=(Student) ac.getBean("student");所以这种方式创建的对象是同一个。
request
session
global-session
是在web开发中才有意义.

种获取ApplicationContext 对象

1. ClassPathXmlApplicationContext->通过类路径
2. FileSystemXmlApplicationContext->通过文件路径
举例:
ApplicationContext ac=newFileSystemXmlApplicationContext("文件路径beans.xml/ applicationContext.xml");
3. XmlWebApplicationContext

使用Bean基本流程

把一个bean纳入到Spring IoC容器之中,这个bean的生命周期就会交由容器进行管理
1.Bean的建立

由BeanFactory读取Bean定义文件,并生成各个实例。

2.Setter注入

执行Bean的属性依赖注入。

3.BeanNameAware的setBeanName()

如果Bean类实现了org.springframework.beans.factory.BeanNameAware接口,则执行其setBeanName()方法。

4.BeanFactoryAware的setBeanFactory()

如果Bean类实现了org.springframework.beans.factory.BeanFactoryAware接口,则执行其setBeanFactory()方法。

5.BeanPostProcessors的processBeforeInitialization()

容器中如果有实现org.springframework.beans.factory.BeanPostProcessors接口的实例,则任何Bean在初始化之前都会执行这个实例的processBeforeInitialization()方法。

6.InitializingBean的afterPropertiesSet()

如果Bean类实现了org.springframework.beans.factory.InitializingBean接口,则执行其afterPropertiesSet()方法。

7.Bean定义文件中定义init-method

在Bean定义文件中使用“init-method”属性设定方法名称,如下:

<bean id="demoBean" class="com.yangsq.bean.DemoBean"init-method="initMethod">

.......

</bean>

这时会执行initMethod()方法,注意,这个方法是不带参数的。

8.BeanPostProcessors的processAfterInitialization()

容器中如果有实现org.springframework.beans.factory.BeanPostProcessors接口的实例,则任何Bean在初始化之前都会执行这个实例的processAfterInitialization()方法。

9.DisposableBean的destroy()

在容器关闭时,如果Bean类实现了org.springframework.beans.factory.DisposableBean接口,则执行它的destroy()方法。

10.Bean定义文件中定义destroy-method
在容器关闭时,可以在Bean定义文件中使用“destory-method”定义的方法

使用案例

1.编写PersonService.java
<span style="font-size:18px;">package com.cloud.beanlife;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean{
public PersonService(){
System.out.println("对象实例化!");
}
public PersonService(String abc){
System.out.println("对象实例化!");
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("PersonService.setName()方法被调用");
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void sayhello(){
System.out.println("hello:"+name+"的年龄是"+age);
}
//该方法的arg0表示正在被实例化的bean的id 是多少
@Override
public void setBeanName(String arg0) {
System.out.println("PersonService.setBeanName():"+arg0);
}
//该方法传递BeanFactory
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("PersonService.setBeanFactory():"+arg0);
}
//该方法可以传递ApplicationContext
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("PersonService.setApplicationContext():"+arg0);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("PersonService.afterPropertiesSet()");
}
//可以通过注解的方式配置<bean destroy-method="mydestory">销毁方法
@PreDestroy
public void mydestroy(){
System.out.println("生命周期结束,释放各种资源");
}
}
</span>
2.编写BeanPostProcess.java

<span style="font-size:18px;">package com.cloud.beanlife;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class BeanPostProcess implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessAfterInitialization 函数被调用");
System.out.println(arg0+"被创建的 时间是"+new java.util.Date());
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessBeforeInitialization 函数被调用");
return arg0;
}
}
</span>
3.编写配置文件
<span style="font-size:18px;"><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<bean id="personService" destroy-method="mydestroy" class="com.cloud.beanlife.PersonService">
<property name="name" value="Spring"/>
<property name="age">
<value>25</value>
</property>
</bean>
<bean id="personService2" class="com.cloud.beanlife.PersonService">
<property name="name" value="小明"/>
</bean>
<!-- 配置自己的处理器,类似于过滤器 -->
<bean id="myBeanPostProcessor" class="com.cloud.beanlife.BeanPostProcess">
</bean>
</beans></span>

4.编写测试文件
<span style="font-size:18px;">package com.cloud.beanlife;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
/**
* 测试大小写字母的转换
*/
public static void main(String[] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService ps = (PersonService) ac.getBean("personService");
ps.sayhello();
}
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: