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

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

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方法---------------    (销毁方法)

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

 

相关文章:

  • 2022-01-02
猜你喜欢
  • 2021-10-12
  • 2021-06-30
  • 2021-05-17
  • 2021-10-30
  • 2021-10-14
  • 2020-07-11
相关资源
相似解决方案