#Converter(autoApply = true) not working in different package - spring-boot

This is under package: com.a.b
package com.a.b;
#Converter(autoApply = true)
public class DateConverter implements AttributeConverter<Date, String> {
}
application start class under package: com.a.c
package com.a.c;
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
in differnet package, autoApply = true not working, except custom LocalContainerEntityManagerFactoryBean. Are there other solutions

You have to use #EntityScan. I assume that your entities are in com.a.c. If this is not the case you have to set the correct package:
package com.a.c;
#SpringBootApplication
#EntityScan(basePackages = {"com.a.c", "com.a.b"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Related

I have a multi module project where I can't solve this error

My problem
Description:
Field userRepository in com.toomuch2learn.microservices.servicea.controller.security.services.UserDetailsServiceImpl required a bean of type 'com.toomuch2learn.microservices.servicea.controller.repository.UserRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.toomuch2learn.microservices.servicea.controller.repository.UserRepository' in your configuration.
I have 3 modules (A,B,C). We have the parent pom.xml. While the pom.xml B takes the A, while the pom.xml A takes the C.
enter image description here
enter image description here
This is list to main
#SpringBootApplication
#EnableJpaRepositories()
#EntityScan("com.toomuch2learn.microservices.servicec.model")
#ComponentScan({"com.toomuch2learn.microservices"})
public class ServiceBApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceBApplication.class, args);
}
}
#SpringBootApplication
#ComponentScan(basePackages = "com.toomuch2learn.microservices")
public class ServiceCApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceCApplication.class, args);
}
}
#SpringBootApplication
#EnableJpaRepositories
#EntityScan("com.toomuch2learn.microservices.servicec.model")
#ComponentScan(basePackages = "com.toomuch2learn.microservices")
public class ServiceAApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAApplication.class, args);
}
}

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!!

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"})

Read application.properties value from Spring boot main class

In my spring boot app, I have variable value set in application.properties.
I want to read it in the main java class. How can I do that? following code returns null.
#SpringBootApplication
public class MyApplication {
#Value( "${spring.shutdown.sleep.time}" )
private static String shutdownSleepTime;
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(MysApplication.class, args);
System.out.println("sleep time : " + shutdownSleepTime);
You can read the property from the applicationContext.
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(MysApplication.class, args);
String shutdownSleepTime = applicationContext.getEnvironment().getProperty("spring.shutdown.sleep.time");
}
I recommend it
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(MysApplication.class, args);
}
#Component
public static class CmdRunner implement CommandLineRunner {
#Value( "${spring.shutdown.sleep.time}" )
private static String shutdownSleepTime;
public void run(String... args) throws Exception {
System.out.println("sleep time : " + shutdownSleepTime);
}
}
So you want to print something when the application context is created.
For that You can use event listeners:
#SpringBootApplication
public class MyApplication {
#Value( "${spring.shutdown.sleep.time}" )
private String shutdownSleepTime; // not static
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext =
SpringApplication.run(MysApplication.class, args);
}
#EventListener
public void onAppContextStarted(ApplicationStartedEvent e) {
System.out.println("sleep time : " + shutdownSleepTime);
}
}

#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