启动OSGI应用程序时找不到context.xml

时间:2017-04-13 04:23:41

标签: java spring hibernate osgi

OSGI bundle的classpath一般是META-INF文件夹。 “spring-context.xml”文件位于META-INF下的spring文件夹中。您可以在应用程序图片中看到。 ClassPathXmlApplicationContext找不到此xml文件。在我看来,问题是Classpath。但我没有解决这个问题。在我的应用程序中,我尝试使用spring-dm配置hibernate。在构建应用时,我帮助了website

我在下面分享给HİbernateUtil.class:

public abstract class HibernateUtil {

    private SessionFactory sessionFactory;

    private SessionFactory buildSessionFactory() {
        try {
            if (this.sessionFactory == null) {
                ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/spring/spring-context.xml");
                this.sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
            }

        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }

        return this.sessionFactory;
    }

    public Session newSession() {
        if (this.sessionFactory == null)
            buildSessionFactory();
        return (this.sessionFactory == null ? null : this.sessionFactory.openSession());
    }

}

我与spring-context.xml共享以下内容:

<?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:osgi="http://www.springframework.org/schema/osgi"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                       http://www.springframework.org/schema/osgi
				    http://www.springframework.org/schema/osgi/spring-osgi.xsd 
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.1.xsd">


	<context:annotation-config />

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		init-method="createDataSource"
		destroy-method="close">
		<property name="driverClassName" value="org.postgresql.Driver" />
		<property name="url" value="jdbc:postgresql://127.0.0.1:5432/xxxxx" />
		<property name="username" value="xxxxx" />
		<property name="password" value="xxxxx" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="50" />
		<property name="maxWait" value="1000" />
	</bean>


	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>tr.cc.ex.dict.kernel.model.xxxx</value>
				<value>tr.cc.ex.dict.kernel.model.xxxx</value> 
				<value>tr.cc.ex.dict.kernel.model.xxxx</value>
				<value>tr.cc.ex.dict.kernel.model.xxxx</value>
				<value>tr.cc.ex.dict.kernel.model.xxxx</value>
				<value>tr.cc.ex.dict.kernel.model.xxxx</value>
				<value>tr.cc.ex.dict.kernel.model.xxxx</value>
				<value>tr.cc.ex.dict.kernel.model.xxxx</value>
				
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.format_sql">false</prop>
			</props>
		</property>
	</bean>
	
</beans>

我与MANIFEST-MF分享以下内容:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Model
Bundle-SymbolicName: tr.cc.ex.dict.kernel.model;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: tr.cc.ex.dict.kernel.model.internals.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
Export-Package: tr.cc.ex.dict.kernel.model,
 tr.cc.ex.dict.kernel.model.hibernate.util
Import-Package: javax.persistence;version="1.99.0",
 org.apache.commons.dbcp;version="1.4.0",
 org.hibernate;version="3.4.0.GA-A",
 org.hibernate.cfg;version="3.3.2.GA",
 org.hibernate.ejb;version="3.4.0.GA-A",
 org.osgi.framework;version="1.8.0",
 org.postgresql;version="9.4.0",
 org.springframework.beans;version="3.1.0.RELEASE",
 org.springframework.beans.factory;version="3.1.0.RELEASE",
 org.springframework.beans.factory.config;version="3.1.0.RELEASE",
 org.springframework.context;version="3.1.0.RELEASE",
 org.springframework.context.support;version="3.1.0.RELEASE",
 org.springframework.core.io.support;version="3.1.0.RELEASE",
 org.springframework.orm.hibernate3;version="3.1.0.RELEASE",
 org.springframework.orm.hibernate3.annotation;version="3.1.0.RELEASE",
 tr.cc.ex.dict.sys.comp

这张图片是app文件夹:

enter image description here

我分享到以下app控制台输出:

Nis 13, 2017 7:13:59 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5ca1b42c: startup date [Thu Apr 13 07:13:59 EET 2017]; root of context hierarchy
Nis 13, 2017 7:13:59 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/spring-context.xml]
Initial SessionFactory creation failed.org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring/spring-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/spring-context.xml] cannot be opened because it does not exist
gogo: InvocationTargetException: null

2 个答案:

答案 0 :(得分:0)

您在build.properties中包含目录META-INF。据我所知,这意味着该jar内部的路径没有META-INF部分。你能试试#34; spring / spring-context.xml&#34;?

答案 1 :(得分:0)

我在代码后面改了ApplicationContext行,可能我解决了这个问题。但应用程序没有问题就开始了:

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("/Desktop/dictionary_project/tr.cc.ex.dict.kernel.model/META-INF/spring/spring-context.xml");

我分享新的控制台输出:

Nis 13, 2017 2:19:03 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@5e474f1b: startup date [Thu Apr 13 14:19:03 EET 2017]; root of context hierarchy
Nis 13, 2017 2:19:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [/home/oem/Desktop/dictionary_project/tr.cc.ex.dict.kernel.model/META-INF/spring/spring-context.xml]
Initial SessionFactory creation failed.org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: file [/home/oem/Desktop/dictionary_project/tr.cc.ex.dict.kernel.model/META-INF/spring/spring-context.xml]

然后我搜索这个例外。应用程序有一个“org.springframework.context”包。但我不明白为什么app会抛出这个异常。