weixin_42434473 2011-03-18 10:51
浏览 204
已采纳

Sf 无法注入 帮我看看吧~~~谢谢

<?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:p=" http://www.springframework.org/schema/p"
    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-2.5.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd"
    default-autowire="byName">

<!-- 为dataSource指定jdbc.properties文件的位置 -->
    <bean
        class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>   
    </bean>
    
<!-- 配置  BoneCP 数据源 -->
    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">  

        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>  
    <!-- 设置测试connection的间隔时间。这个参数默认为240,单位:分钟。设置为0该功能失效。 -->
        <property name="idleConnectionTestPeriod" value="60"/>
    <!-- 设置connection的空闲存活时间。这个参数默认为60,单位:分钟。设置为0该功能失效。  -->
        <property name="idleMaxAge" value="240"/>        
    <!--每个分区最大connection数 -->
        <property name="maxConnectionsPerPartition" value="60"/>  
    <!-- 每个分区最小connection数 -->
        <property name="minConnectionsPerPartition" value="20"/>  
    <!-- 设置分区个数。这个参数默认为1,建议3-4 -->
        <property name="partitionCount" value="3"/>  
    <!-- 设置分区中的connection增长数量。这个参数默认为1 -->
        <property name="acquireIncrement" value="10"/>                                
    
        <property name="statementsCacheSize" value="50"/>  

        <property name="releaseHelperThreads" value="3"/>  

    </bean> 
<!-- Hibernate配置 、 sessionFactory -->
    <bean name="sessionFactory" 
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.nes.vo.admin</value>
            </list>
        </property>       
    </bean>

<!-- 事务 begin -->  
 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  

    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="sel*" read-only="true" />
            
            <tx:method name="save*" />  
            <tx:method name="del*" />
        </tx:attributes>  
    </tx:advice>  

    <!-- 事务 end --> 


<!-- Inject DAO -->

    <bean name="basicDAO" class="com.nes.dao.BasicDAO">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
</beans>

 

package com.nes.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class BasicDAO {

    private SessionFactory sessionFactory;
    
    private Session session;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    public void setSession(Session session) {
        this.session = session;
    }
    
    
}

 

 

我用Junit测试了 SF一直是空的....

 

请大家帮我看看吧~~~~

 

 

  • 写回答

8条回答 默认 最新

  • jbutton 2011-03-18 13:27
    关注

    Junit 测试你又不能启动你的web.xml,用Junit测试 获取ApplicationContext 的逻辑必须写在setUpBeforeClass 里面。
    [code="java"]

    @BeforeClass
    public static void beforeClass() {
    ctx = new ClassPathXmlApplicationContext("classpath:config/application-context.xml");

    }
    

    [/code]

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

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入