Spring学习笔记:基于xml的依赖注入

基于xml依赖注入简单流程

最近刚刚开始学习Spring boot,老师让尝试三种方式进行依赖注入。注解配置和Java配置都很容易,就是这个xml有点麻烦,这个比较旧,而且网上大多教程都没有完整的流程,我这个新手学起来总是一卡一卡的。所以在我将整个流程走过一遍后,决定写一个完整的流程,以防忘记。

目录结构

目录结构

创建POJO(简单的Java对象,实际就是普通JavaBeans)

首先创建需要依赖注入的例子。

具体功能:
类MovieLister的方法moviesDirectedBy用于返回指定导演的所有电影。其代码如下所示。其中,类Movie表示电影,包括电影名字,电影导演等信息。finder对象是接口MovieFinder的实例。MovieFinder接口的方法findAll()用于获取所有的电影列表。类CSVMovieFinder和InMemMovieFinder是接口MovieFinder的两个实现类,分别从CSV文件(文件位于工程根目录下,名为movies.csv)和内存中读取电影信息。

简单来说就是一个名叫MovieLister的类有一个根据导演找电影的方法:movieDirectedBy,这个类依赖实现了MovieFinder接口的CSVMovieFinder类或InMemMovieFinder类。

代码实现

主函数

@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太麻烦就不写了。

写xml

在src/main/resources目录下新建一个beans.xml

<?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>

<beans>标签为最大的标签,将需要依赖注入的类使用<bean>标签写入其中。
class属性表示指定类的全限定名。用于反射创建对象。默认情况下调用无参构造函数。

使用类中的构造函数,给成员变量赋值,通过在xml文件中的bean进行配置的方式给对象赋值。

构造方法注入涉及的标签:

constructor-arg

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

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

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

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

上述标签描述引用自博客Spring基于XML方式的使用

最后

当POJO和xml都完成后,一定记得在主函数上面写上@ImportResource("classpath:beans.xml")表示使用该xml配置。

最后主函数应为

@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"));
    }
}

之后就大功告成了,点击运行即可。

猜你喜欢

转载自blog.csdn.net/weixin_46149121/article/details/109344201