1.有三种实例化方式(无参构造,静态工厂,实例工厂),最常用的还是无参构造的方式

2.bean标签中常用的属性

      id/name:唯一标识名称,两者作用一样,只是name中可以包含特殊字符

      class:类的完全路径名

      scope:bean的作用域    singleton :单例 (默认)  prototype:每getBean创建一个新的 (常用) request    session   (不常用)

                                           一般都默认为singleton ,当整合struts2时,它的action是多例的,它要交给spring去管理,所以用多例

      init-method:当bean被载入容器中时调用                                                                  

      destroy-method:当bean从容器中删除时调用 (scope=prototype时有效)

                                (init-method和destroy-method在web容器中会自动调用,但是main函数或测试用例需要手动调用)

                              

3.bean的生命周期

    

package com.pheony.demo2;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Food implements BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean {

    public Food(){
        System.out.println("第一步,利用构造方法实例化啦");
    }

    private String name;        //食品名称
    public void setName(String name) {
        System.out.println("第二步,设置属性啦");
        this.name = name;
    }
    public String getName() {
        return name;
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("第三步,设置bean的名称啦");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("第四步,了解工厂信息啦");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("第六步,属性设置后啦");
    }

    public void setup(){
        System.out.println("第七步,初始化啦");
    }

    public void run(){
        System.out.println("第九步,执行自身业务方法啦");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("第十步,执行spring的销毁方法啦");
    }

    public void setdown(){
        System.out.println("第十一步,执行自身销毁方法啦");
    }

}
package com.pheony.demo2;

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

/**
 * 后处理bean,很关键,AOP就是利用这个实现的
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("第五步,初始化前方法来啦");
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("第八步,初始化后方法来啦");
        return o;
    }
}
<bean id="food" class="com.pheony.demo2.Food" scope="singleton" init-method="setup"  destroy-method="setdown">
        <!--在实体类中属性最好都加上set/get方法,DI根据set/get方法找到对应的属性-->
        <property name="name" value="香蕉"></property>
    </bean>
    <bean class="com.pheony.demo2.MyBeanPostProcessor"></bean>
package com.pheony.demo2;

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

public class TestFood {

    @Test
    public void  test1(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Food food = (Food)ac.getBean("food");

        food.run();

        ac.close();
    }
}