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.
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.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
JpaConfiguration.java
===============
app-context.xmlCode: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); } ....
==============
LoginUserRepositoryTest.javaCode:<?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>
======================
Activator.javaCode: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); } }
===========
Did anyone know what's the problem in my coding?Code:public void start(BundleContext context) throws Exception { super.start(context); plugin = this; ctx = new AnnotationConfigApplicationContext(JpaConfiguration.class); }
Best Rdgs
Ellis


Reply With Quote
