Announcement Announcement Module
Collapse
No announcement yet.
Can't read XML definition file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't read XML definition file

    Dear All,

    I'm building an Eclipse RCP application using Eclipse 3.7.1 and Spring 3.0.6. I use Java based container configuration method to setup the spring. In my unit test, that's fine without any problem. However, when I use AnnotationConfigApplicationContext to initiate my main program, it can't find my xml file. Below is the error message.

    Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [app-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [app-context.xml] cannot be opened because it does not exist
    That's what I don't understand, it has same spring configuration and read same xml file as my unit test . Below is my coding.

    JpaConfiguration.java
    ===============
    Code:
    package com.rainbow.claim;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.sql.DataSource;
    
    import org.hibernate.cache.HashtableCacheProvider;
    import org.hibernate.dialect.DerbyDialect;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.JpaVendorAdapter;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.orm.jpa.vendor.Database;
    import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
    import org.springframework.transaction.PlatformTransactionManager;
    
    @Configuration
    @ImportResource("classpath:/app-context.xml")
    public class JpaConfiguration {
    
    	private @Value("${jdbc.url}")
    	String url;
    	private @Value("${jdbc.showSql}")
    	boolean showSql;
    	private @Value("${jdbc.username}")
    	String username;
    	private @Value("${jdbc.password}")
    	String password;
    	private @Value("${hibernate.formatSql}")
    	String formatSql;
    	private @Value("${hibernate.hbm2ddl.auto}")
    	String hbm2ddl;
    
    	@Bean
    	public DataSource dataSource() {
    		return new DriverManagerDataSource(url, username, password);
    	}
                 ....
    app-context.xml
    ==============
    Code:
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:repository="http://www.springframework.org/schema/data/repository"
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    		http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<description>configuration on rainbow claim case</description>
    	
    	<context:component-scan base-package="com.rainbow.claim" />
    	<context:component-scan base-package="com.rainbow.claim.service" />
    
    	<context:property-placeholder location="classpath:/database.properties"/>
    
    	<jpa:repositories base-package="com.rainbow.claim.repository"/>
    	
    
    </beans>
    LoginUserRepositoryTest.java
    ======================
    Code:
    package com.rainbow.claim;
    
    import static org.junit.Assert.assertEquals;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.rainbow.claim.entity.User;
    import com.rainbow.claim.service.LoginUserService;
    
    @ContextConfiguration(locations = { "classpath:/app-context.xml" })
    @RunWith(SpringJUnit4ClassRunner.class)
    @Transactional
    public class LoginUserRepositoryTest {
    
    	@Autowired
    	LoginUserService userSrv;
    	User user;
    
    	@Before
    	public void setUp() {
    		user = new User();
    		user.setUserId("userid");
    		user.setUsername("username");
    		user.setPassword("password");
    	}
    
    	@Test
    	public void testCrud() {
    		user = userSrv.save(user);
    		assertEquals(user, userSrv.findByUserId(user.getUserId()));
    	}
    
    	@Test
    	public void testMethodQuery() throws Exception {
    		Boolean succeed = false;
    
    		user = userSrv.save(user);
    		succeed = userSrv.authenticate("userid", "password");
    		assertEquals(succeed, true);
    	}
    }
    Activator.java
    ===========
    Code:
    	public void start(BundleContext context) throws Exception {
    		super.start(context);
    		plugin = this;
    		ctx = new AnnotationConfigApplicationContext(JpaConfiguration.class);
    	}
    Did anyone know what's the problem in my coding?

    Best Rdgs
    Ellis

  • #2
    Are you positive the xml file is being copied into your classpath in your application. Whereas it is already in your classpath in your text.

    Also, if it is copying it, are you positive that the xml file is at the root of your classpath? If it is further in your classpath, you will have to tell Spring exactly the package path to go to.

    What are you using to build your app, are you using Maven? Ant?

    Mark

    Comment


    • #3
      Thanks Mark, I'm sure the xml file is copied into classpath. I'm using Eclipse 3.7.1 RCP to build my application. I didn't use either Maven or Ant for my program.

      My xml file put in the root of src folder.

      com.rainbow.claim
      |
      |__src
      | |_app-context.xml
      |
      |__test

      Comment


      • #4
        First I noticed you had


        <context:component-scan base-package="com.rainbow.claim" />
        <context:component-scan base-package="com.rainbow.claim.service" />

        That is redundant. The first component scan will also scan subpackages under com.rainbow.claim. So that already includes com.rainbow.claim.service.

        For file not found. try

        "classpath:app-context.xml"

        The forward slash in front is not needed. If you look at your archive created after compiling, do you see the app-context.xml in the actual root of your classpath?

        Thanks

        Mark

        Comment


        • #5
          After struggling this problem for few days, I think the problem was caused by using RCP. Since RCP is based on OSGi archecture, it seemed the classloader causing this problem. I think I may need to change using Eclipse Gemini (Former: Spring DM) to solve my problem. If anyone know better solution on this, please kindly let me know.

          Rdgs
          Ellis

          Comment

          Working...
          X