Spring Nosuchbeanexception - spring

I have created simple spring core application
,while trying to execute it I am getting NoSuch bean defined exception even though I have defined particular bean in config
Here are my files
#config.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.xsd">
<bean id="reportService" class="com.nagesh.sample.ReportServie"/>
</beans>
object class
repoService.java
`package com.nagesh.sample;
public class ReportService {
public void display() {
System.out.println("Hi, Welcome to Report Generation application");
}
}
main class file
mainclass
package com.nagesh.spring.Sample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.nagesh.sample.ReportService;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath*:config.xml");
ReportService reportService =
(ReportService) context.getBean("reportService");
reportService.display();
}
}`
here is pom.xml file
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>
<groupId>com.nagesh.spring</groupId>
<artifactId>Sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Sample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>
</project>`
help me to resolve this
Thanks in advance

One Error :
<bean id="reportService" class="com.nagesh.sample.ReportServie"/>
changed it to
<bean id="reportService" class="com.nagesh.sample.ReportService"/>
Job Done :)

Related

Autowired applicationContext is null in JUnit 5

I'm trying to migrate my spring applications to test with JUnit 5. It worked fine with JUnit 4. My problem is that I can't access the application context, it is null, but should be an object. I've included imports etc, in case something is wrong there.
Test:
package com.mycompany.mavenspringjunit5;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
#SpringJUnitConfig(locations = "/test-config.xml")
public class MainTest
{
#Autowired
ApplicationContext applicationContext;
#Autowired
Integer number;
// fails
#Test
public void testGivenAppContext_WhenInjected_ThenItShouldNotBeNull()
{
System.out.println("number: " + number);
Assertions.assertNotNull(applicationContext, "applicationContext should not be null");
}
// works
#Test
public void testDoStuff()
{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/test-config.xml");
Main instance = ctx.getBean(Main.class);
instance.doStuff();
}
}
Main:
package com.mycompany.mavenspringjunit5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
#Component
public class Main
{
#Autowired
Integer number;
#Autowired
ApplicationContext applicationContext;
public static void main(String[] args)
{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ctx.getBean(Main.class).doStuff();
}
void doStuff()
{
System.out.println("The number is " + number);
System.out.println("applicationContext " + applicationContext);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.mycompany</groupId>
<artifactId>MavenSpringJunit5</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.3.4</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
test-config.xml (in the directory "....\MavenSpringJunit5\src\test\resources\test-config.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config />
<context:component-scan base-package="com.mycompany.mavenspringjunit5"/>
<bean id="number" class="java.lang.Integer">
<constructor-arg value="42" />
</bean>
</beans>
Output:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.mavenspringjunit5.MainTest
The number is 42
applicationContext org.springframework.context.support.ClassPathXmlApplicationContext#51521cc1, started on Mon Feb 22 10:49:15 CET 2021
number: null
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.884 sec <<< FAILURE!
com.mycompany.mavenspringjunit5.MainTest.testGivenAppContext_WhenInjected_ThenItShouldNotBeNull() Time elapsed: 0.009 sec <<< FAILURE!
org.opentest4j.AssertionFailedError: applicationContext should not be null ==> expected: not <null>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:39)
at org.junit.jupiter.api.Assertions.fail(Assertions.java:118)
at org.junit.jupiter.api.AssertNotNull.failNull(AssertNotNull.java:47)
at org.junit.jupiter.api.AssertNotNull.assertNotNull(AssertNotNull.java:36)
at org.junit.jupiter.api.Assertions.assertNotNull(Assertions.java:292)
at com.mycompany.mavenspringjunit5.MainTest.testGivenAppContext_WhenInjected_ThenItShouldNotBeNull(MainTest.java:24)
Tested it and tests run fine in my IDE (Intellij) running the same with your provided pom.xml fails.
You need to upgrade the maven-surefire-plugin to properly detect and execute the classes.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
This is also expressed in the Junit5 documentation.

Spring boot with Java 7 in Weblogic

I have a requirement to deploy the spring boot application in weblogic server 11g. The weblogic server supports only Java 7. Please assist me with the correct spring boot version and I get the following error if I use spring boot version 1.5.6.RELEASE.
On hover the following message gets displayed.
"Multiple markers at this line
- The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from
required .class files
- The type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from
required .class files"
Application.java
package com.example.ap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.WebApplicationInitializer;
#SpringBootApplication
#EnableAutoConfiguration
#ComponentScan({ "com.example.ap" })
public class Application extends SpringBootServletInitializer implements
WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
builder) {
return builder.sources(Application.class);
}
}
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>
<groupId>com.example.ap</groupId>
<artifactId>test</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath />
</parent>
<properties>
<java-version>1.7</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
ResourceController.java
package com.example.ap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/resource")
public class ResourceController {
#RequestMapping(method = RequestMethod.GET)
String readResource() {
return "hello!";
}
}
In src/main/webapp/WEB-INF folder, I have weblogic.xml and dispatcherServlet-servlet.xml
I have excluded the embedded tomcat, because I need to deploy it in weblogic. Please help me with finding the issue.
weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd">
<wls:context-root>sg-manutouch-lite-api</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
<wls:weblogic-version>10.3.6</wls:weblogic-version>
</wls:weblogic-web-app>
dispatcherServlet-servlet.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.xsd">
Weblogic 11g(10.3.4) will support servlet 2.5(max). If needed to create an application using servlet 2.5, then web.xml is mandatory. Spring boot's way of configuring the application by using SpringBootServletInitializer, WebApplicationInitializer will be supported from servlet 3.0 onwards only.
Thanks for the guidance M.Denium.

Spring support for WebHDFS

Is there any Spring support for wedhdfs? I didnt find any useful link on google.
I want to connect to hadoop with normal authentication and kerberos authentication via webhdfs. Is this supported in spring?
Any useful links will be helpful.
Thanks
Yes, Spring Data supports this. According to this documentation, it's possible to configure any supported Hadoop file system:
http://docs.spring.io/spring-hadoop/docs/current/reference/html/fs.html
SHDP does not enforce any specific protocol to be used - in fact, as
described in this section any FileSystem implementation can be used,
allowing even other implementations than HDFS to be used.
See below for a code sample that demonstrates auto-wiring a WebHDFS FileSystem instance into a command-line application. To run this, pass file paths as command line arguments, and it will list every file present at that path by calling FileSystem.listStatus.
The code sample is configured to connect to an unsecured WebHDFS instance with "simple" authentication. To connect to a WebHDFS instance secured with Kerberos, you'd set up the relevant configuration properties in the <hdp:configuration id="hadoopConfiguration" /> bean. Hadoop security configuration is a very large topic. Rather than repeat the information, I'll just point to the documentation in Apache:
http://hadoop.apache.org/docs/r2.4.1/hadoop-project-dist/hadoop-common/SecureMode.html
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-spring-hadoop</groupId>
<artifactId>test-webhdfs</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Test Spring Hadoop with WebHDFS</name>
<description>Test Spring Hadoop with WebHDFS</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.0.RELEASE</version>
</parent>
<repositories>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<properties>
<start-class>testwebhdfs.Main</start-class>
<java.version>1.6</java.version>
<hadoop.version>2.4.1</hadoop.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
</project>
src/main/resources/hadoop-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:hdp="http://www.springframework.org/schema/hadoop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
<hdp:configuration id="hadoopConfiguration" />
<hdp:file-system uri="webhdfs://localhost:50070" />
</beans>
src/main/java/testwebhdfs/Main.java
package testwebhdfs;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
#Configuration
#ImportResource("hadoop-context.xml")
public class Main implements CommandLineRunner {
#Autowired
private FileSystem fs;
#Override
public void run(String... strings) throws Exception {
Path[] paths = new Path[strings.length];
for (int i = 0; i < strings.length; ++i) {
paths[i] = new Path(strings[i]);
}
for (FileStatus stat: fs.listStatus(paths)) {
System.out.println(stat.getPath());
}
}
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}

Spring #Autowired working without context:annotation-config [duplicate]

This question already has answers here:
Difference between <context:annotation-config> and <context:component-scan>
(15 answers)
Closed 3 years ago.
I've tested the behavior of auto wiring in case the context:annotation-config element is missing in the application context xml file. To my surprise it worked just the same.
So here is my question:
How come an AutowiredAnnotationBeanPostProcessor is registered in the ApplicationContext even though the context:annotation-config element is missing from the application context configuration file, or what else mechanism makes this configuration work?
I'm using Spring version 3.0.6.RELEASE
This is the project pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples.spring</groupId>
<artifactId>spring-utility</artifactId>
<version>1.0.0.CI-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring Utility</name>
<url>http://www.springframework.org</url>
<description>
<![CDATA[
This project is a minimal jar utility with Spring configuration.
]]>
</description>
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.0.6.RELEASE</spring.framework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
This is the application context configuration file, with the context:annotation-config element commented out:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Example configuration to get you started.</description>
<!-- context:annotation-config/ -->
<context:component-scan base-package="com.foo.ch04" />
</beans>
A MessageProvider, that will be used as collaborator by a dependent bean MessageRenderer
package com.foo.ch04.helloworld;
import org.springframework.stereotype.Service;
#Service("messageProvider")
public class HelloWorldMessageProvider implements MessageProvider {
public String getMessage() {
return "Hello, World!";
}
}
The MessageRenderer, whose dependency messageProvider gets auto-injected:
package com.foo.ch04.helloworld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service("messageRenderer")
public class StandardOutMessageRenderer implements MessageRenderer {
private MessageProvider messageProvider;
public void render() {
if (messageProvider == null) {
throw new RuntimeException(
"You must set the property messageProvider before rendering message.");
}
System.out.println(messageProvider.getMessage());
}
#Autowired
public void setMessageProvider(MessageProvider provider) {
messageProvider = provider;
}
public MessageProvider getMessageProvider() {
return messageProvider;
}
}
The test application loading the application context and testing the messageRenderer:
package com.foo.ch04.helloworld;
import org.springframework.context.support.GenericXmlApplicationContext;
public class DeclareSpringComponents {
public static void main(String[] args) {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("classpath:META-INF/spring/app-context-annotation.xml");
context.refresh();
MessageRenderer renderer = context.getBean("messageRenderer",
MessageRenderer.class);
renderer.render();
}
}
Even though the is missing in the application context configuration file, the message "Hello, World!" is written to stdout when the application is run.
The use of <context:component-scan /> implies annotation based configuration and as such specifying <context:annotation-config/> is redundant.
'context:annotation-config' This annotation-config tag is used to process the auto wired beans declared in the application context XML file. If the auto wired bean could be discovered using the scope of 'context:component-scan' tag then no need to use 'context:annotation-config' tag

Can find imported Spring beans from JAR, but not from WAR

My application consists of three Maven projects (I am omitting other modules)): FrameworkBase (JAR), FrameworkBaseImpl (JAR), FrameworkRestService (WAR).
FrameworkRestService is a plugin to FrameworkBaseImpl, drawing its class/interface definitions
from FrameworkBase, which FrameworkBaseImpl implements/extends. (I had to split FrameworkBase to avoid Maven curcular dependencies).
FrameworkBaseImpl has a main, which invokes Jetty, passing the FrameworkRestService war.
I am trying to have Spring inject a RestService instance as a field member in the (sole) instance of FrameworkMain class of FrameworkBaseImpl. My Spring points to FrameworkRestService class inside FrameworkRestService project as implementing the restService bean.
And here is the problem: Although Spring seems to find the imported framework_rest_service.context file in the WAR that contains the bean it fails injecting it (no bean named restService is found). But if I convert FrameworkRestService into a JAR - it magically succeeds! I am stuck for 2 days on it! Please help!
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 snippet:
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();
....
}
Sorry for the long question. :-)
It wasn't a class loader issue. I had the WAR and the JAR use the same class loader (of the JAR). Somebody commented that I shouldn't have tried to link a WAR and look for beans in there.
So instead of trying to solve the problem I simply bypassed it by canceling Spring dependencies between the JAR and the WAR. In the WAR I still needed a reference to the FrameworkMain singleton instantiated in the JAR.
I "hacked" this as follows: I added a class named Holder with public static methods get and set the FrameworkMain instance singleton. I call get from the WAR, and set from the jar. I could not add this to FrameworkMain, which is actually an interface in FrameworkBase jar (implemented by FrameworkMainImpl in the FrameworkBaseImpl jar).
Thanks.

Resources