我的应用程序包含三个Maven项目(我省略了其他模块)):FrameworkBase(JAR),FrameworkBaseImpl(JAR),FrameworkRestService(WAR)。
FrameworkRestService是FrameworkBaseImpl的一个插件,用于绘制其类/接口定义 来自FrameworkBase,FrameworkBaseImpl实现/扩展。 (我不得不拆分FrameworkBase以避免Maven curcular依赖)。
FrameworkBaseImpl有一个main,它调用Jetty,传递FrameworkRestService war。
我试图让Spring在FrameworkBainImpl的FrameworkMain类的(唯一)实例中注入一个RestService实例作为字段成员。 My Spring指向FrameworkRestService项目中的FrameworkRestService类,作为实现restService bean。
问题在于:虽然Spring似乎在WAR中找到导入的framework_rest_service.context文件,但是它无法注入它(没有找到名为restService的bean)。但是,如果我将FrameworkRestService转换为JAR - 它会神奇地成功!我被困2天了!请帮忙!
FrameworkBase pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>framework.base</artifactId>
<packaging>jar</packaging>
<name>framework.base</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>controlapps</groupId>
<artifactId>controlapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../controlapp/pom.xml</relativePath>
</parent>
</project>
FrameworkBase RestService:
package com.company.controlapps.framework.base;
public class RestService {
public FrameworkMain frameworkMain;
/* Setters for Spring */
public void setFrameworkMain(FrameworkMain frameworkMain)
{this.frameworkMain = frameworkMain;}
}
FrameworkBase FrameworkMain:
package com.company.controlapps.framework.base;
public interface FrameworkMain {
...
}
FrameworkBaseImpl pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>framework.base.impl</artifactId>
<packaging>jar</packaging>
<name>framework.base.impl</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>controlapps</groupId>
<artifactId>controlapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../controlapp/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>controlapps</groupId>
<artifactId>framework.base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>controlapps</groupId>
<artifactId>framework.restservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>runtime</scope>
<type>war</type>
</dependency>
</dependencies>
</project>
FrameworkBaseImpl FrameworkMainImpl:
package com.company.controlapps.framework.base.impl;
public class FrameworkMainImpl implements FrameworkMain {
....
protected RestService restService;
public void setRestService(RestService restService)
{this.restService = restService;}
}
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext(SPRING_CONTEXT_FILENAME);
FrameworkMainImpl frameworkMain = (FrameworkMainImpl) context.getBean("frameworkMain");
...
}
FrameworkBaseImpl 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="classpath*:framework_restservice_context.xml"/>
<bean id="frameworkMain" class="com.company.controlapps.framework.base.impl.FrameworkMainImpl" >
<property name="restService" ref="restService" />
</bean>
</beans>
FrameworkRestService FrameworkRestService pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>framework.restservice</artifactId>
<packaging>war</packaging>
<name>framework.restservice</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>1.12</jersey.version>
</properties>
<parent>
<groupId>controlapps</groupId>
<artifactId>controlapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../controlapp/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>controlapps</groupId>
<artifactId>framework.base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server-linking</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
FrameworkRestService spring framework_restservice_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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="restService" class="com.radware.controlapps.framework.restservice.FrameworkRestService">
<property name="frameworkMain" ref="frameworkMain" />
</bean>
</beans>
FrameworkRestService FrameworkRestService.java片段:
package com.company.controlapps.framework.restservice;
@Path("myresource")
public class FrameworkRestService extends RestService {
...
@Context
UriInfo uriInfo;
@Context
Request request;
@GET
@Produces(MediaType.APPLICATION_JSON)
public MyResource getMyResource() {
...
frameworkMain.doSomething();
....
}
很抱歉这个问题很长。 : - )
答案 0 :(得分:0)
这不是类加载器问题。我有WAR和JAR使用相同的类加载器(JAR)。有人评论说我不应该尝试链接WAR并在那里寻找bean。
因此,我不是试图解决问题,而是通过取消JAR和WAR之间的Spring依赖关系来绕过它。在WAR中,我仍然需要对JAR中实例化的FrameworkMain单例进行引用。
我“黑客攻击”如下:我添加了一个名为Holder的类,使用公共静态方法获取并设置了FrameworkMain实例单例。我从WAR调用get,并从jar中设置。我无法将其添加到FrameworkMain,它实际上是FrameworkBase jar中的一个接口(由FrameworkBaseImpl jar中的FrameworkMainImpl实现)。
感谢。