Loading spring application context files that are inside a jar in classpath - spring

I am trying to use ClassPathXmlApplicationContext in my java standalone code to load applicationContext.xml that is inside a jar file which is in my class path.
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/applicationContext*.xml");
applicationContext.xml entry as follows,
<bean id="myAdder" class="com.foo.bar.MyAdder">
<property name="floatAdder" ref="floatAdder"/>
</bean>
And, when I try to load a bean that way I am getting NoSuchBeanException. Can't a bean by loaded in this way?
The jar file is added to my classpath as a maven dependency. When I see the Java Build Path in Eclipse for this project, I see this jar linked as M2_REPO/.../..
I was assuming I can load the bean inside the jar file as the jar is in classpath this way. Am I missing something?
Thanks,Abi

This should work, I just created the 2 projects and checked.
Project A (standard Maven project created with STS) has applicationContext.xml in src/main/resources.
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>org.D</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
applicationContext.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="myAdder" class="com.foo.bar.MyAdder">
<property name="foo" value="bar" />
</bean>
</beans>
Project B:
pom.xml: same as A, except A is added as dependency:
<dependency>
<groupId>org.D</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Start.java in project B:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:**/applicationContext*.xml");
MyAdder myAdder = (MyAdder) context.getBean("myAdder");
System.out.println(myAdder.getFoo());
}
mvn install A first, then run Start in project B.

Do you really need the classpath*: prefix on that location? (Is that * legal?) I would have expected something more like:
ApplicationContext context = new ClassPathXmlApplicationContext("**/applicationContext*.xml);

Related

Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1] [ERROR]

I've got a new project from spring boot initializer, the first run he work fine, but the next run i have this error : please add a mainClass,
The project is a spring boot application with kotlin and spring web.
The main class is in the project and i don't change the pom which is defined from spring boot initializer.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.6.0</kotlin.version>
</properties> ....
Do you have the the application class ?
MyApplication.java
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
If no, add this class in root
If class already exists try to run
mvn clean package spring-boot:repackage
I've downloaded a new spring initializer project and copied my files from the old project. It is now working fine for me. I think the problem is my intellij configuration is bad.

Getting error while implementing a project to start spring cloud dataflow server on local

I am implementing very basic POC to start spring cloud dataflow server on local.
But I am getting below error :
Field appRegistry in
org.springframework.cloud.dataflow.completion.CompletionConfiguration
required a bean of type
'org.springframework.cloud.dataflow.registry.service.AppRegistryService'
that could not be found. The injection point has the following
annotations:
-#org.springframework.beans.factory.annotation.Autowired(required=true)
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ms</groupId>
<artifactId>abc-ui</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>abc-ui</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<properties>
<java.version>8</java.version>
<h2.version>1.4.193</h2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-dataflow-server</artifactId>
<version>2.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</project>
Java file :
#EnableDataFlowServer
#SpringBootApplication(exclude = {CloudFoundryDeployerAutoConfiguration.class})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
Is there any way to disable "AppRegistry" bean configuration?
I have implemented same project in another system and it is working fine.
Please let me know what mistake I am making.
The AppRegistryService is a required component for SCDF as it is the source of truth for all the applications registered for Stream and Task applications.
Hence, there is no way to disable it explicitly. But, you can still have a different implementation of AppRegistryService and have it override the existing one though.

Unable to access the spring boot rest service from the WL server

