spring boot postconstruct not called when running java -jar - spring-boot

I have the following class which has postconstruct.. But it doesnt seem to call the postconstruct? But it works in the eclipse IDE? But when i call it via java -jar... nothing
#SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#PostConstruct
public void postcontruct() {
System.out.println("******************TEST ********");
}
}

Related

spring boot "#ComponentScan(nameGenerator = CustomGenerator.class)" not working jpaRepository bean

#ComponentScan(nameGenerator = CustomGenerator.class)
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
if you do it with the above code, the name of jpa Repository Bean will not be changed
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.beanNameGenerator(new CustomGenerator())
.sources(SampleApplication.class)
.run();
}
}
If the code is used above, the name of the jpa Repository bean will be changed, but the CustomGenerator will not be injected during the test
what should i do!! helppppp
it is the same as above!!

Before spring boot start read properties file

I have spring boot application, before running the application run method I have to validate the properties file, then only run the application example
Interface ValidateProperties {
public boolean isValid();
}
#SpringBootApplication
public class MyApp {
public static void main(String[] args) throws Exception {
**// what is the best way to call Interface here**
if (i.isValid) {
SpringApplication.run(MyApp.class, args);
}
}
}

ApplicationReadyEvent not listened to when declaring #ComponentScan for library package

#componentscan({"com.test.cloud"})
#SpringBootApplication
public class TestClass {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(TestClass.class, args);
}
}
public class TestClass2{
#eventlistener(ApplicationReadyEvent.class)
public void testMethod() {
.....
.....
}
}
Here if I remove #componentscan({"com.test.cloud"}) annotation then the testMethod is triggered. But when I use #componentscan({"com.test.cloud"}) then the testMethod is not triggered.
SpringBoot Version: 1.5.10.RELEASE
Worked by adding:
#componentscan({"com.test.cloud","com.example.demo"})

Spring batch with multiple main and scheduler

I am working on a Spring Batch project that has two Main classes with #SpringBootApplication in it. I also have to use the #Scheduler and associate it only with one Main class. But the issue here is no matter which class I run, the scheduler is getting executed. Snippets below,
MainApp1.java
#SpringBootApplication
#EnableScheduling
public class MainApp1{
public static void main(String[] args) {
SpringApplication.run(MainApp1.class, args);
}
}
MainApp2.java
#SpringBootApplication
public class MainApp2{
public static void main(String[] args) {
SpringApplication.run(MainApp2.class, args);
}
}
Scheduler.java
#Configuration
public class TmsBatchSenderScheduler {
#Scheduled(fixedDelay = 5000)
public void myScheduler() {
}
}
I think what's happening here is the scheduler is getting created for both mains because of the #Configuration annotation. Is there a way to achieve this? I want the scheduler to run only when the MainApp1 is run.
Thanks in advance :)

#SpringbootApplication does not scan components in default package

Hi I have my main class of spring boot app in package com
and two other beans defined in com.bbh.fx.pack1.
As per my understanding SpringBootApplication will autoscan all the components in its pakacge and childern package. But it is not scanning. Not sure what i am missing
**package com.bbh.fx.pack1;**
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
#Component
public class BeanA {
#PostConstruct
public void init() {
System.out.println("in BEANA");
}
}
**package com;**
#SpringBootApplication
public class MainApp{
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
You do not have a main in your MainApp your code should look like:
#SpringBootApplication
public class MainApp{
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
I usually like to start all new applications from https://start.spring.io/ you can choose your project name, build type (maven or gradle) and select any spring dependencies you'd like included in the application.
Use #ComponentScan to scan components.
#SpringBootApplication
#ComponentScan("com")
public class MainApp{
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}

Resources