Spring基础总结

来源:互联网 发布:高达 知乎 编辑:程序博客网 时间:2024/05/29 17:40

Bean的定义:

1. 从本质上讲,Spring是一个大的工厂,而Bean则是工厂中的产品。

2. 对于XML中的<beans>基本标签,主要是设置的是bean的默认属性。


<bean>中的作用域,用scope设置,有五种类型

1. singleton:单利模式,只有一个对象

2. prototype:原型模式,每次同个getBean()获取都是一个新的对象

3. request::只有web中可以使用,每次使用request请求 就会产生一个新的实例,request完成后,实例也会消失。

4. session:只有web对象可以使用,使用session就会产生一个新的实例

5. global session:?


<bean>设置对象依赖,有两种方法:

//(1)<bean id="" class="" scope=""><property name="" value=""></property><property name="" ref=""></property></bean>//(2)<bean id="" class="" scope=""><contructor index="" value=""></property><contructor index="" ref=""></property></bean>

如果对象是同一个XML,使用ref,如果不是,使用local

<bean id="" class="" scope=""><property name=""><ref local=""/></property><property name="" ref=""></property></bean>

注入集合

<bean id="listtext" class="ok.ListTest" scope="singleton">        <property name="list">            <list>                <value>小薛</value>                 <value>高中</value>                  <value>初中</value>            </list>        </property>        <property name="scores">            <map>                <entry key="数学1" value="89" />                <entry key="数学2" value="89" />                <entry key="数学3" value="89" />                <entry key="数学4" value="89" />                            </map>        </property>        <property name="menscores">            <map>                <entry key="1" value-ref="men" />                <entry key-ref="" value-ref="">            </map>                    </property>        <property name="set">            <set>                <value>你好</value>                <bean class="ok.Person"/>                <ref local="men"/>            </set>        </property>        <property name="books">            <list>           <value>小薛</value>           <value>高中</value>           <value>初中</value>           <value>小薛</value>           <value>高中</value>           <value>初中</value>            </list>        </property>    </bean> 


组合属性设置

<bean id="a" class="">     <property name="foo.bar.x.y" value="xxx"></property>     </bean>
相当于设置 a.getFoo().getBar().getX().setY(xxx)

Spring中Bean和JavaBean的区别:

1. 用处不同,传统的JavaBean更多的作为值对象传递参数;Spring中的Bean无处不在,任何应用组建都称为bean

2. 写法不同:JavaBean作为值传递,一般都会设置set和get方法;而Bean一般只需要为设置值的注入设置set方法。

3 生命周期不同,传统的JavaBean作为值传递,没有生命周期,而Bean有Spring管理,有生命周期。

通常的情况下, 组件和组件之间的耦合,采用依赖注入管理,但是普通的JavaBean属性值,应该直接在代码中设置。



Bean的创建有三种

1. 构造器创建

2. 调用静态工厂方法创建

3. 调用非静态工厂方法创建


静态工厂方法创建和非静态

    <!-- 静态工厂 --><bean id="wowan" class="bean.FactoryClass" factory-method="getPeople">    <constructor-arg value="0"/></bean>    <bean id="man" class="bean.FactoryClass" factory-method="getPeople">    <constructor-arg value="2"/></bean>    <!-- 非静态工厂 --><bean id="factory" class="bean.FactoryClass"></bean><bean id="wowan1" factory-bean="factory" factory-method="getPeople1">    <constructor-arg value="0"/></bean>    <bean id="man1" factory-bean="factory" factory-method="getPeople1">    <constructor-arg value="2"/></bean>

抽象Bean和使用子Bean

Spring容器不会初始化抽象Bean(可以没有class属性),子bean可以继承抽象Bean的属性,也可以覆盖

<!-- 抽象bean -->    <bean id="ab" abstract="true">        <property name="age" value="10"></property>    </bean><bean id="ab1" parent="ab" class="bean.AbstractBean">    <property name="name" value="ab1"></property></bean><bean id="ab2" parent="ab" class="bean.AbstractBean">    <property name="name" value="ab2"></property></bean><bean id="ab3" parent="ab" class="bean.AbstractBean">    <property name="name" value="ab3"></property>    <property name="age" value="20"></property></bean>


容器中的工厂Bean

上面的工厂Bean是标准工厂Bean,而这个是一种Spring特殊的Bean,需要继承FactoryBean接口,需要实现下面三个方法:

1. T getBean: 实现该方法负责返回该工厂Bean生成的Java实例。

2. Class<?> getObjectType();返回该工厂类实现的实例的实现类

