1.Bean的配置
如果把spring看作一个工厂,那么spring容器中的bean就是该工厂的产品,想要使用这个工厂来进行对bean的管理,就需要在配置文件中告诉它需要哪些bean,以及使用什么方式将这些bean装配到一起。
bean的本质就是Java中的类,而spring中的bean就是对实体类的引用,进行生产java类对象,从而进行生产和管理bean。
| 属性或子元素名称 | 描述 |
|---|---|
| id | 是bean的唯一标识,Spring容器对bean的配置管理,都通过该属性完成 |
| name | Spring容器亦可以通过此属性对bean进行配置管理,name可以为bean指定多个名称 |
| class | 指定啦bean的具体实现类,需使用类的全限类名 |
| … | … |
在配置文件中,一个bean通常只需要定义id或者name,与class两个属性即可。
定义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 id="bean1" class="com.itheima.Bean1" />
<bean name="bean2" class="com.itheima.Bean2" />
</beans>
2.Bean的实类化
在spring中,要想使用容器中的bean,便要实类化Bean,其有三种方式,构造器实类化(常用),静态工厂方式实类化,实类工厂方式实类化
2.1构造器实类化
public class Bean1 {
public Bean1(){
}
}
beans1.xml
<?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="bean1" class="cn.ssm.bean.Bean1"/>
</beans>
测试:
public class BeanTest1 {
public static void main(String[] args){
String xmlpath="cn//ssm//bean//beans1.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
Bean1 bean1=(Bean1) applicationContext.getBean("bean1");
System.out.print(bean1);
}
}
2.2静态工厂方式实类化
public class Bean2{
}
静态工厂类
public class MyBean2Factory {
public static Bean2 createBean(){
return new Bean2();
}
}
bean2.xml
<?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="bean2" class="cn.ssm.beanfactory.MyBean2Factory" factory-method="createBean"/>
</beans>
bean2.test
public class BeanTest2 {
public static void main(String[] args){
String xmlpath="cn//ssm//beanfactory//beans1.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
Bean2 bean2=(Bean2) applicationContext.getBean("bean2");
System.out.print(bean2);
}
}
2.3实类工厂方式实类化
其采用直接创建bean实类的方式,同时在配置文件中,通过factory-bean属性指向配置的实类工厂,使用factory-method属性确定使用工厂中的哪个方法
bean3.java
public class Bean3 {
}
mybean3factory
public class MyBean3Factory {
public MyBean3Factory(){
System.out.println("bean工厂实例化中");
}
public Bean3 createBean(){
return new Bean3();
}
}
bean3.xml
public class MyBean3Factory {
public MyBean3Factory(){
System.out.println("bean工厂实例化中");
}
public Bean3 createBean(){
return new Bean3();
}
}
Bean3Test.java
public class Bean3Test {
public static void main(String[] args){
String xmlpath="cn//ssm//bean3//beans3.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
Bean3 bean3=(Bean3) applicationContext.getBean("bean3");
System.out.print(bean3);
}
}
3.Bean的作用域
种类:
| 作用域名称 | 介绍 |
|---|---|
| singleton(单例) | 使用singleton定义的bean在spring容器中将只有一个实例,就是说,无论有多少bean引用,始终指向同一个对象。也是spring默认的作用域。 |
| prototype(原型) | 每次通过spring容器获取的prototype定义的bean时,容器都将创建一个新的bean实例 |
| request | 再一次http请求中,容器会返回该bean的同一个实例,对不同的http请求则会产生一个新的bean,且该bean仅对当前http request有效 |
| session | 在一个http session中,容器会返回该bean的同一个实例,对不同的http请求则会产生一个新的bean,而且该bean仅在当前http session内有效 |
| globalSession | 在一个全局HTTP Session中,容器会返回该bean的同一个实例,仅在使用portlet上下文时有效 |
| appliction | 为每一个ServletContext对象创建一个实例,仅在web相关的ApplictionContext时有效 |
| websocket | 为每个websocket创建一个实例,仅在web相关的ApplictionContext中生效 |
2266

被折叠的 条评论
为什么被折叠?



