Spring中bean⽤法详解
获取bean的⽅法
1.从ApplicationContex应⽤上下⽂容器中获取bean和从bean⼯⼚容器中获取bean
具体案例:
从ApplicationContext中取bean
ApplicationContextac=new ClassPathXmlApplicationContext("com/hsp/l");
当我们去实例化l,该⽂件中配置的bean被实例(该bean scope是singleton)从bean中取出 ,如果我们使⽤beanfactory去获取bean,当你只是实例化该容器, 那么容器的bean不被实例化,只有当你去使⽤getBean某个bean时,才会实时的创建.
BeanFactory  factory = new XmlBeanFactory(newClassPathResource("com/hsp/l"));
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("⽂件路径l/ l");
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
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 t.ApplicationContext;
import t.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;
}
spring ioc注解
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
import org.springframework.beans.BeansException;
import org.springframework.fig.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 ><beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:mvc="/schema/mvc" xmlns:context="/schema/context"
xmlns:aop="/schema/aop" xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.2.xsd
/schema/mvc
/schema/mvc/spring-mvc-3.2.xsd
/schema/context
/schema/context/spring-context-3.2.xsd
/schema/aop
/schema/aop/spring-aop-3.2.xsd
/schema/tx
/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.编写测试⽂件

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。