Reputation: 12061
I am getting the following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.
Action:
Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.
I have never seen this error before but it's odd that the @Autowire
is not working. Here is the project structure:
Applicant Interface
public interface Applicant {
TApplicant findBySSN(String ssn) throws ServletException;
void deleteByssn(String ssn) throws ServletException;
void createApplicant(TApplicant tApplicant) throws ServletException;
void updateApplicant(TApplicant tApplicant) throws ServletException;
List<TApplicant> getAllApplicants() throws ServletException;
}
ApplicantImpl
@Service
@Transactional
public class ApplicantImpl implements Applicant {
private static Log log = LogFactory.getLog(ApplicantImpl.class);
private TApplicantRepository applicantRepo;
@Override
public List<TApplicant> getAllApplicants() throws ServletException {
List<TApplicant> applicantList = applicantRepo.findAll();
return applicantList;
}
}
Now I should be able to just Autowire Applicant and be able to access, however in this case it is not working when I call it in my @RestController:
@RestController
public class RequestController extends LoggingAware {
private Applicant applicant;
@Autowired
public void setApplicant(Applicant applicant){
this.applicant = applicant;
}
@RequestMapping(value="/", method = RequestMethod.GET)
public String helloWorld() {
try {
List<TApplicant> applicantList = applicant.getAllApplicants();
for (TApplicant tApplicant : applicantList){
System.out.println("Name: "+tApplicant.getIndivName()+" SSN "+tApplicant.getIndSsn());
}
return "home";
}
catch (ServletException e) {
e.printStackTrace();
}
return "error";
}
}
------------------------UPDATE 1-----------------------
I added
@SpringBootApplication
@ComponentScan("module-service")
public class WebServiceApplication extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(WebServiceApplication.class, args);
}
}
and the error went away but nothing happened. However when I commented out everything dealing with Applicant
in the RestController
prior to adding @ComponentScan()
I was able to return a string the UI
, thus meaning my RestController
was working, now it is being skipped. I got an ugly Whitelabel Error Page
now.
---------------------UPDATE 2------------------------------
I added the base package of the bean it was complaining about. Error reads:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setApplicantRepo in com.service.applicant.ApplicantImpl required a bean of type 'com.delivery.service.request.repository.TApplicantRepository' that could not be found.
Action:
Consider defining a bean of type 'com.delivery.request.request.repository.TApplicantRepository' in your configuration.
I added @ComponentScan
@SpringBootApplication
@ComponentScan({"com.delivery.service","com.delivery.request"})
public class WebServiceApplication extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(WebServiceApplication.class, args);
}
}
----------------------------Update 3----------------------
adding:
@SpringBootApplication
@ComponentScan("com")
public class WebServiceApplication extends SpringBootServletInitializer {
still is complaining about my ApplicantImpl
class which @Autowires
my repo TApplicantRepository
into it.
Upvotes: 250
Views: 971737
Reputation: 43
It was working for me after replacing with jakarta.persistence.Entity instead of javax.persistence and also added @ComponentScan({"com.myapp.service","com.myapp.repo"})
then solved my problem
Upvotes: 0
Reputation: 12061
It might be because the project has been broken down into different modules:
@SpringBootApplication
@ComponentScan({"com.delivery.request"})
@EntityScan("com.delivery.domain")
@EnableJpaRepositories("com.delivery.repository")
public class WebServiceApplication extends SpringBootServletInitializer {
Upvotes: 357
Reputation: 52646
Try to remove
@ComponentScan({"com.delivery.service","com.delivery.request"})
File WebServiceApplication.java
at folder where the most outter of all files/packages.
@SpringBootApplication
public class WebServiceApplication extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(WebServiceApplication.class, args);
}
}
Upvotes: 0
Reputation: 1
i had the "Action: Consider defining a bean of type 'blabla.blabla.blabla.repository' in your configuration." error and this is how i fixed it;
I was work with a MongoDB. that so, my "EmployeeRepo.java interface" had to contains as like this;
**public interface EmployeeRepo extends MongoRepository<Employee, Long> **
Firstly, the area with the MongoRepository part was like this "JPARepository". Then i changed with the MongoRepository. And the problem fixed
Upvotes: 0
Reputation: 1
Use @Component
for the Class where the Bean is not found.
And Use
@ComponentScan("<Use_The_Root_Directory_Of_Main_Class>")
. This can happen when the Components are spread over many directories.
@Configuration
@SpringBootApplication
@ComponentScan({"com.example.demo"})
public class Application {
}
Upvotes: 0
Reputation: 917
This can also happen if you have two beans with the same name.
For example if you have factory method monkey
creating Monkey and you mistakenly name another factory method (creating Car) also monkey
.
Problematic code:
@Bean
public Car monkey() {
return new Car();
}
Should be:
@Bean
public Car car() {
return new Car();
}
Reference:
By default, the bean name will be the same as the method name...
Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master and uses it to set the property.
Upvotes: 0
Reputation: 243
I also received a similar error:
Consider defining a bean of type 'A_REPOSITORY_INTERFACE' in your configuration.
Then, according to Akashe's solution, I added @EnableJpaRepositories
to my main class. After that, I received the following error instead:
Consider defining a bean of type 'entityManagerFactory' in your configuration.
Next, I went through all the responses here, googled a lot and read a lot of other resources, which didn't worked out.
Finally, I was lucky to have found the solution on a blog/website (javatute.com). I just followed its examples.
Like suggested by many here, I added @ComponentScan("YOUR_BASE_PACKAGE.*")
and @EntityScan("YOUR_BASE_PACKAGE.*")
to my main app class, followed by adding a config
package and creating a JpaConfig
class like:
package YOUR_BASE_PACKAGE.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@EnableJpaRepositories(basePackages = "YOUR_BASE_PACKAGE")
public class JpaConfig {
}
The blog I followed:
Consider defining a bean of type in your configuration
which lead me to:
and finally to:
Many To Many Mapping In Hibernate/JPA Using Spring Boot And Oracle
Upvotes: 1
Reputation: 111
@Service
should be imported from org.springframework.stereotype.Service
you might be importing it from org.jvnet.hk2.annotations.Service
In CrudRepository
, check if the Entity
class @Id using import javax.persistence.Id;
, not import org.springframework.data.annotation.Id;
Upvotes: 2
Reputation: 1
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Upvotes: -1
Reputation: 4278
I had the same problem, i was get this error for redis repository like this:
@Repository
public interface AppTokenRepository extends CrudRepository<AppToken, String> {
}
@RedisHash("APP_TOKEN")
public class AppToken implements Serializable {
private String id;
private String accessToken;
}
I solved my issue by adding @EnableRedisRepositories
over spring boot application class.
Upvotes: 0
Reputation: 11
Had the same error, transpired it was an issue with the application properties with incorrect username, password and driver and completely unrelated to Bean.
Upvotes: 1
Reputation: 2484
For me, it was because of using @Value in class that use lombok @RequiredArgsConstructor annotation. Just changed it to constructor injection.
Upvotes: 0
Reputation: 2307
There is a chance...
You might be missing @Service
, @Repository
or @Component
annotation on your respective implementation classes.
Upvotes: 188
Reputation: 4932
In my case, this problem was caused by an errant spring.autoconfigure.exclude
property value. I had this property because my Spring Boot project initially didn't connect to a database, but then I added my first JpaRepository
class and tried to declare an instance of it with the @Autowired
annotation in a @RestController
class.
The line in application.properties
that caused the problem:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
Once I deleted or commented out that line, my Spring Boot service ran just fine.
Upvotes: 0
Reputation: 1
In my case, I have just added @Component
annotations to some classes (to make sure every Classes have configuration).
@Component
, @Service
or @Repository
are almost familiar. Baeldung link reference
Upvotes: 0
Reputation: 41
I had a similar issue, exact same error message. But my error only happened when I tried to install and run on my local docker.
Turns out the issue was profile dependant.
I had two classes that implemented the interface, one with a production Profile @Profile("prod") and the second with the "Test Environment" profiles. @Profile("!local & !prod")
However, there was no active profile for when I tried to run locally (on Docker).
I created a 3rd class that implemented the interface for a local profile @Profile("local") and this fixed my issue
Upvotes: 0
Reputation: 357
I had the same issue while using MongoDB due to incorrect basePackages name of the @Repository class
@Configuration
@EnableMongoRepositories(basePackages = {"org.mycompany.repository.mongo.primary"}, mongoTemplateRef = "getMongoTemplatePrimary")
Upvotes: 0
Reputation: 1
If you are using Eclipse and tried all possibilities and you still have errors, then try to update the project using Maven. You can to this like this: Right click on your project-> Maven -> Update Project
.
It helped me to solve my errors. Sometimes Eclipse shows this error if a project is not updated with all repos which you declared in the pom file.
Upvotes: 0
Reputation: 27255
This error message also pops up when you fail to annotate the Entity classes associated with your bean with the @Entity
Annotation.
My ComponentScan
worked fine but this popped up for the @repository
interface:
@Repository
public interface ExpenseReportAuditRepository extends
PagingAndSortingRepository<ExpenseReportAudit, Integer> {
because I failed to add the @Entity annotation to ExpenseReportAudit
@Entity // <--- Adding this fixed the issue.
public class ExpenseReportAudit {
.....
Upvotes: 2
Reputation: 51
I think you can make it simplified by annotating your repository with @Repository
, then it will be enabled automatically by Spring Framework.
Upvotes: 5
Reputation: 1269
In my case, our project has a Configuration class, so I just added mine like this
@Configuration
public class DependencyConfiguration {
@Bean
public ActivityService activityService(
@Value("${send.money.ms.activity.url}") final String activityHistoryUrl,
final HttpRestService httpRestService
) {
return new ActivityServiceImpl(activityHistoryUrl, httpRestService);
}
.......................
Then the microservice started alright.
PS: I encountered this issue even though the library I need is imported properly and could be seen on External Libraries imported.
Upvotes: 1
Reputation: 11
For me - using Spring Boot with MongoDB, the following was the Problem:
In my POM.xml I had:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-starter-data-mongodb</artifactId>
</dependency>
but I needed the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
(Short: Add "spring-boot-..." instead of only "spring-...")
Upvotes: -1
Reputation: 9
For me worked a clean install on pom.xml
right click on pom.xml
expand Run As
select Maven build
set Goals to the command clean install
apply > run > close
Upvotes: 0
Reputation: 22905
To fix errors like:
It happened to me that the LocalContainerEntityManagerFactoryBean
class did not have the setPackagesToScan
method. Then I proceeded to use the @EntityScan
annotation which doesn't work correctly.
Later I could find the method setPackagesToScan()
but in another module, so the problem came from the dependency which did not have this method because it was an old version.
This method can be found in the spring-data-jpa or spring-orm dependency of updated versions:
From:
implementation("org.springframework", "spring-orm", "2.5.1")
To:
implementation("org.springframework", "spring-orm", "5.2.9.RELEASE")
Or to:
implementation("org.springframework.data", "spring-data-jpa", "2.3.4.RELEASE")
In addition, it was not necessary to add other annotations other than that of
@SprintBootApplication
.
@SpringBootApplication
open class MoebiusApplication : SpringBootServletInitializer()
@Bean
open fun entityManagerFactory() : LocalContainerEntityManagerFactoryBean {
val em = LocalContainerEntityManagerFactoryBean()
em.dataSource = dataSource()
em.setPackagesToScan("app.mobius.domain.entity")
...
}
GL
Upvotes: 0
Reputation: 385
For anybody who was brought here by googling the generic bean error message, but who is actually trying to add a feign client to their Spring Boot application via the @FeignClient
annotation on your client interface, none of the above solutions will work for you.
To fix the problem, you need to add the @EnableFeignClients
annotation to your Application class, like so:
@SpringBootApplication
// ... (other pre-existing annotations) ...
@EnableFeignClients // <------- THE IMPORTANT ONE
public class Application {
Side note: adding a @ComponentScan(...)
beneath @SpringBootApplication
is redundant, and your IDE should flag it as such (IntelliJ IDEA does, at least).
Upvotes: 10
Reputation: 139
Moving the Springbootapplication(application.java) file to another package resolved the issue for me. Keep it separate from the controllers and repositories.
Upvotes: 4
Reputation: 6236
check the base package name.If the package has different modules that are not prefixed with base package name.
Upvotes: 0
Reputation: 1
reminder that spring doesn't scan the world , it uses targeted scanning wich means everything under the package where springbootapplication is stored. therefore this error "Consider defining a bean of type 'package' in your configuration [Spring-Boot]" may appear because you have services interfaces in a different springbootapplication package .
Upvotes: 0
Reputation: 2367
Try configuring the project structure as given below:
Put all the repo, service, packages in the child package of the main package:
package com.leisure.moviemax; //Parent package
@SpringBootApplication
@PropertySource(value={"classpath:conf.properties"})
public class MoviemaxApplication implements CommandLineRunner {
package com.leisure.moviemax.repo; //child package
@Repository
public interface UsrRepository extends JpaRepository<UserEntity,String> {
Upvotes: 2