Spring5 导入和混合配置使用

Posted 冰点IT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring5 导入和混合配置使用相关的知识,希望对你有一定的参考价值。

      前面我们已经探讨了自动装配、JavaConfig、Xml配置的单独使用,下面我们探讨他们之间的配合使用,彰显一下Spring配置的威力。 

1、JavaConfig配置拆分与聚合

      在开发中,随着随着时间的推移与业务的复杂度,我们的配置类也会变得越来越臃肿,为了使我们的配置变得简洁、清爽,我们肯定会对配置类根据其业务范围、运行环境以及配置习惯等等将其拆分,而后再根据拆分之后的配置进行聚合,完成统一配置的架构体系,废话不多说了,来看例子。

1.1、复杂的配置类(假定)

1.1.1、准备三个简单Java类

public class Blog { private String blogName; private User user; public Blog(String blogName, User user) { this.blogName = blogName; this.user = user; } public String getBlogInfo() { return "看" + this.blogName+ "的是:" + this.user.getUserName(); }}public class Movie { private String movieName; private User user; public Movie(String movieName, User user) { this.movieName = movieName; this.user = user; } public String getMovieInfo() { return "看" + this.movieName+ "的是:" + this.user.getUserName(); }}public class User { private String userName; public User(String userName) { this.userName = userName; } public String getUserName() { return "冰点IT-" + userName; }}

1.1.2、编写复杂配置类

@Configurationpublic class BeanConfig { @Bean public User getUser() { return new User("icypt"); } @Bean public Movie getMovie(User user) { return new Movie("《湄公河行动》", user); } @Bean public Blog getBlog(User user) { return new Blog("《Spring5实战》", user); }}

1.1.3、编写测试类

public class TestMixConfigBean { public static org.slf4j.Logger logger = LoggerFactory.getLogger(TestMixConfigBean.class);public static AnnotationConfigApplicationContext acpc =new AnnotationConfigApplicationContext(BeanConfig.class); @Test public void testBlotConfig() { User user = acpc.getBean("getUser", User.class); Movie movie = acpc.getBean("getMovie", Movie.class); Blog blog = acpc.getBean("getBlog", Blog.class); logger.info(user.getUserName()); logger.info(movie.getMovieInfo()); logger.info(blog.getBlogInfo()); }}

运行测试类:

21:46:30,213 INFO main service.TestMixConfigBean:28 - 冰点IT-icypt21:46:30,213 INFO main service.TestMixConfigBean:29 - 看《湄公河行动》的是:冰点IT-icypt21:46:30,214 INFO main service.TestMixConfigBean:30 - 看《Spring5实战》的是:冰点IT-icypt

       根据运行结果可知我们的配置文件是完全没有问题的,但是假定现在这个配置文件已经臃肿到了不能不拆分地步了,那么该如何拆分呢?

1.2、拆分导入

     现将BeanConfig拆分成为UserConfig,UseUserConfig两个配置类,并将UserConfig导入到UseUserConfig之中完成配置,此时我们配置文件入口肯定是UseUserConfig无疑了。

1.2.1、编写配置类

@Configurationpublic class UserConfig { @Bean public User getUser() { return new User("icypt"); }}@Configuration@Import({UserConfig.class})public class UseUserConfig { @Bean public Movie getMovie(User user) { return new Movie("《湄公河行动》", user); } @Bean public Blog getBlog(User user) { return new Blog("《Spring5实战》", user); }}

1.2.2、修改测试类

public static AnnotationConfigApplicationContext acpc = new AnnotationConfigApplicationContext(UseUserConfig.class);

运行测试类:

22:11:42,214 INFO main service.TestMixConfigBean:30 - 冰点IT-icypt22:11:42,214 INFO main service.TestMixConfigBean:31 - 看《湄公河行动》的是:冰点IT-icypt22:11:42,215 INFO main service.TestMixConfigBean:32 - 看《Spring5实战》的是:冰点IT-icypt

可见这样配置也是没有问题的,但是这种拆分还是依赖与原配置的,那么如何做到配置统一入口,各个配置在入口处聚合呢?

1.3、拆分聚合

        拆分聚合是一种思路,用到的技术还是导入,只是我们对配置文件提供统一入口,让子配置类在入口处聚合,从而达到配置类真正意义上的解耦合。

1.3.1、拆分配置

