Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1085 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring Boot @autowired does not work, classes in different package

#1
I have a Spring boot application.

I get the following error

> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'birthdayController': Injection of autowired
> dependencies failed; nested exception is
> org.springframework.beans.factory.BeanCreationException: Could not
> autowire field: private com.esri.birthdays.dao.BirthdayRepository
> com.esri.birthdays.controller.BirthdayController.repository; nested
> exception is
> org.springframework.beans.factory.NoSuchBeanDefinitionException: No
> qualifying bean of type [com.esri.birthdays.dao.BirthdayRepository]
> found for dependency: expected at least 1 bean which qualifies as
> autowire candidate for this dependency. Dependency annotations:
> {@org.springframework.beans.factory.annotation.Autowired(required=true)}
> at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
> ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
> ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
> ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
> ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
> at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
> ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
> at or


Following is code of my Repository class




package com.esri.birthdays.dao;
import com.esri.birthdays.model.BirthDay;
public interface BirthdayRepository extends MongoRepository<BirthDay,String> {
public BirthDay findByFirstName(String firstName);
}

Following is controller.



package com.esri.birthdays.controller;
@RestController
public class BirthdayController {

@Autowired
private BirthdayRepository repository;

**Works if they are in same package.** Not sure why


Reply

#2
Try annotating your Configuration Class(es) with the `@ComponentScan("com.esri.birthdays")` annotation.
Generally spoken: If you have sub-packages in your project, then you have to scan for your relevant classes on project-root. I guess for your case it'll be "com.esri.birthdays".
You won't need the ComponentScan, if you have no sub-packaging in your project.
Reply

#3
Try this:

@Repository
@Qualifier("birthdayRepository")
public interface BirthdayRepository extends MongoRepository<BirthDay,String> {
public BirthDay findByFirstName(String firstName);
}

And when injecting the bean:

@Autowired
@Qualifier("birthdayRepository")
private BirthdayRepository repository;

If not, check your **CoponentScan** in your config.
Reply

#4
Spring Boot will handle those repositories automatically as long as they are included in the same package (or a sub-package) of your @SpringBootApplication class. For more control over the registration process, you can use the @EnableMongoRepositories annotation. [spring.io guides][1]

@SpringBootApplication
@EnableMongoRepositories(basePackages = {"RepositoryPackage"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}



[1]:

[To see links please register here]

Reply

#5
Just put the packages inside the @SpringBootApplication tag.

@SpringBootApplication(scanBasePackages = { "com.pkg1", "com.pkg2", .....})

Let me know.
Reply

#6
When you use @SpringBootApplication annotation in for example package

> com.company.config

it will automatically make component scan like this:

@ComponentScan("com.company.config")

So it will NOT scan packages like com.company.controller etc.. Thats why you have to declare your @SpringBootApplication in package one level prior to your normal packages like this: com.company OR use scanBasePackages property, like this:

@SpringBootApplication(scanBasePackages = { "com.company" })

OR componentScan:

@SpringBootApplication
@ComponentScan("com.company")
<br>
Reply

#7
For this kind of issue, I ended up in putting `@Service` annotation on the newly created service class then the autowire was picked up.
So, try to check those classes which are not getting autowired, if they need the corresponding required annotations(like `@Controller`, `@Service`, etc.) applied to them and then try to build the project again.
Reply

#8
In my case `@component` was not working because I initialized that class instance by using `new <classname>()`.

If we initialize instance by conventional Java way anywhere in code, then spring won't add that component in IOC container.
Reply

#9
There will definitely be a bean also containing fields related to Birthday
So use this and your issue will be resolved

@SpringBootApplication
@EntityScan("com.java.model*") // base package where bean is present
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Reply

#10
I had the same problem. It worked for me when i removed the private modifier from the Autowired objects.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through