wurenjian 2010-01-19 10:53
浏览 792
已采纳

在Main方法中获取spring Bean,调用Bean方法后,程序不能终止?

Main方法如下:
[code="java"]
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"ApplicationContext-jotm.xml"});
BeanFactory factory = (BeanFactory) context;

    UserService userService = (UserService)factory.getBean("userService");
    if(args[0].equals("help") || args[0].equals("?")){
        System.out.println("-------------------------");
        System.out.println("equip   设备同步");
        System.out.println("-------------------------");
    }
    if(args[0].equals("equip")){
        userService.synchronyEquipment();
    }

}

[/code]

测试里面调用userService.synchronyEquipment(),可以正常终止。Main方法操作执行了,但程序不能终止。
我使用jotm操作两个数据库。我想是不是spring没有释放数据库连接?
不知道有人遇到过这种情况没有?

ApplicationContext-jotm.xml
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>

<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="userTransaction" ref="jotm" />
</bean>

<bean id="eamds" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
    <property name="dataSource">
        <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
            <property name="transactionManager" ref="jotm" />
            <property name="driverName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost/d7i" />
        </bean>
    </property>
    <property name="user" value="root" />
    <property name="password" value="root"/>
</bean>

<bean id="zhjds" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
    <property name="dataSource">
        <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
            <property name="transactionManager" ref="jotm" />
            <!-- property name="driverName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@10.10.150.216:1521:eampoc" /-->
            <property name="driverName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost/eampoc"/>
        </bean>
    </property>
    <property name="user" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="eamds" />
</bean>

<bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="zhjds" />
</bean>

<bean id="EAMDao" class="com.hgsoft.EAMDAO">
    <property name="jdbcTemplate">
        <ref bean="template1"></ref>
    </property>
    <property name="fetchSize" value="1"/>
</bean>

<bean id="ZHJDao" class="com.hgsoft.ZHJDAO">
    <property name="jdbcTemplate">
        <ref bean="template2"></ref>
    </property>
</bean>

<bean id="userService" class="com.hgsoft.UserServiceImpl">
    <property name="EAMDao" ref="EAMDao"/>
    <property name="executeService" ref="executeService"/>
    <property name="yaml" ref="yaml"/>
    <property name="yamlStr">
        <value>equipment:
           count: select count(*) from new_object_collection t where t.issended='0'
           query: select t.equip_code,t.equip_name,t.sealflag from new_object_collection t where t.issended='0'
           update: update new_object_collection set issended='1' where equip_code=?
           insert: insert into eam_ncinv_equip (EQUIP_CODE,EQUIP_NAME,SEALFLAG ) values (?,?,?)</value>
    </property>
</bean>

<bean id="yaml" class="org.yaml.snakeyaml.Yaml" scope="prototype" /> 

<bean id="executeServiceTarget" class="com.hgsoft.ExecuteService">
    <property name="EAMDao" ref="EAMDao"/>
    <property name="ZHJDao" ref="ZHJDao"/>
</bean>

<bean id="executeService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">    
    <property name="transactionManager">
        <ref bean="txManager"/>
    </property>    
    <property name="target">
        <ref bean="executeServiceTarget"/>
    </property>
    <property name="transactionAttributes">        
        <props>
            <prop key="executeSynchrony">PROPAGATION_REQUIRES_NEW,-Exception</prop>          
        </props>
    </property>
</bean>


[/code]

  • 写回答

3条回答 默认 最新

  • CaiHuajiang 2010-01-19 11:23
    关注

    这是理所当然的,,如果spring管理是应用于当前线程的话,那么你活干完了,不是要把所以的配置全释放了吗?要别人怎么使用spring容器?
    可以主动调用ApplicationContext的close释放spring容器。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题