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

#11
By default, in Spring boot applications, component scan is done inside the package where your main class resides. any bean which is outside the package will not the created and thus gives the above mentioned exception.

Solution: you could either move the beans to the main spring boot class(which is not a good approach) or create a seperate configutation file and import it:



@Import({CustomConfigutation1.class, CustomConfiguration2.class})
@SpringBootpplication
public class BirthdayApplication {

public static void main(String [] args) {
springApplication.run(BirthdayApplication.class, args );
}
}

Add beans to these CustomConfiguration files.
Reply

#12
package com.test.springboot;
@SpringBootApplication
@ComponentScan(basePackages = "com.test.springboot")
public class SpringBoot1Application {

public static void main(String[] args) {
ApplicationContext context= SpringApplication.run(SpringBoot1Application.class, args);

=====================================================================

package com.test.springboot;
@Controller
public class StudentController {
@Autowired
private StudentDao studentDao;

@RequestMapping("/")
public String homePage() {
return "home";
}




Reply

#13
Another fun way you can screw this up is annotating a setter method's parameter. It appears that for setter methods (unlike constructors), you have to annotate the method as a whole.

This does not work for me:
`public void setRepository(@Autowired WidgetRepository repo)`

but this does:
`@Autowired public void setRepository(WidgetRepository repo)`

(Spring Boot 2.3.2)
Reply

#14
When I add `@ComponentScan("com.firstday.spring.boot.services")` or `scanBasePackages{"com.firstday.spring.boot.services"}` jsp is not loaded. So when I add the parent package of project in @SpringBootApplication class it's working fine in my case

Code Example:-

package com.firstday.spring.boot.firstday;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.firstday.spring.boot"})
public class FirstDayApplication {

public static void main(String[] args) {
SpringApplication.run(FirstDayApplication.class, args);
}

}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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