开发者社区> 问答> 正文

web调用后台程序spring无法注入问题

架构是这样的 ssh(struts2,spring3.1,hibernate4) action->el->service-dao
el模块里有个 线程池 ,我管 线程池 叫 工头 .然后el模块还有个线程类.我管这个 线程类 叫 工人 .
工头从任务队列添加任务,工头要先查下数据库这个任务是否(调service查找),这个时候service对象是为空的,
我分析了是service里的dao里的sessionFactiory,是为空,导致层层都没有获取成功,我现在假设把线程池对象交给spring管理那么线程池产生的线程对象也是交给spring,可能这种做法不是很好,但是不交给spring的话,service老为空,注意:工头也调service,工人也是调service .
如果可以解决service不为空, 解决这个问题也可以不交spring管理
也分享一下我感觉比较爽的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spider"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
      </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
         <property name="packagesToScan">
              <list>
                <value>net.it.entity</value>
              </list>
        </property>
        <property name="hibernateProperties">
              <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                hibernate.show_sql=true
              </value>
        </property> 
    </bean>
<bean id="transactionManager" 
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
        <props>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="add*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="check*">PROPAGATION_REQUIRED</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
        </props>
    </property>
</bean>
<bean id="ProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
     <property name="beanNames"> 
        <list> 
            <value>*Service</value>
        </list> 
    </property> 
    <property name="interceptorNames"> 
        <list> 
            <value>transactionInterceptor</value> 
        </list> 
     </property> 
</bean>
<context:annotation-config/>
<context:component-scan base-package="net.it">
</context:component-scan>
</beans>

展开
收起
a123456678 2016-03-16 09:49:10 2085 0
1 条回答
写回答
取消 提交回答
  • <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="5" />
        <property name="maxPoolSize" value="10" />
        <property name="queueCapacity" value="25" />
    </bean>
     
    private TaskExecutor pool;
     
        public TaskExecutor getPool() {
            return pool;
        }
         
        @Resource(name="taskExecutor")
        public void setPool(TaskExecutor pool) {
            this.pool = pool;
        }
    2019-07-17 19:03:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载