I am very new to spring boot stuff. My question is about how to access a spring boot rest service deployed on weblogc server. I have successfully deployed (at least it seems from the WL console) the spring boot war file to the weblogic 12c container. But I am not able to access the service.
I am trying to use the following url to access:
http://host:port/myweb/resource/hello -- I am also not sure whether I should use WL server port on which the war is deployed or 8080 (default spring-boot port)
I have given all the code below. Am i missing anything? I would greatly appreciate any help
POM file:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.demo</groupId>
<artifactId>SpringBootWL1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootWL1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<start-class>com.example.demo.SpringBootWl1Application</start-class>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Rest Controller:
#RestController
#RequestMapping("/resource")
public class ResourceController {
#GetMapping("/hello")
String home() {
return "Hello World, How are you!";
}
Spring Boot Application
#SpringBootApplication
public class SpringBootWl1Application extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootWl1Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringBootWl1Application.class);
}
}
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://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
<wls:context-root>/myweb</wls:context-root>
<wls:container-descriptor>
<!-- <wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes> -->
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
<wls:package-name>com.fasterxml.jackson.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
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">
</beans>
Your configuration seems ok. The default port of Weblogic is 7001, and every web application is deployed using this port.
To verify the deployment you can access to the console from (http://host:7001/console) and go to the Deployments section, there you must find your application deployed and it must be in State OK. Your can check there your web application context there.
One thing I noticed is your file is called: Weblogic.xml, but it must be called weblogic.xml all lower case.
Let me know if it is not working.

mac maven cannot find symbol wrong servlet-api.jar

I compile spring webflow samples project using maven in mac os and got errors. (The project compiled successfully in windows)
So I create a simple maven project to reproduce the error.
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</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<dependencies>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
src/main/java/Test.java
import javax.servlet.ServletContext;
public class Test {
public void init(ServletContext context) {
context.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
}
}
mvn compile
The error is:
I thought is was something that maven used a wrong servlet-api.jar but not servlet as pom dependency specified which is correct. (I use javac -cp javax.servlet-api-3.0.1.jar Test.java, result no error)
I check mvn dependency:tree, the result show correct servlet-api-3.0.1.jar.
I also chech mvn script to see if there is some option to set classpath.
But I just can't figure out how maven use which jars as it's classpath.
It's very weird.
After I've imported this test maven project into eclipse,
I run mvn compile again, no error happened.

Spring Batch Admin (2.0.0.M1): error creating bean batchMBeanExporter

I am trying to run spring batch admin from spring boot application.
I am using this excellent example from Michael (#mminella). This application works fine.
However, I need similar application using maven; I went ahead and created one with same files, jobs, configs and dependencies. But I ended up in following exception.
Only difference I see is the version of spring-batch-core. With maven it's version is 3.0.6 while with gradle (Michael's application) has 3.0.4.
Not sure what's going on when I use maven; really appreciate your help. Please let me know if any additional details required.
Looking forward for your inputs.
Thank you,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchMBeanExporter' defined in URL [jar:file:/C:/Users/Admin/.m2/repository/org/springframework/batch/spring-batch-admin-manager/2.0.0.BUILD-SNAPSHOT/spring-batch-admin-manager-2.0.0.BUILD-SNAPSHOT.jar!/META-INF/spring/batch/bootstrap/manager/jmx-context.xml]: Cannot create inner bean 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c' of type [org.springframework.aop.framework.ProxyFactoryBean] while setting bean property 'jobService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c': Post-processing of FactoryBean's object failed; nested exception is java.lang.IllegalStateException: #Scheduled method 'removeInactiveExecutions' found on bean target class 'SimpleJobService' but not found in any interface(s) for a dynamic proxy. Either pull the method up to a declared interface or switch to subclass (CGLIB) proxies by setting proxy-target-class/proxyTargetClass to 'true'.
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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-batch-admin.version>2.0.0.BUILD-SNAPSHOT</spring-batch-admin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-manager</artifactId>
<version>${spring-batch-admin.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-domain</artifactId>
<version>${spring-batch-admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>sqljdbc4</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots-local</id>
<name>Spring Maven Snapshot Repository</name>
<url>https://repo.spring.io/libs-snapshot-local/</url>
</repository>
</repositories>
</project>
Spring Boot config
#SpringBootApplication(exclude = {HypermediaAutoConfiguration.class, MultipartAutoConfiguration.class})
#EnableBatchAdmin
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
In order to avoid, JobController null pointer exception, put below
server.servletPath=/*
Best regards,
Jigar.
I finally found a way to make the annotation #EnableBatchAdmin in a maven project.
This allow you to fix the following error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchMBeanExporter' defined in URL [jar:file:/C:/Users/Admin/.m2/repository/org/springframework/batch/spring-batch-admin-manager/2.0.0.BUILD-SNAPSHOT/spring-batch-admin-manager-2.0.0.BUILD-SNAPSHOT.jar!/META-INF/spring/batch/bootstrap/manager/jmx-context.xml]: Cannot create inner bean 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c' of type [org.springframework.aop.framework.ProxyFactoryBean] while setting bean property 'jobService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c': Post-processing of FactoryBean's object failed; nested exception is java.lang.IllegalStateException: #Scheduled method 'removeInactiveExecutions' found on bean target class 'SimpleJobService' but not found in any interface(s) for a dynamic proxy. Either pull the method up to a declared interface or switch to subclass (CGLIB) proxies by setting proxy-target-class/proxyTargetClass to 'true'.
The solution was found in this issue https://github.com/spring-projects/spring-batch-admin-samples/issues/12
and is to override the jmx-context.xml file by the following
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:mbean-server id="mbeanServer"/>
<context:mbean-export default-domain="spring.application" server="mbeanServer"/>
<!-- Override the `batchMBeanExporter` and set the `proxyTargetClass` property to true on the `jobService` proxy.
This is a workaround for the related https://jira.spring.io/browse/BATCHADM-126 -->
<bean id="batchMBeanExporter" class="org.springframework.batch.admin.jmx.BatchMBeanExporter">
<property name="server" ref="mbeanServer"/>
<property name="jobService">
<bean class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="jobService"/>
<property name="proxyTargetClass" value="true"/>
</bean>
</property>
<property name="defaultDomain" value="spring.application"/>
</bean>
by placing this code in your own jmx.context.xml file in main/resources/META-INF/spring/batch/override
Finally, I was able to run the spring batch with spring batch admin; though with this project. Still not able to use # EnableBatchAdmin with maven build. Many thanks to mminella and #vesperaba.
Though I am facing another issue; when I run the project with 2.0.0.M1 from repository, it works fine. However, when I build project locally after resolving [JobController null pointer exception] (https://github.com/spring-projects/spring-batch-admin/pull/42);
I have to include "spring-batch-admin-resources" in POM explicitly;
while this was not the case when I run the application using
repository (libs-snapshot-local) 2.0.0.M1
build.
When I include "spring-batch-admin-resources" application is not
able to start saying that "cvc-datatype-valid.1.2.1:
'/job-configuration' is not a valid value for 'NCName'."
Any comments will be appreciated. Thank You.

Resources