Results 1 to 5 of 5

Thread: why can't spring be aware of my changes of the config file

  1. #1
    Join Date
    Sep 2004
    Location
    China
    Posts
    17

    Default why can't spring be aware of my changes of the config file

    Hello All.

    I come up against a very strange problem! spring can't be aware of my changes of the config files when I run unittest with ant. I must run a main
    function in which I get some beans by ApplicationContext from config file. I can only notify my application that my config files have been change in this way! Why is there such a strange thing!

    Because the problem is so strange so I don't know what info must I supply to you!

    Any suggestion or hint ?

    Thanx in advance!

    Regards
    Yoshi

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    You're going to have to be more descriptive. I don't really know what you're trying ot do or what the exact problem is.

    If you are running unit tests, and they are loading application contexts or beanfactories, this just goes through standard file loading mechanisms, so should pick up any changes you make...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Spring will load the files once, so if you change the files, put them in it the right place, re-start the Unit tests and load context files properly from the Unit tests, it shouldn't be a problem.

    I run unit tests by putting the context files in the classpath and load them using something like:
    Code:
            String[] paths = new String[] {
                    "applicationContext.xml", "timspring-servlet.xml"
                };
            ctx = new ClassPathXmlApplicationContext(paths);

  4. #4
    Join Date
    Sep 2004
    Location
    China
    Posts
    17

    Default

    Thank you for you repidly replies!

    code snippets of build.xml

    Code:
    <project name="Gameshop" default="test" basedir=".">
    
        <property name="webapp.dir" value="D&#58;/tomcat/webapps/GameShop"/>
        <property name="lib.dir" value="$&#123;webapp.dir&#125;/WEB-INF/lib"/>
        <property name="build.dir" value="$&#123;webapp.dir&#125;/WEB-INF/classes"/>
        <property name="src.dir" value="./src"/>
        <property name="test.dir" value="./test"/>
    
        <path id="classpath">
            <pathelement location="$&#123;build.dir&#125;"/>
            <fileset dir="$&#123;lib.dir&#125;">
                <include name="**/*.jar"/>
            </fileset>
        </path>
    
        <path id="classpathTest">
            <fileset dir="d&#58;/lib">
                <include name="**/*.jar"/>
            </fileset>
        </path>
    
        <target name="all" depends="isJUnitAvailable, test"/>
    
        <target name="isJUnitAvailable">
            <available property="junit.present" classname="junit.framework.TestCase"/>
        </target>
    
        <target name="clean">
            <delete dir="$&#123;build.dir&#125;">
                <exclude name="**/*.properties"/>
                <exclude name="**/*.xml"/>
            </delete>
        </target>
    
        <target name="init" depends="clean">
            <mkdir dir="$&#123;build.dir&#125;"/>
        </target>
    
        <target name="compile" depends="init">
            <javac srcdir="$&#123;src.dir&#125;" destdir="$&#123;build.dir&#125;" classpathref="classpathTest"/>
        </target>
    
        <target name="compileTester" depends="compile">
            <javac srcdir="$&#123;test.dir&#125;" destdir="$&#123;build.dir&#125;" classpathref="classpathTest"/>
        </target>
    
        <target name="test" depends="compileTester">
            <junit printsummary="true">
                <classpath>
                    <path refid="classpath"/>
                    <path refid="classpathTest"/>
                </classpath>
    
                <formatter type="xml" usefile="true"/>
    
                <test name="gameshop.framework.dao.BaseDAOTest"/>
                <test name="gameshop.framework.dao.impl.BaseDAOIbatisImplTest"/>
    
            </junit>
        </target>
                  <!-- otherthings omitted -->
    </project>
    the whole Config File

    Code:
    <?xml version="1.0" encoding="GB2312"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
        <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="mappingResources">
                <list>
                    <value>gameshop/framework/testmodel/Entity.hbm.xml</value>
                    <value>gameshop/framework/testmodel/EntityChild.hbm.xml</value>
                </list>
            </property>
        </bean>
    
        <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="mySessionFactory"/>
            </property>
        </bean>
    
        <bean id="baseDAO"
            class="org.springframework.transactinterceptor.TransactionProxyFactoryBean"
            abstract="true">
            <property name="target">
                <ref local="baseDAOHibernate"/>
            </property>
            <property name="transactionManager">
                <ref local="myTransactionManager"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
        <bean id="baseDAOHibernate" class="gameshop.framework.dao.impl.BaseDAOHibernateImpl">
            <property name="sessionFactory">
                <ref local="mySessionFactory"/>
            </property>
        </bean>
    
        <bean id="baseDAOIbatis" class="gameshop.framework.dao.impl.BaseDAOIbatisImpl">
            <property name="dataSource">
                <ref local="dataSource"/>
            </property>
            <property name="sqlMapClient">
                <ref local="sqlMap"/>
            </property>
        </bean>
    
        <!--    dbunit和Ibatis需要用到下边的dataSource-->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName">
                <value>org.gjt.mm.mysql.Driver</value>
            </property>
            <property name="url">
                <value>jdbc&#58;mysql&#58;//localhost/Gameshop?useUnicode=true&amp;characterEncoding=GBK</value>
            </property>
            <property name="username">
                <value>root</value>
            </property>
            <property name="password">
                <value></value>
            </property>
        </bean>
    
        <bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocation">
                <value>ibatis/SqlmapConfig.xml</value>
            </property>
        </bean>
    </beans>
    I load bean with my custom class: BeanLocator

    Is theare any problem in this Class?

    Code:
    public class BeanLocator
    &#123;
        public static ClassPathXmlApplicationContext context;
    
        static
        &#123;
            try
            &#123;
                init&#40;&#41;;
            &#125;
            catch &#40;Exception e&#41;
            &#123;
                e.printStackTrace&#40;&#41;;  
            &#125;
        &#125;
    
        private BeanLocator&#40;&#41;
        &#123;
        &#125;
    
        public static Object getBean&#40;final String beanName&#41;
        &#123;
            return context.getBean&#40;beanName&#41;;
        &#125;
    
        private static void init&#40;&#41;
        &#123;
            String keyConfigFileName = "/spring/Context.xml";
            String path = BeanLocator.class.getResource&#40;keyConfigFileName&#41;.getPath&#40;&#41;;
    
            File file = new File&#40;path&#41;;
            File dir = file.getParentFile&#40;&#41;;
            String fileNames&#91;&#93; = dir.list&#40;&#41;;
    
            for &#40;int i = 0; i < fileNames.length; ++i&#41;
            &#123;
                fileNames&#91;i&#93; = "/spring/" + fileNames&#91;i&#93;;
            &#125;
    
            context = new ClassPathXmlApplicationContext&#40;fileNames&#41;;
        &#125;
    &#125;
    My test class:

    Code:
    public class BaseDAOTest extends DatabaseBaseTestCase
    &#123;
        IBaseDAO baseDAO = &#40;IBaseDAO&#41; BeanLocator.getBean&#40;"baseDAO"&#41;;
        Entity entity = null;
        Entity anotherEntity = null;
        private static final String entityName = "entity";
        private static final String anotherEntityName = "anotherEntity";
    
        protected SessionFactory sessionFactory = null;
    
        public BaseDAOTest&#40;String s&#41;
        &#123;
            super&#40;s&#41;;
        &#125;
    
        protected void setUp&#40;&#41; throws Exception
        &#123;
            super.setUp&#40;&#41;;
    
            emptyAllTables&#40;&#41;;
            entity = new Entity&#40;&#41;;
            entity.setId&#40;"1"&#41;;
            entity.setName&#40;entityName&#41;;
    
            anotherEntity = new Entity&#40;&#41;;
            anotherEntity.setId&#40;"2"&#41;;
            anotherEntity.setName&#40;anotherEntityName&#41;;
    
            sessionFactory = &#40;SessionFactory&#41; BeanLocator.getBean&#40;"mySessionFactory"&#41;;
            Session session = SessionFactoryUtils.getSession&#40;sessionFactory, true&#41;;
            TransactionSynchronizationManager.bindResource&#40;sessionFactory, new SessionHolder&#40;session&#41;&#41;;
        &#125;
    
        protected void tearDown&#40;&#41; throws Exception
        &#123;
            SessionHolder sessionHolder =
                    &#40;SessionHolder&#41; TransactionSynchronizationManager.unbindResource&#40;sessionFactory&#41;;
            SessionFactoryUtils.closeSessionIfNecessary&#40;sessionHolder.getSession&#40;&#41;, sessionFactory&#41;;
    
            super.tearDown&#40;&#41;;
        &#125;
    
        public void testSave&#40;&#41; throws SQLException, DataSetException, HibernateException
        &#123;
            ITable table = connection.createQueryTable&#40;"ExceptedData", QUERY&#41;;
            assertEquals&#40;0, table.getRowCount&#40;&#41;&#41;;
    
            baseDAO.save&#40;entity&#41;;
    
            table = connection.createQueryTable&#40;"ExceptedData", QUERY&#41;;
            assertEquals&#40;1, table.getRowCount&#40;&#41;&#41;;
        &#125;
    &#125;
    Every thing goes well excpet that spring can't aware of my changes of the config files when I run the test in the build file with ant. It seems that spring has been slept in ant' build file!

    In order to wake the spring up. All must I do is only to run such a dumb thing like this:

    Code:
        public static void main&#40;String&#91;&#93; args&#41;
        &#123;
            IBaseDAO baseDAO = &#40;IBaseDAO&#41; BeanLocator.getBean&#40;"baseDAOIbatis"&#41;;
        &#125;
    And then the dear MR.spring wake up!The changes of the config files will be known by MR.spring.

    Any suggestion ? more details?

    Thanx in advance!

    Bow
    Yoshiyan

  5. #5
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    I am not clear on what you mean by Spring waking up... Spring will not cache anything, although certainly a static var like you are using will end up caching a context while that class is still in memory.

    Are you running ant from command-line or from within your IDE? In either case, a new invocation of the test would end up loading a new vm and a new instance of the context, with your code. If you are running multiple tests at the same time, then in some IDEs the IDE will not fork for each TestCase (which is why I think you are using the static in the first place), but the first test will trigger loading of the context. Spring does not and can not 'cache' information between different VM invocations. If you are getting cached data with your setup in the same VM invocation, it is becaue you are keeping your 'BeanLocator' class in memory...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Replies: 2
    Last Post: Apr 12th, 2012, 09:34 AM
  2. Cannot reference my spring config files from another project
    By lorelia in forum SpringSource Tool Suite
    Replies: 4
    Last Post: Aug 30th, 2005, 01:52 AM
  3. Replies: 2
    Last Post: Aug 4th, 2005, 05:16 PM
  4. Help loading Spring config file from classpath
    By phersh in forum Container
    Replies: 1
    Last Post: Mar 2nd, 2005, 07:20 PM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •