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

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

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

Spring does not find bean

I get the following error:
Field delegate in package.rest.api.MyApiController required a bean of type 'package.rest.api.MyApiDelegate' that could not be found.
I've tried to add annotation #ComponentScan("package.rest") to the main class, but it didn't help.
package package;
#SpringBootApplication
#Import(SpecificationConf.class)
//#ComponentScan("package.rest")
public class MyApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
package package.rest.api;
#Controller
#RequestMapping(...)
public class MyApiController implements MyApi {
#Autowired
#Qualifier("myService")
private MyApiDelegate delegate;
}
package package.rest;
#Service("myService")
public class MyApiDelegateImpl implements MyApiDelegate {
...
}
package package.rest.api;
public interface MyApiDelegate {
...
}
Can that problem happen due to incorrect configuration of the Application context or an inappropriate maven profile? I've tried to move classes to the same package, but it also didn't help.
Given that you are building a JAR file and not a WAR file, please try dropping SpringBootServletInitializer from your main class as follows:
package package;
#SpringBootApplication
#Import(SpecificationConf.class)
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}

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

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

How to read it from the properties file in Springboot

application.properties file has:
my.greeting = Hello
ServiceClass file has:
#Value("${my.greeting}")
private String messageFromProperties;
MainAppfile:
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationConceptsApplication.class, args);
ServiceClass serv = new ServiceClass();`enter code here`
System.out.println(serv.getMessageFromProperties());//getting null
}
Please let me know what am i missing here. What all configuration is pending
You need to use #PropertySource to specify the source.
For example:
#PropertySource(value = "example.properties")
You can use the above-mentioned annotation with your main class.
For example:
#SpringBootApplication
#PropertySource(value = "example.properties")
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
Or, you can use them with classes annotated with #Configuration.
For example:
#Configuration
#PropertySource("example.properties")
public class ExampleConfiguration {
//code
}
If this doesn't help, I would advise you to share your complete code for everyone to understand what exactly the problem is.

#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