Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带OSGi的SpringMVC-如何注册新控制器-第二部分?_Java_Spring_Spring Mvc_Osgi_Osgi Bundle - Fatal编程技术网

Java 带OSGi的SpringMVC-如何注册新控制器-第二部分?

Java 带OSGi的SpringMVC-如何注册新控制器-第二部分?,java,spring,spring-mvc,osgi,osgi-bundle,Java,Spring,Spring Mvc,Osgi,Osgi Bundle,我有maven web应用程序,SpringWebMVC和我在主应用程序@RequestMapping注释中使用的是什么 <context:component-scan base-package="net.arturik.testosgispring.controllers" /> <mvc:annotation-driven /> 控制器代码,我希望映射到URL的内容: package net.arturik.testosgispring.control

我有maven web应用程序,SpringWebMVC和我在主应用程序@RequestMapping注释中使用的是什么

<context:component-scan
       base-package="net.arturik.testosgispring.controllers" />
<mvc:annotation-driven />
控制器代码,我希望映射到URL的内容:

package net.arturik.testosgispring.controllers.q;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class C1 {

    @RequestMapping(value = "/77777", method = RequestMethod.GET)
    public ModelAndView getdata() {

        ModelAndView model = new ModelAndView("index");

        return model;

    }

}
dispatcher-context.xml文件的内容为

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <context:annotation-config />
    <context:component-scan
        base-package="net.arturik.testosgispring.controllers.q" annotation-config="true" />

    <bean name="/helloWorld.htm"
          class="net.arturik.testosgispring.controllers.q.C1" />


    <mvc:annotation-driven />


</beans>

看起来您用Spring的东西创建了一个单独的包,或者是从某处获得的。因此,包含
FileSystemXmlApplicationContext
类的捆绑包的类路径无法看到您的本地类。因此出现了
ClasssNotFoundException

当然,您可以调整类加载器,例如在创建上下文之前将bundle类加载器设置为线程,但我强烈建议您不要使用Spring/OSGi组合

Spring(更具体地说:具有所有隐藏魔法的更高版本)和OSGi实际上并不协同工作。这是众所周知的,这就是为什么你的问题没有得到太多回应的原因。相信我,你所走的道路会带来很多困难,很可能会走到死胡同

如果必须使用这种组合,那么最好使用其他容器,如ApacheKaraf。我不使用它,但我知道其他人在这方面取得了一些成功。

我们可以尝试使用Felix的“嵌入依赖项”。 另一个选择是在Spring上启动felix

参考文献1:

参考文献2:

另一个选择是使用MSF4J,它构建在OSGi之上

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <context:annotation-config />
    <context:component-scan
        base-package="net.arturik.testosgispring.controllers.q" annotation-config="true" />

    <bean name="/helloWorld.htm"
          class="net.arturik.testosgispring.controllers.q.C1" />


    <mvc:annotation-driven />


</beans>
18-Nov-2014 18:37:53.370 INFO [Thread-6] org.springframework.context.support.FileSystemXmlApplicationContext.prepareRefresh Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@25796c7e: startup date [Tue Nov 18 18:37:53 MSK 2014]; root of context hierarchy
18-Nov-2014 18:37:53.378 INFO [Thread-6] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from file [k:\myhostingpanel\myhostingpanel\Tests\TestOSGIBundle\src\main\resources\dispatcher-context.xml]
18-Nov-2014 18:37:53.452 WARNING [Thread-6] org.springframework.context.support.FileSystemXmlApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt
 org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [net.arturik.testosgispring.controllers.q.C1] for bean with name '/helloWorld.htm' defined in file [k:\myhostingpanel\myhostingpanel\Tests\TestOSGIBundle\src\main\resources\dispatcher-context.xml]; nested exception is java.lang.ClassNotFoundException: net.arturik.testosgispring.controllers.q.C1
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1325)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:623)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:592)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1394)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:957)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:705)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at net.arturik.testosgibundle.Activator.start(Activator.java:25)
    at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2154)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2072)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:976)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:963)
    at khartn.khartnosgiwrapper.OSGiLoader.installAndStartBundles(OSGiLoader.java:175)
    at khartn.khartnosgiwrapper.OSGiLoader.start(OSGiLoader.java:90)
    at net.arturik.testosgispring.OSGIThread.run(OSGIThread.java:34)
Caused by: java.lang.ClassNotFoundException: net.arturik.testosgispring.controllers.q.C1
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1324)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:246)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1346)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1317)
    ... 18 more

Could not create framework: org.osgi.framework.BundleException: Activator start error in bundle net.arturik.TestOSGIBundle [1].
org.osgi.framework.BundleException: Activator start error in bundle net.arturik.TestOSGIBundle [1].
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2204)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2072)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:976)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:963)
    at khartn.khartnosgiwrapper.OSGiLoader.installAndStartBundles(OSGiLoader.java:175)
    at khartn.khartnosgiwrapper.OSGiLoader.start(OSGiLoader.java:90)
    at net.arturik.testosgispring.OSGIThread.run(OSGIThread.java:34)
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [net.arturik.testosgispring.controllers.q.C1] for bean with name '/helloWorld.htm' defined in file [k:\myhostingpanel\myhostingpanel\Tests\TestOSGIBundle\src\main\resources\dispatcher-context.xml]; nested exception is java.lang.ClassNotFoundException: net.arturik.testosgispring.controllers.q.C1
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1325)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:623)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:592)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1394)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:957)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:705)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at net.arturik.testosgibundle.Activator.start(Activator.java:25)
    at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2154)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: net.arturik.testosgispring.controllers.q.C1
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1324)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:246)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1346)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1317)
Export-Package: net.arturik.testosgibundle;uses:="org.osgi.framework,org
 .springframework.context.support,net.arturik.testosgispringlib,org.osgi
 .util.tracker";version="1.0.0.SNAPSHOT",net.arturik.testosgibundle.cont
 rollers;uses:="org.springframework.web.servlet,org.springframework.ster
 eotype,org.springframework.web.bind.annotation";version="1.0.0.SNAPSHOT
 ",net.arturik.testosgispring.controllers.q;uses:="org.springframework.w
 eb.servlet,org.springframework.stereotype,org.springframework.web.bind.
 annotation";version="1.0.0.SNAPSHOT"
Import-Package: net.arturik.testosgispringlib,org.osgi.framework;version
 ="[1.7,2)",org.osgi.util.tracker;version="[1.5,2)",org.springframework.
 context.support,org.springframework.stereotype,org.springframework.web.
 bind.annotation,org.springframework.web.servlet