Spring - AbstractApplicationContext close()方法不会破坏bean

时间:2013-02-25 02:42:24

标签: spring

总结:

一个。 spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="org.tutorial.spring" />
</beans>

湾SpringJdbcDemo.java

package org.tutorial.spring;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.tutorial.spring.dao.SpringJdbcDao;
import org.tutorial.spring.model.Circle;

public class SpringJdbcDemo {

public static void main(String[] args) {
    AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
    SpringJdbcDao dao = ctx.getBean("springJdbcDao", SpringJdbcDao.class);
    ctx.close();
    Circle circle = dao.getCircle(1);
    System.out.println(circle);
}

}

请注意,ctx.close()位于dao.getCircle()之前;

℃。 SpringJdbcDao.java

package org.tutorial.spring.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Component;
import org.tutorial.spring.model.Circle;

@Component
public class SpringJdbcDao {

public Circle getCircle(int circleId) {

    Connection conn = null;
    Circle circle = null;
    String derbyDriver = "org.apache.derby.jdbc.ClientDriver";

    try {
        Class.forName(derbyDriver).newInstance();
        conn = DriverManager.getConnection("jdbc:derby://localhost:1527/db");
        PreparedStatement ps = conn.prepareStatement("select * from circle");
        ResultSet rs = ps.executeQuery();

        if (rs.next()) {
            circle = new Circle(circleId, rs.getString("name"));
        }

        rs.close();
        ps.close();
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return circle;
}

}

输出:

2013年2月24日下午9:40:55 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 信息:在org.springframework.beans.factory.support.DefaultListableBeanFactory@45e7c8de中销毁单例:定义bean [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation。 internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springJdbcDao,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor];工厂等级制的根源 Circle [id = 1,name = First Circle

dao仍然在ctx.close()方法之后工作。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

  

dao仍然在ctx.close()方法之后工作。

为什么不起作用?一旦有了SpringJdbcDao的实例,它就不需要Spring的任何帮助来完成它的工作。有了上述内容,您基本上编写了一个非常复杂的SpringJdbcDao dao = new SpringJdbcDao()版本。