This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: Understand the spring-@autowired usage

I am reading the Spring 3.0.x reference documentation for the Spring Autowired notes:

The following example is beyond me. Do we need to do anything in the XML to make it work?

Example:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

Copy the code
public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}
Copy the code

How do you auto-assemble two classes to implement the same interface and use the same class?

class Red implements Color
class Blue implements Color

class myMainClass{
    @Autowired 
    privateColor color; draw(){ color.design(); }}Copy the code

Which design method will be invoked? How do I determine that the design methods of the Red class will be called instead of Blue?

Answer 1:

The @Autowired annotation allows you to skip other injected configurations and just do the configuration for you. Assuming your package is com.mycompany.movies, you must put this tag in XML (application context file) :

<context:component-scan base-package="com.mycompany.movies" />
Copy the code

The label will be scanned automatically. Assuming that each class that must become a bean is annotated with the correct annotation (such as @Component (for simple beans) or @Controller (for servlet controls) or @Repository (for DAO classes), And those classes are located somewhere under the com.mycompany.movies package. Spring will find all of these and create a bean for each one. This is done by two scans of the class – the first where it only searches for the classes that need to be beans and maps the injections that need to be made, and the second scan where it will inject the beans. Of course, you can define beans in a more traditional XML file or using the @Configuration class (or any combination of the three).

The @Autowired annotation tells Spring where injection is needed. If placed on the setMovieFinder method, it will understand (annotated by the prefix set + @autoWired) that a bean needs to be injected. On the second scan, Spring searches for a bean of type MovieFinder and, if it finds it, injects it into this method. If it finds two such beans, you get exceptions. To avoid exceptions, use the @qualifier notation and tell it which of the two beans to inject by:

@Qualifier("redBean")
class Red implements Color {
   // Class code here
}

@Qualifier("blueBean")
class Blue implements Color {
   // Class code here
}
Copy the code

In the @AutoWired declaration, you also need to add the @qualifier to indicate the bean of two colors to be injected:


@Autowired
@Qualifier("redBean")
public void setColor(Color color) {
  this.color = color;
}
Copy the code