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

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

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 postconstruct not called when running java -jar

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

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

Spring boot - autowire fails

I'm trying to fetch some properties from application.properties file ad my code is the following:
Main Application class:
package it.mysite;
#SpringBootApplication
#EnableTransactionManagement
public class MailSenderApplication {
public static void main(String[] args){
ConfigurableApplicationContext context = SpringApplication.run(MailSenderApplication.class, args);
System.out.println("*****");
for (String name : context.getBeanDefinitionNames()) {
System.out.println(name);
}
System.out.println("*****");
new MySendService().sendReport();
}
}
My service class:
package it.mysite.service;
#Service
public class MySendService {
#Value("${mail.fc.to}")
private String[] to;
#Value("${mail.fc.subject}")
private String subject;
#Autowired ReportService reportEmailService;
#Autowired MailProperties mailProperties;
public void sendReport(){
if(mailProperties.getTo().length > 0) {
}
}
Class where I fetch the properties:
package it.mysite.properties;
#Component
#ConfigurationProperties("mail.fc")
public class MailProperties {
private String[] to;
public String[] getTo(){
return to;
}
}
Config file:
# Email config
mail.fc.to=my#mail.com
mail.fc.subject=My subject
All of the #Autowired properties are null, and also the #Value properties (I tried to get them in that way also). When I print my context I can see these classes in the bean list, and for what I know my packages hierarchy is correct, so what can be the problem?
EDIT
Ok, I got the suggestion from the duplicate question and I changed my main class code as follows:
Main Application class:
package it.mysite;
#SpringBootApplication
#EnableTransactionManagement
public class MailSenderApplication {
#Autowired MySendService mySendService;
public static void main(String[] args){
ConfigurableApplicationContext context = SpringApplication.run(MailSenderApplication.class, args);
System.out.println("*****");
for (String name : context.getBeanDefinitionNames()) {
System.out.println(name);
}
System.out.println("*****");
new MailSenderApplication().boot();
}
private void boot(){
mySendService.sendReport();
}
}
But I got the same error. Wasn't that the suggestion?

Resources