-4

If I have beans tagged with @Component annotation. But two beans in different folders. One in com.mycompany.core.bean1 and another in com.mycompany.DAO.bean2. When I'm trying to autowire bean1 in bean 2 like:

  public class Bean2{

  @Autowired
  Bean1 bean1;
  .....
  }

Do I need to import package with bean1 in bean2 or would it scan for it by itself?

UPD: I know about question with the same name. But questions is totally different. So please unswer my question if you now it, and don't post links to another question.

Ars
  • 591
  • 1
  • 4
  • 19

2 Answers2

0

In the java class Bean2 you have to organize your imports, the Bean1 should be imported though.

Besides you have to do configure your components scan due to autowiring and component scanning. Either you use Java or XML configuration. You can check some examples here: https://www.mkyong.com/spring/spring-auto-scanning-components/

you can find the official Spring reference here: https://docs.spring.io/spring/docs/5.0.0.RELEASE/spring-framework-reference/core.html#spring-core

schoener
  • 795
  • 5
  • 12
0

As I understand your question, it does not really have anything to do with Spring or Autowiring. You have a reference to Java class Bean1 in Bean2 and you say that Bean1 and Bean2 are not in the same package. So you do have to import Bean1 into Bean2, otherwise your code won't even compile.

anothernode
  • 5,100
  • 13
  • 43
  • 62