@Configurationpublic class BlogConfig { @Bean public Blog getBlog(User user) { return new Blog("《Spring5实战》", user); }}@Configurationpublic class UserConfig { @Bean public User getUser() { return new User("icypt"); }}@Configurationpublic class MovieConfig { @Bean public Movie getMovie(User user) { return new Movie("《湄公河行动》", user); }}

1.3.2、提供配置入口

@Configuration@Import({UserConfig.class, MovieConfig.class, BlogConfig.class})public class EntranceConfig {}

1.3.3、修改测试类

public static AnnotationConfigApplicationContext acpc = new AnnotationConfigApplicationContext(EntranceConfig.class);

运行配置类:

22:11:42,214 INFO main service.TestMixConfigBean:30 - 冰点IT-icypt22:11:42,214 INFO main service.TestMixConfigBean:31 - 看《湄公河行动》的是:冰点IT-icypt22:11:42,215 INFO main service.TestMixConfigBean:32 - 看《Spring5实战》的是:冰点IT-icypt

由运行结果可见这种思路是no problem的。

2、XmlConfig配置拆分与聚合

       用同样的思路我们来对Xml配置进行拆分和聚合,为了文档不重复,我就不贴测试代码了,直接看配置。

       测试代码: https://github.com/icypt/spring-learn-rep

2.1、准备工作

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="blog" class="com.icypt.learn.service.Blog" c:_0="《Spring5实战》" c:_1-ref="user"/></beans><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="movie" class="com.icypt.learn.service.Movie" c:_0="《湄公河行动》" c:_1-ref="user"/></beans><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.icypt.learn.service.User" c:userName="冰点IT-icypt"/></beans>

2.2、拆分导入

拆分导入配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="applicationContext_user.xml"/> <bean id="blog" class="com.icypt.learn.service.Blog" c:_0="《Spring5实战》" c:_1-ref="user"/> <bean id="movie" class="com.icypt.learn.service.Movie" c:_0="《湄公河行动》" c:_1-ref="user"/></beans>

2.3、拆分聚合

配置统一入口配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="applicationContext_user.xml"/> <import resource="applicationContext_blog.xml"/> <import resource="applicationContext_movie.xml"/></beans>

3、AutoConfig、JavaConfig、XmlConfig混合使用

      以上是JavaConfig与XmlConfig的拆分聚合配置,下面我们来讨论三种配置的混合使用。

3.1、使用JavaConfig定义一个Bean

@Configurationpublic class UserConfig { @Bean public User getUser() { return new User("icypt"); }}

3.2、使用AutoConfig定义一个Bean

@Component

public class Food { private String foodName; public Food(String foodName) { this.foodName = foodName; } public String getFoodName() { return "爱吃的" + foodName; }}

3.3、使用XmlConfig提供配置统一入口

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.icypt.learn"/> <bean id="blog" class="com.icypt.learn.service.Blog" c:blogName="《Spring5实战》" c:user-ref="getUser"/></beans>

3.4、编写测试类

public class TestMixXmlConfigBean { public static org.slf4j.Logger logger = LoggerFactory.getLogger(TestMixXmlConfigBean.class); public static ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("applicationContext_mix_entrance.xml"); @Test public void testBlotConfig() { User user = cac.getBean("getUser", User.class); Food food = cac.getBean("food", Food.class); Blog blog = cac.getBean("blog", Blog.class); logger.info(user.getUserName()); logger.info(food.getFoodName()); logger.info(blog.getBlogInfo()); }}

运行结果:

23:34:13,184 INFO main service.TestMixXmlConfigBean:24 - 冰点IT-icypt23:34:13,184 INFO main service.TestMixXmlConfigBean:25 - 爱吃的冰激凌!23:34:13,185 INFO main service.TestMixXmlConfigBean:26 - 看《Spring5实战》的是:冰点IT-icypt

以上就是三种配置的混合使用,这里只是将开发中常用的混合方式做一演示,大家可以在此基础上继续扩展,最终找到一套属于自己的个性化配置,以便在以后的工作或学习中使用。

关注我

冰点IT,探索最新开发技术,记录追求梦想的点点滴滴!


    长按二维码,或点击最上方蓝色的 冰点IT 关注我,您的关注是对我最好的鼓励!

以上是关于Spring5 导入和混合配置使用的主要内容,如果未能解决你的问题,请参考以下文章

Spring之导入和混合配置

Spring_总结_03_装配Bean之导入与混合配置

Spring-导入和混合配置

Spring5学习笔记(10) — “JdbcTemplate的概念及准备工作”

Spring5学习笔记(10) — “JdbcTemplate的概念及准备工作”

高级openg 混合,一个完整程序