3. boolean isSingleton();

实现该接口的Bean无法像正常的Bean使用,和正常一样设置xml,但是客户端调用的时候,返回的是该工厂的产品,如果需要返回该工厂的实例,需要在id前面加一个&

public class PersonFactory implements FactoryBean{People p = null;@Overridepublic People getObject() throws Exception{if(p==null)p = new People;return p;}@Overridepublic Class getObjectType(){return People.class;}@Overridepublic boolean isSingleton(){return true;}}

//返回工厂产品实例ctx.getBean("people");//返回工厂实例ctx.getBean("&people");

获取Bean本身的ID

如果已经获得了实例,但是像获取该实例的id,这就需要这个类实现BeanNameAware接口,我们需要封装一个变量,然后实现接口中的方法,setBeanName方法。

public class Person implements BeanNameAware{private String beanName;public void setBeanName(String beanName){//这个就是Idthis.beanName = beanName;}}


让Bean获取Spring容器

需要让Bean实现一个ApplicationContextAware

public class Person implements ApplicationContextAware{private ApplicationContext context;@Overridepublic void setApplicationContext(ApplicationContext context) throws BeansException{// TODO Auto-generated method stubthis.context =context;}}


强制初始化:

Spring有个规则,先初始化主调的Bean, 然后初始化依赖Bean。在极端情况下,如果主调的Bean没有初始化,那么就会出现异常。所以就需要强制初始化。

<bean id="peo" class=""  depends-on="people"></bean>    <bean id="people" class="" ></bean>


注入其他Bean的属性值:

PropertyPathFactoryBean用来获取目标bean的属性值(实际上就是使用get方法的返回值),获得的值可以注入给其他对象,也可以直接定义为新的Bean

<bean id="person" class="">    <property name="age" value="30"></property>    <property name="son">        <bean class=" ">            <property name="age" value="11"></property>        </bean>    </property></bean>    <bean id="son" class="">    <property name="age">        <bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"></bean>    </property></bean>

也可以指定成新的bean,需要指定PropertyPathFactoryBean的两个属性

1. targetBeanName: 用于指定目标Bean, 确定获取哪个Bean的属性值。

2. propertyPath::用于指定属性,确定目标Bean的那一个属性值。

<bean id="person" class="">    <property name="age" value="30"></property>    <property name="son">        <bean class=" ">            <property name="age" value="11"></property>        </bean>    </property></bean>    <bean id="son" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">    <property name="targetBeanName" value="person"></property>    <property name="propertyPath" value="son"></property></bean>


注入其他Bean的属性值:

通过FieldRetrievingFactoryBean类,可以将其他Bean的Field值,注入给其他的Bean

<bean id="son" class="">    <property name="age">        <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/> <!--如果是非静态的属性,则需要使用id来定位-->    </property></bean>

也可以指定成新的bean,需要指定FieldRetrievingFactoryBean的两个属性

1. targetClass或者targetObject: 指定目标类或者目标对象,如果是静态,则是类,否则这是对象

2. targetField::用于指定目标Field。

<bean id="integer" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">    <property name="targetClass" value="java.sql.Connection"></property>    <property name="targetField" value="TRANSACTION_SERIALIZABLE"></property></bean>


注入其他Bean的方法返回

通过MethInvokingFactoryBean,可以指定方法返回值注入生成的Bean



也可以直接返回到一个新的bean中



如果想传入参数 则使用argument

<bean id="teacher"class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">       <property name="staticMethod">           <value>com.teacher.Teacher.setStudentDao</value>       </property>       <property name="arguments">           <list>              <ref bean="studentDao" />           </list>       </property>    </bean>    <bean id="studentDao" class="com.teacher.StudentDao">    </bean>
方法如下
public class Teacher {    private static StudentDao studentDao;    public static String setStudentDao(StudentDao stuDao) {        Teacher.studentDao = stuDao;        return "hello world";    }}

Bean生命周期:

Bean全部属性执行之后指定的行为,有两种

1. 使用init-method属性。

2. 继承InitalizingBean接口。这个优先

Bean销毁之前的行为,有两种

1. 使用destroy-method:

2. 继承DisposableBean接口:这个优先

Spring有良好的扩展性,可以通过Bean的后处理器扩展,可以实现BeanPostProcessor接口,有两个方法

1. Object postProcessBeforeIniialization(Object bean, String name)

2. Object postProcessAfterIniialization(Object bean, String name)

其中,bean是进行后处理的实例,name是该实例的名字。





0 0