Spring study notes: xml-based dependency injection

Simple process based on xml dependency injection

I just started learning Spring boot recently, and the teacher asked me to try three ways to perform dependency injection. Annotation configuration and Java configuration are very easy, but this xml is a bit troublesome, this is relatively old, and most of the online tutorials do not have a complete process, I am a novice who always learns one card by one card. So after I walked through the entire process, I decided to write a complete process in case I forget it.

Directory Structure

Directory Structure

Create POJO (simple Java object, actually ordinary JavaBeans)

First create an example that requires dependency injection.

Specific functions:
The moviesDirectedBy method moviesDirectedBy of the class MovieLister is used to return all movies of the specified director. The code is shown below. Among them, the category Movie represents a movie, including information such as movie name and movie director. The finder object is an instance of the interface MovieFinder. The method findAll() of the MovieFinder interface is used to obtain a list of all movies. The classes CSVMovieFinder and InMemMovieFinder are two implementation classes of the interface MovieFinder, which read movie information from the CSV file (the file is located in the root directory of the project and is named movies.csv) and the memory respectively.

To put it simply, a class called MovieLister has a method for finding movies based on the director: movieDirectedBy. This class relies on the CSVMovieFinder class or the InMemMovieFinder class that implements the MovieFinder interface.

Code

Main function

@SpringBootApplication
public class MovieXmlApplication {
    
    

    public static void main(String[] args) throws IOException {
    
    
        ConfigurableApplicationContext context = SpringApplication.run(MovieXmlApplication.class, args);

        MovieLister movieLister = context.getBean(MovieLister.class);
        System.out.println(movieLister.moviesDirectedBy("233"));
    }

}

MovieLister

public class MovieLister {
    
    
    private final MovieFinder finder;

    public MovieLister(MovieFinder movieFinder) {
    
    
        this.finder = movieFinder;
    }

    public List<Movie> moviesDirectedBy(String director) throws IOException {
    
    
        List<Movie> movies = finder.findAll();
        return movies.stream()
                .filter(movie -> movie.getDirectorName().equals(director))
                .collect(Collectors.toList());
    }

}

InMemMovieFinder

public class InMemMovieFinder implements MovieFinder{
    
    
    List<Movie> movieList;

    public InMemMovieFinder() {
    
    
        this.movieList = new ArrayList<>();
    }
    @Override
    public List<Movie> findAll() {
    
    
        return movieList;
    }
}

MovieFinder

public interface MovieFinder {
    
    
    List<Movie> findAll() throws IOException;
}

CSVMovieFinder is too troublesome to write.

Copy xml

Create a new beans.xml in the src/main/resources directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <bean class="com.movie.moviexml.bean.injection.movies.MovieLister">
        <constructor-arg  ref="csvMovieFinder"/>
        <!--        constructor-arg用来指定构造函数的参数-->
    </bean>

    <bean class="com.movie.moviexml.bean.injection.movies.InMemMovieFinder" name="inMemMovieFinder"/>
    <bean class="com.movie.moviexml.bean.injection.movies.CSVMovieFinder" name="csvMovieFinder"/>

</beans>

The <beans> tag is the largest tag. Use the <bean> tag to write classes that require dependency injection.
The class attribute represents the fully qualified name of the specified class. Used to create objects in reflection. The no-argument constructor is called by default.

Use the constructor in the class to assign values ​​to member variables, and assign values ​​to objects through the bean configuration in the xml file.

Tags involved in constructor injection:

constructor-arg

index:指定参数在构造函数参数列表的索引位置

name:指定参数在构造函数中的名称

value:它能赋的值是基本数据类型和 String 类型

ref:它能赋的值是其他 bean 类型,且必须是在配置文件中配置过的 bean

The above tag description is quoted from the blog Spring's use of XML-based methods

At last

When both POJO and xml are completed, remember to write on the main function to @ImportResource("classpath:beans.xml")indicate the use of the xml configuration.

The final main function should be

@ImportResource("classpath:beans.xml") // xml
@SpringBootApplication
public class MovieXmlApplication {
    
    

    public static void main(String[] args) throws IOException {
    
    
        ConfigurableApplicationContext context = SpringApplication.run(MovieXmlApplication.class, args);

        MovieLister movieLister = context.getBean(MovieLister.class);
        System.out.println(movieLister.moviesDirectedBy("233"));
    }
}

Then you're done, just click Run.

Guess you like

Origin blog.csdn.net/weixin_46149121/article/details/109344201