Spring-boot scheduler runs without #EnableScheduling annotation - spring

I was following this example to create a scheduled task in a sample project: https://spring.io/guides/gs/scheduling-tasks
It says, #EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
But, by mistake i didn't use it. How come it still works?
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.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</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>
Other Classes:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
#Component
public class ScheduledTaskRunner {
Logger log = LoggerFactory.getLogger(ScheduledTaskRunner.class);
#Scheduled(cron = "0/1 * * * * *")
public void run(){
log.info("Hello");
}
}
Isn't the scheduled task supposed to not run?

Your code added the spring-boot-starter-actuator when that is added a scheduler is added as well. This scheduler is used by the actuator to schedule the automatic export of the metrics.
The code for this is in the MetricExportAutoConfiguration and has been added in Spring Boot 1.3.0.
So if you remove the actuator scheduling won't work anymore.

Related

Spring boot gives AbstractJdbcConfiguration errors on JUnit Test

Im trying to test my Repository with junit.
I have parent project Maven and some subproject for user activty.
this is parent 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>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.testpr</groupId>
<artifactId>testprWebParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>testprWebParent</name>
<description>parent testpr web parent</description>
<properties>
<java.version>19</java.version>
</properties>
<modules>
<module>testprBackEnd</module>
<module>testprFrontEnd</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>com.testprcommon</groupId>
<artifactId>testprCommon</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
and this is a child or subproject 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>com.testpr</groupId>
<artifactId>testprWebParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>testprBackEnd</artifactId>
<name>testprBackEnd</name>
<description>testpr Admin Project</description>
<properties>
<java.version>19</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The child project is in the parent folder and also used tag from parant project.
I made a test class in child project inside eclipse and if i run Junit it gives me this
error :
java.lang.NoClassDefFoundError: org/springframework/data/jdbc/repository/config/AbstractJdbcConfiguration .....
Okey kinda confusing error log but after i change my test class from this
#DataJdbcTest
#AutoConfigureTestDatabase(replace = Replace.NONE)
public class RoleRepositoryTests {
#Autowired
private RoleRepository repo;
#Test
public void testCreateFirstRole() {
Role roleAdmin = new Role("Admin","For Test");
Role savedRole = repo.save(roleAdmin);
assertThat(savedRole.getId()).isGreaterThan(0);
}
}
to
#SpringBootTest
#AutoConfigureTestDatabase(replace = Replace.NONE)
public class RoleRepositoryTests {
#Autowired
private RoleRepository repo;
#Test
public void testCreateFirstRole() {
Role roleAdmin = new Role("Admin","For Test");
Role savedRole = repo.save(roleAdmin);
assertThat(savedRole.getId()).isGreaterThan(0);
}
}
And my application.properties from this :
spring.jpa.properties.hibernate.dialect = com.mysql.cj.jdbc.Driver
to this :
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
Junit Test Runs Successfully.

NotFound Failure in Spring Integration Test of Health Indicator

I want to write a simple test for a custom HealthIndicator, which is defined in its own module separated from the module of the Spring application.
The HealthIndicator is pretty simple:
#Component
public class MyHealthIndicator implements HealthIndicator {
public Health health() {
return Health.up().withDetail("my-health", "okay").build();
}
}
This is the test:
#WebAppConfiguration
#ContextConfiguration(classes = {TestConfiguration.class, MyHealthIndicator.class})
#ExtendWith(SpringExtension.class)
class MyHealthIndicatorTests {
#Autowired
private MockMvc mockMvc;
#Test
public void healthTest() throws Exception {
mockMvc.perform(get("/management/health")).andExpect(status().isOk());
}
}
And this is the simple Test Configuration:
#Configuration
#EnableWebMvc
#ImportAutoConfiguration(classes = {HealthEndpointAutoConfiguration.class, MockMvcAutoConfiguration.class})
#TestPropertySource(properties = {"debug:true", "management.endpoints.enabled-by-default:true",
"management.endpoints.web.base-path:/management", "management.endpoints.web.exposure.include=health"})
public class TestConfiguration {
}
The test of the health endpoint returns a 404 - Not Found.
In the logs I see that the TestDispatcherServlet has no mapping for the health endpoint:
12:48:38.309 [main] WARN org.springframework.web.servlet.PageNotFound - No mapping for GET /management/health
but why?
In the logs I see also that the HealthEndpoint actually is created:
12:48:38.013 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'healthEndpoint'
But the mapping in the TestDispatcherServlet seems not to be configured. How can I enable the mapping?
Please note: I want to test my Health Indicator without a SpringBootApplication. Isn't that possible at all?
Edit:
Nothing special in the POM:
<?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.3.1.RELEASE</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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</project>
Looks like a typo in defining the test property source.
Instead of
management.endpoints.web.base-path:/management
management.endpoints.enabled-by-default:true
it should be
management.endpoints.web.base-path=/management
management.endpoints.enabled-by-default=true

property read from application.properties returning null

I am new to springboot and i am trying to read the property value from the application.properties file in the location(src/main/resources). But it always return null. I need help for the same. Attaching the classes and property files.
Please note: I have tried different ways from "https://www.baeldung.com/properties-with-spring "How to access a value defined in the application.properties file in Spring Boot".
But this is not helping in getting the value from the application.properties file.
#SpringBootApplication
#EnableAutoConfiguration
#PropertySource("classpath:application.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SampleTest ap=new SampleTest();
System.out.println("+++++++++++++ "+ap.returnvalue());
}
#Configuration
#PropertySource("classpath:application.properties")
public class SampleTest {
#Value("${testValue}")
String value1;
public String returnvalue()
{
return value1;
}
}
application.properties file
testValue=Value from application
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>ReadValue</groupId>
<artifactId>com.read.vale</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.read.vale</name>
<description>Demo project for Spring Boot</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>
<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>
</project>
Try this:
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
SampleTest ap = ctx.getBean(SampleTest.class);
System.out.println("+++++++++++++ "+ap.returnvalue());
}

spring #controller vs #RestController error whitelabel error

I created a basic Spring MVC project in STS IDE.
When I try to add a controller I add the annotation #Controller and run the spring boot program, I get the following error:-(Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.), but when I add a #RestController the method returns the 'hello' string. Please help me out on how to fix it using #Controller and please let me know why this happened.
Sample code:
package com.adithya.spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Controller
public class ApplicationController {
#RequestMapping("/")
public String Hello() {
return "Hello";
}
}
Also, my pom.xml file is as below:
<?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.adithya</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyApplication</name>
<description>Login & Reg</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Project directory structure .

SpringBootApplication cannot be resolved to a type

Hi below is my Spring code and pom.xml. Maven is not able to update even though I tried to force project update. Any resolution on this or direction where I am getting wrong.
package com.boot;
import org.springframework.*;
#SpringBootApplication
public class App
{
public static void main( String[] args )
{
System.out.println("Hello World");
}
}
pom
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<name>das-boot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.boot.App</start-class>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I am not getting why Maven is not able to get the dependencies for my project. A
I faced the same issue.
I changed the version from 2.0.5.RELEASE to 2.0.3.RELEASE to make it work
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
If you are fine to move to 2.x then please follow below steps:
Step 1 copy below pom
<?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.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>das-boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Here is your main application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DasBootApplication {
public static void main(String[] args) {
SpringApplication.run(DasBootApplication.class, args);
}
}
or you can also stay with 1.4.2, just change java version to 1.6.
You should provide the class name in the run() method like below:
#SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(App.class, args);
Sytem.out.print("Started");
}
And remove the below line from pom.xml
<start-class>com.boot.App</start-class>

Resources