【问题标题】:Runnig spring via Daemon as Standalone通过 Daemon 作为独立运行 spring
【发布时间】:2012-07-01 12:44:24
【问题描述】:

我正在尝试通过 Daemon 服务作为独立应用程序运行 Spring。

我已经配置了 run.sh 脚本并添加了所有 Spring jars 框架。

现在我正在尝试以这种方式从守护程序类执行我的起点类:

代码: 公共类 FeedDaemon 实现守护进程 { 公共 FeedDaemon() { }

protected final static Logger log = LoggerFactory.getLogger(FeedDaemon.class);
protected boolean shouldBeRunning = false;
protected ProviderFactory runner = null;

@Override
public void destroy()
{
    runner = null;
}

@Override
public void init(DaemonContext arg0) throws Exception
{
    runner = new ProviderFactory();
}

public void start() throws RuntimeError, ConfigError
{
    log.info("Starting daemon");
    runner.start();
}

public void stop() throws Exception
{
    log.info("Starting Shutting daemon ...");
    runner.stop();
}

}

代码:

package com.spring.test;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.aspect.Spring3HelloWorld;
import com.spring.beans.ParkingCar.CarBean;
import com.spring.beans.ParkingCar.CarMaker;
import com.spring.beans.ParkingCar.FourWheelsVechile;
import com.spring.beans.ParkingCar.TwoWheelsVechile;
import com.spring.beans.ParkingCar.Vechile;
import com.spring.beans.ParkingCar.VechileDetails;
import com.spring.beans.calculator.CalculateNumbersHolderBean;
import com.spring.beans.calculator.CalculateStrategyBean;
import com.spring.beans.calculator.CalculatorBean;

public class Spring3HelloWorldTest
{

    static Logger logger = Logger.getLogger(Spring3HelloWorldTest.class);

    public static void execute()
    {
        try
        {   
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            logger.debug("3");


            Vechile fourWheelsVechile = (FourWheelsVechile) context.getBean("Ambulance");
            fourWheelsVechile.drive();
            CarMaker carMaker = (CarMaker) context.getBean("carMaker");
            CarBean carBean = carMaker.createNewCar();
            carBean.driveCar();
        }
        catch (Throwable e)
        {
            logger.error(e);
        }

    }
}

我得到这个错误:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.commons.daemon.support.DaemonLoader.load(DaemonLoader.java:164)
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
    at com.spring.runner.FeedDaemon.init(FeedDaemon.java:37)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    ... 6 more
Cannot load daemon
Service exit with a return value of 3

这是我的 run.sh 脚本:

导出 JAVA_HOME=/../ FIXGW=/../FIXGW CLASSPATH=$FIXGW/lib/FeedHandler.jar:$FIXGW/lib/FixSpring.jar:$FIXGW/lib/org.springframework.web-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework。 web.struts-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.web.servlet-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.web.portlet-来源-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.test-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.orm-sources-3.1.1.RELEASE。 jar:$FIXGW/lib/org.springframework.jms-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.jdbc-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org .springframework.context.support-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.aspects-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.transaction-sources -3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.oxm-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.instrument-sources-3.1.1.RELEASE.jar :$FIXGW/lib/org.springframework.instrument.tomcat-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org .springframework.expression-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.core-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.context-sources-3.1 .1.RELEASE.jar:$FIXGW/lib/org.springframework.beans-sources-3.1.1.RELEASE.jar:$FIXGW/lib/org.springframework.asm-sources-3.1.1.RELEASE.jar:$ FIXGW/lib/org.springframework.aop-sources-3.1.1.RELEASE.jar:$FIXGW/lib/commons-daemon-1.0.3.jar

cd $FIXGW /../jsvc -用户狐狸\ -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:SurvivorRatio=16 \ -Dlog4j.configuration=文件:$FIXGW/conf/log4j.properties \ -outfile /dev/null \ -errfile $FIXGW/logs/error.log \ -verbose -classpath $CLASSPATH \ com.spring.runner.FeedDaemon

有什么想法吗?

谢谢, 射线。

【问题讨论】:

    标签: java spring daemon


    【解决方案1】:

    您似乎已经添加了所有包含 Spring java 源代码(未编译代码)的 jar。您应该改为将未命名为 ...-sources... 的 jar 添加到类路径中。

    【讨论】:

    • 应该如何完整命名?请给我写一个罐子的例子。谢谢
    • 如果你使用 Maven,你想要的文件应该就在源文件旁边。如果没有,你想要名为 spring-core-3.1.1.RELEASE.jar 的文件等等。
    • 在 ANT 上使用 Maven 会更好吗?因为我知道如何使用 ANT,以前从未使用过 Maven。
    • 认为您即将在这里开始一场非常激烈的辩论 ;-) Maven 确实内置了依赖处理,可以从不同的存储库自动下载。实用且使依赖项看起来很容易,但在我看来可能会导致一种错误的安全感,认为依赖项处理很容易。与其在此处开始冗长的讨论,不如搜索 Maven,您会发现很多信息以及优缺点说明。
    • 好吧。那么告诉我你将如何让 Spring 在独立应用程序中运行?我的意思是必须有一些while方法
    【解决方案2】:

    spring jars 不在类路径中。检查我的answer。通过命令行在类路径中添加所有库(当然在你的 shell 脚本中)。

    好吧,我现在看到了您的问题。您在类路径中有源文件。这些被称为org.springframework.web.servlet-sources-3.1.1.RELEASE.jar,而编译的jar被称为org.springframework.web.servlet-3.1.1.RELEASE.jar

    【讨论】:

    • 但是 jars 在类路径中.. 检查我的 run.sh 脚本
    • 你是否从 spring source.org 下载了完整的 zip 文件
    • 是的,我做到了,修复后我得到:2012-07-01 13:22:36,091 com.spring.test.Spring3HelloWorldTest [ERROR] org.springframework.beans.factory.BeanCreationException: Error创建名为“org.springframework.aop.config.internalAutoProxyCreator”的bean:bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]:构造函数抛出异常;嵌套异常是 java.lang.NoClassDefFoundErrororg/aopalliance/intercept/MethodInterceptor
    • @rayman : org.springframework.aop jar 不在类路径中。检查一下。
    • 是的,现在我得到了其他错误:2012-07-01 14:00:40,146 com.spring.test.Spring3HelloWorldTest [错误] org.springframework.beans.factory.BeanCreationException:创建带有名称的 bean 时出错类路径资源 [applicationContext.xml] 中定义的“Spring3HelloWorldBean”:bean 初始化失败;嵌套异常是 org.springframework.aop.framework.AopConfigException:无法代理目标类,因为 CGLIB2 不可用。将 CGLIB 添加到类路径或指定代理接口。在我修复后,我得到了 asm 丢失错误。为什么不全部捆绑?
    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2016-09-07
    • 2023-03-17
    • 2017-09-15
    • 2013-05-22
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多