在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个回答

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

你用工具分析下main方法启动的jvm进程中有多少个线程,搞不好新开了线程

spring中对bean的管理有两种类型 ApplicationContext和BeanFactory
ApplicationContext在容器启动时候会全部初始化bean,而factory只会在调用get(“abc”)时候才会初始化

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