Spring中bean生命周期(浅显易懂)

1、网上下的图,以供参考

2、
/**
 * 创建一个测试类的UserService
 * @author lion
 *
 */

public class UserService {
    private String username;//用户名
    private String password;//密码
    /**
     * 以下方法为了方便测试、bean生命周期过程的执行顺序,事先打乱方法输出顺序
     */
    public void destroy(){
        System.out.println("---destory方法 -----");
    }
    public void init(){
        System.out.println("---初始化方法 -----");
    }
    public UserService(){
        System.out.println("---构造方法 -----");
    }
    public void setUsername(String username) {
        this.username = username;
        System.out.println("---setName方法-----");
    }
 
}

 

/**
 * 创建一个 TestUserServlet类
 */

@WebServlet("/TestUserServlet")
public class TestUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public TestUserServlet() {
        super();    
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
         UserService userService =(UserService) wac.getBean("userservice");//调用userService这个bean,查看打印输
    }
}
 

3、配置文件

<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 " >
    <!--注意测试生命周期的时候,需要配置init-method,destroy-method-->
    <bean id="userservice" class="cn.web.neusoft.service.UserService" init-method="init" destroy-method="destroy">
            <property name="username" value="pyc" /> 
            <property name="password" value="123" /> 
    </bean>
   
</beans>
 

打印输出结果:

------参考输出信息,可以看出bean的生命周期几个重要方法的执行顺序---------------------------

--------构造方法---------

---------setName方法-----------------

---------init方法----------------------(由init-method属性指定)

//如果有功能方法 如login(),输出在初始化方法init()方法后

---------destory方法---------------    (销毁方法)

以上大家可以测试一下,我试过没问题。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值