#EventListener spring boot fails to compile - java

Wrote my first Spring Boot app that count number of refreshes. I print to port 8080 "Hello World" + Counter.
Counter should increase at each user refresh and show on live the number of refreshes after the "Hello World" string.
For this goal I am using the #RestController and #EventListener interfaces.
When building my app, it fails on #EventListener import and interface call:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project backend: Compilation failure: Compilation failure:
[ERROR] /home/project/backend/src/main/java/com/iz/backend/myController.java:[9,58] error: cannot find symbol
[ERROR] package org.springframework.cloud.context.config.annotation
[ERROR] /home/project/backend/src/main/java/com/iz/backend/myController.java:[57,5] error: incompatible types: EventListener cannot be converted to Annotation
Class:
package com.iz.backend;
import org.springframework.stereotype.Controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.config.annotation.EventListener;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
#RefreshScope
#RestController //interface
public class myController {
static int counter = -1;
#RequestMapping("/")
public String helloWorld() {
try
{
return "Hello World " + counter;
}
catch (IOException e)
{
e.printStackTrace();
}
return "Failed to execute";
}
#EventListener
public void handleContextRefreshEvent(ContextRefreshedEvent event) {
counter += 1;
}
}
pom.xml 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 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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.iz</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>backend</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
MAVEN 3.6.3, JAVA 11

I suggest you to do the following:
Remove the try catch block (Exception 'java.io.IOException' is never thrown in the corresponding try block)
Optimize the imports (you have duplicate/ambigous imports)
Use something like an AtomicInteger as counter

Related

Unit testing java maven

I have a maven project that looks like this:
If I run in the terminal:
mvn test
it will build the application and run the 3 tests:
SocialMultiplicationApplicationTests
MultiplicationServiceTest
RandomGeneratorServiceTest
but not RandomGeneratorServiceImplTest.
If I try to explicitly run this class:
mvn test -Dtest=RandomGeneratorServiceImplTest
I get:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test)
on project social-multiplication: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
Here's my 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 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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>microservices.book</groupId>
<artifactId>social-multiplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>social-multiplication</name>
<description>Social Multiplication App</description>
<properties>
<java.version>1.8</java.version>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here's the test class that won't run:
package microservices.book.multiplication.service;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class RandomGeneratorServiceImplTest {
private RandomGeneratorServiceImpl randomGeneratorServiceImp;
#Before
public void setUp(){
randomGeneratorServiceImp = new RandomGeneratorServiceImpl();
}
#Test
public void generateRandomFactorIsBetweenExpectedLimits() {
List<Integer> randomFactors = IntStream.range(0,1000)
.map(i -> randomGeneratorServiceImp.generateRandomFactor())
.boxed()
.collect(Collectors.toList());
for(Integer i: randomFactors){
assertThat(i).isIn(
IntStream.range(11, 100).
boxed().collect(Collectors.toList()));
}
}
}
EDIT: SOLUTION
The proble was indeed the conflict between junit4 and junit5.
I chose to move my tests to junit5, hence I replaced #Before with #BeforeAll and added #TestInstance(Lifecycle.PER_CLASS) on top of the RandomGeneratorServiceImplTest class definition.
Couple links I found useful:
junit docs
differences between junit4 and junit5
The reason lies in mixing JUnit 5 and JUnit 4.
The maven-surefire-plugin picks only one strategy to execute tests and favors JUnit 5 in this case.
Either replace the dependency to junit by junit-vintage-engine:
<!-- <dependency>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
Or adapt your tests to use JUnit 5 instead (as for example in RandomGeneratorServiceImplTest):
...
//import org.junit.Before;
//import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
...
public class RandomGeneratorServiceImplTest {
...
// #Before
#BeforeEach
public void setUp(){
...
}
// #Test can stay as is, only import needs to be updated
#Test
public void generateRandomFactorIsBetweenExpectedLimits() {
...
}
}
Try adding this plugin to your build section in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

Getting error "Whitelabel Error Page" while running localhost:8080

I started learning Spring Framework and get this kind of error while running localhost:8080
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Controller.java
package com.springFramework.helloGame.controller;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class Controller {
#GetMapping("/sum")
public long displaySum(){
return 100;
}
}
HelloGameApplication.java
package com.springFramework.helloGame;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
public class HelloGameApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(HelloGameApplication.class, args);
}
}
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 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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springFramework</groupId>
<artifactId>helloGame</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloGame</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
my project structure looks fine, but don't know why I got error!
Please help me!
The Whitelabel page is a spring error handling method and it tries to hint to you that you do have a mapping for /error.
Now coming to the issue you need to write #RequestMapping("/") above your get mapping in the controller so that the spring knows the base URI.
It's an entry point for the spring application for the browser.
Now you have written /sum get URI and in the browser, you are trying for http://localhost:8080
So for the above Uri to work you need to provide #RequestMapping("/")
or you can use http://localhost:8080/sum with you current code.
It seems your controller only have a mapping for "/sum" path. Try querying localhost:8080/sum.

