Spring boot Jar to war - spring-boot

I am trying to change the spring music application (https://github.com/cloudfoundry-samples/spring-music) from jar to war file to test in Liberty.
I did the following change
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
and also build.gradle
apply plugin: 'war'
dependencies {
// Spring Boot
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
The war files get created,but while trying to access the application,it gives the error
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.repository.CrudRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Am I missing anything here ?

If you want to use CrudRepository you need to add a dependency to
spring-boot-starter-data-jpa like
compile("org.springframework.boot:spring-boot-starter-data-jpa")

Related

spring-cloud-stream sample projects raises NoSuchBeanDefinitionException of KafkaStreamsFunctionProcessor

I'm trying to touch spring-cloud-stream, and creating a sample project of the official blog.
Implementation is totally same as the article.
#SpringBootApplication
public class SimpleConsumerApplication {
#Bean
public java.util.function.Consumer<KStream<String, String>> process() {
return input ->
input.foreach((key, value) -> {
System.out.println("Key: " + key + " Value: " + value);
});
}
}
I've selected Cloud Stream and Spring for Apache Kafka Stream on Spring initializr, and added ShadowJar. Now my build.gradle is like this.
plugins {
id 'org.springframework.boot' version '2.4.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'com.github.johnrengelman.shadow' version '6.1.0'
}
group = 'com.lipsum'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
jar {
manifest {
attributes('Main-Class': 'com.lipsum.kafkastream.KafkastreamApplication')
}
}
shadowJar {
archiveBaseName.set('kafka-stream-practice')
archiveClassifier.set('')
archiveVersion.set('')
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2020.0.2")
}
dependencies {
implementation 'org.apache.kafka:kafka-streams'
implementation 'org.springframework.cloud:spring-cloud-stream'
implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka-streams'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
I execute the uber jar, but springboot application fails to recognize the bean.
$ java -jar kafka-stream-practice.jar --spring.cloud.stream.bindings.process-in-0.destination=kafka-stream-practice
...
22:47:21.162 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'kafkaStreamsFunctionProcessorInvoker' defined in class path resource [org/springframework/cloud/stream/binder/kafka/streams/function/KafkaStreamsFunctionAutoConfiguration.class]: Unsatisfied dependency expressed through method 'kafkaStreamsFunctionProcessorInvoker' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsFunctionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsFunctionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
I don't think the implementation has any problems. Do I miss some dependencies?
I tried a quick maven project from the initialize and it starts fine. There was a known bug on the last release (3.0.11) which was fixed since then on the snapshot. You can fix the issue by adding the boot actuator dependency to the project or by upgrading the binder to the latest snapshot. Could you try the maven approach? If the problem still persists, please share a reproducible sample, and then we will take a look.
It works after removing shadowJar and instead uses bootJar task of Spring Boot Gradle plugin.

Quarkus dependency injection failing with bean from Gradle Library Module

I´m trying to get dependency injection working in my multi-module project where I want to inject a bean from a library module. However, it´s failing because it cannot find the bean.
project root settings.gradle
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
include ':service-module'
include ':library-module'
service-module build.gradle
Tried compile, as well as implementation
dependencies {
compile project(":library-module")
// implementation project(":library-module")
}
Bean from library-module
import javax.enterprise.context.ApplicationScoped;
#ApplicationScoped
public class LibraryBean {
public void hello() {
System.out.println("Hello");
}
}
service-module where injection happens
#ApplicationScoped
public class Application {
#Inject
LibraryBean libraryBean;
}
Stacktrace
Unsatisfied dependency for type com.mylibrary.LibraryBean and
qualifiers [#Default]
- java member: com.myservice.Application#LibraryBean
- declared on CLASS bean [types=[com.myservice.Application, java.lang.Object], qualifiers=[#Default, #Any],
target=com.myservice.Application]
I´m not sure if this issue is Quarkus-related or a general problem that exists with CDI and Gradle modules.
How can I make the DI working accross modules?
Do you have a beans.xml file in your library module? See this question/answer for more info: https://stackoverflow.com/a/55513723/742081

SpringBoot EntityManagerFactory not found

So, trying to run first sprinboot application with JPA implementation and got the following error :
Description:
Field personneDAO in com.example.demo.controller.PersonneController required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
tried to add #Repositoy on my repo, and #EnableJpaRepositories to the main, but it doesn't helps...
the repository (nothing too fancy):
#Repository
public interface PersonneDAO extends JpaRepository<Personne, Integer>{
}
the main :
#SpringBootApplication
#EnableJpaRepositories(basePackages ="com.example.demo.repository")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
gradle dependencies :
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.16.Final'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '2.0.6.RELEASE'
testCompile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.5.1.jre9-preview'
}
Any ideas guys? :/
PS : forgot the application.properties
spring.mvc.view.prefix = /WEB-INF/
spring.mvc.view.suffix = .jsp
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:sqlserver://localhost/DB_TEST
spring.datasource.username=sa
spring.datasource.password=Pa$$w0rd
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
JPA will not get auto-configured without spring-boot-starter-data-jpa as a dependency. With the dependency in place, you shouldn't even need #EnableJpaRepositories.
Also, you may remove spring-data-jpa as spring-boot-starter-data-jpa already depends on that.

IllegalStateException occured when deployed war in tomcat with spring boot 2.0.1

I packaged a war with gradle + boot 2.0.1, gradle script as below
plugins {
id 'org.springframework.boot' version '2.0.1.RELEASE'
id 'java'
id 'war'
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
repositories {
mavenCentral()
}
bootWar{
mainClassName = 'api.abroad.Application'
}
configurations {
providedRuntime
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
The Application class with main function as below
#SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
and I also wirted a class extends SpringBootServletInitializer to override configure function, although it was not required in docs.
public class ServletInitializer extends SpringBootServletInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
I installed tomcat 8.5.29 in my windows, and deployed the war. An exception occured as below
2018-04-09 14:40:19.978 INFO 11720 --- [ost-startStop-1] com.tbiedu.abroad.ServletInitializer : Started ServletInitializer in 4.113 seconds (JVM running for 10.228)
2018-04-09 14:40:19.989 INFO 11720 --- [ost-startStop-1] c.t.abroad.controller.DemoController : Root context already created (using as parent).
09-Apr-2018 14:40:19.992 严重 [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/abroad-service-api]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: No SpringApplication sources have been defined. Either override the configure method or add an #Configuration annotation
at org.springframework.util.Assert.state(Assert.java:73)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:127)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:87)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5204)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
Did i missed something...
you need to place #Configuration above this class
#Configuration//--> this line u need to place
public class ServletInitializer extends SpringBootServletInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}

converting a simple maven project to a spring boot project from maven

use eclipse,I new a maven project.
After,I replace pom to the spring boot pom and create counter java file.
But when I run this application,it went error:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.yao.service.PersonInfoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 31 more
I think this maybe coused by some config,but I don't know what config to.
Supplement: When I Generate the demo project from here, the same code is run normal.
This is my main class:
#ComponentScan(basePackages = { "com.yao" })
#SpringBootApplication
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}

Resources