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一直是空的....

 

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

 

 

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

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

}

[/code]

没有上下文环境导致。

定义 ApplicationContext ap = new FileSystemXmlApplicationContext(config);
或者是ClassPathXmlApplicationContext(config).

然后getbean("basicDAO"),就可以注入了。

是呀,手动注入肯定是没有问题的。

junit测试类完整的发出来。

这种写法肯定是不可以的。

将获取ApplicationContext 的逻辑写在setUpBeforeClass 里面。

BasicDAO basicDAO = new BasicDAO(); 这种方式没有通过上下文环境,需要通过getBean的方式才可以。

建议 搜索一下junit 测试hibernate 方面的文章看看。

不知道楼主结贴了没有 如果没有 告诉你的问题在哪里 你如果用Jnuit肯定要把applicontContext.xml 给读上来 不然spring 怎么知道 去注入啊

LS正解

junit测试时注意它不像web容器一样参照web.xml把所有配置文件读进来,想模拟的话,自己加载吧。