Whitelabel Error Page There was an unexpected error (type=Not Found, status=404, whenever i return an HTML page from #requestMapping Controller,

https://github.com/hardikSinghBehl/jokes-app
The Project Structure
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hardik</groupId>
<artifactId>chuck-norris-jokes-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring-Boot-chuck-norris-jokes-app</name>
<description>Project using spring boot and actuator to create a jokes application</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>guru.springframework</groupId>
<artifactId>chuck-norris-for-actuator</artifactId>
<version>0.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hardik.chucknorrisjokesapp.services.JokeService;
#Controller
public class JokeController {
private JokeService jokeService;
public JokeController() {
super();
}
#Autowired
public JokeController(JokeService jokeService) {
super();
this.jokeService = jokeService;
}
#RequestMapping({"/",""})
public String showJoke(Model model){
model.addAttribute("joke", jokeService.getJoke());
return "chucknorris";
}
}
Service class
import org.springframework.stereotype.Service;
import guru.springframework.norris.chuck.ChuckNorrisQuotes;
#Service
public class ChuckNorrisJokeService implements JokeService{
private ChuckNorrisQuotes chuckNorrisQuotes;
public ChuckNorrisJokeService() {
super();
this.chuckNorrisQuotes = new ChuckNorrisQuotes();
}
public ChuckNorrisJokeService(ChuckNorrisQuotes chuckNorrisQuotes) {
super();
this.chuckNorrisQuotes = chuckNorrisQuotes;
}
#Override
public String getJoke(){
return chuckNorrisQuotes.getRandomQuote();
}
}
code works fine, i don't get any error, but when i go to localhost, the 404 error message is displayed
ChuckNorrisQuotes class is available in jar file added in dependencies
have attatched my git repository for this project, have tried everything and i am stuck, tried getMapping annotation, tried #getParam
when i am using #RestComponent, then local host is returning "chucknorris" as a String on screen rather than the html file in resources/templates

Spring Boot app: Could not resolve placeholder in application.properties?

I have a spring boot app. It works when fine when I click "run" button in SpringToolSuit4.
But when I run as "maven install", I got the error message:
<error message="Failed to load ApplicationContext" type="java.lang.IllegalStateException">java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'envVersionNum' in value "${envVersionNum}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'envVersionNum' in value "${envVersionNum}"
</error>
The content of my application properties:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true
spring.thymeleaf.cache=false
version=${envVersionNum}
If I change it to
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true
spring.thymeleaf.cache=false
version=1.1.1
It works fine. But I want to get the envVersionNum from user, which is stored in .bash_profile (export envVersionNum= 1.1.1).
the content of 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 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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jrp</groupId>
<artifactId>project-management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project-management</name>
<description>project management application</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The code of homeController:
package com.jrp.pma.controllers;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jrp.pma.dao.EmployeeRepository;
import com.jrp.pma.dao.ProjectRepository;
import com.jrp.pma.dto.ChartData;
import com.jrp.pma.dto.EmployeeProject;
import com.jrp.pma.entities.Project;
#Controller
public class HomeController {
#Value("${version}")
private String ver;
#Autowired
ProjectRepository proRepo;
#Autowired
EmployeeRepository empRepo;
#GetMapping("/")
public String displayHome(Model model) throws JsonProcessingException {
model.addAttribute("versionNumber", ver);
Map<String, Object> map = new HashMap<>();
// we are querying the database for projects
List<Project> projects = proRepo.findAll();
model.addAttribute("projectsList", projects);
List<ChartData> projectData = proRepo.getProjectStatus();
// Lets convert projectData object into a json structure for use in javascript
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(projectData);
//[["NOTSTARTED", 1], ["INPROGRESS", 2], ["COMPLETED", 1]]
model.addAttribute("projectStatusCnt", jsonString);
// we are querying the database for employees
List<EmployeeProject> employeesProjectCnt = empRepo.employeeProjects();
model.addAttribute("employeesListProjectsCnt", employeesProjectCnt);
return "main/home";
}
}
Any suggestions gratefully accepted.
If you're setting any env variable in export command then it it visible only through the command line only.
If you're using Eclipse or any other IDE then it won't get the values from the export command because the scope of the export is inside the terminal.
Try by running your jar after exporting the variables in command line it will work.

Spring Load Time Weaving Not Working on #Configurable object

Basically, what I want in the end, is that an object in some piece of code is wired up with my Spring Component, although that object is not created / managed by Spring.
The object in question is:
package demo;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
#Configurable(autowire= Autowire.BY_TYPE)
public class NotSoSpring {
SpringComponent springComponent;
public NotSoSpring() {
springComponent.test();
}
#Autowired
public void setSpringComponent(SpringComponent springComponent) {
this.springComponent = springComponent;
}
public void test() {
springComponent.test();
}
}
And the component is as simple as that:
package demo;
import org.springframework.stereotype.Component;
#Component
public class SpringComponent {
public void test() {
System.out.println("Hello. Wow, this works!");
}
}
The Spring configuration is done by the following piece of code:
package demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
#SpringBootApplication
#EnableSpringConfigured
#EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
#EnableAspectJAutoProxy
#ComponentScan(basePackages = "demo")
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
public void run(String... strings) throws Exception {
NotSoSpring spring = new NotSoSpring();
spring.test();
}
}
And here's the project's configuration:
<?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>org.test</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.2.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
That's all the files, and here's the JVM command line option I used:
-javaagent:c:/.m2/repository/org/springframework/spring-instrument/4.1.5.RELEASE/spring-instrument-4.1.5.RELEASE.jar
So this results in
Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at demo.DemoApplication.main(DemoApplication.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
at demo.NotSoSpring.<init>(NotSoSpring.java:13)
at demo.DemoApplication.run(DemoApplication.java:24)
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
... 10 more
I looked through other SO questions, the Spring Documentation and a lot of blogs, but unfortunately they usually refer to Spring 3 and to XML based configurations.
Might be related to this question which has no solution either.

Categories