ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaConfig和XML之间的互相引用

2022-09-03 19:34:51  阅读:181  来源: 互联网

标签:XML springframework public JavaConfig 引用 import org Import annotation


JavaConfig引用JavaConfig

   现在,我们临时假设 PersonConfig 已经变得有些笨重,我们想要将其进行拆分。当然,它目前只定义了两个 bean,远远称不上复杂的 Spring 配置。不过,我们假设两个 bean 就已经太多了。

package person;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;

/**
 * Created on 2022/9/3.
 *
 * @author 国洪志
 */
@Configurable
public class PersonConfig{
    @Bean
    public Game game() {
        return new Lol();
    }

    @Bean
    public User user(Game game) {
        return new User(game);
    }
}

   实现的一种方案就是将 PersonConfig拆分,定义到它自己的 Config类中。

package person;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;

/**
 * Created on 2022/9/3.
 *
 * @author 国洪志
 */
@Configurable
public class GameConfig {
    @Bean
    public Game game() {
        return new Lol();
    }
    
}

package person;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * Created on 2022/9/3.
 *
 * @author 国洪志
 */
@Configurable
@Import(GameConfig.class)
public class UserConfig {
    @Bean
    public User user(Game game) {
        return new User(game);
    }
}

UserConfig 中使用 @Import 注解导入 GameConfig; 或者采用一个更好的办法,也就是不在 UserConfig 中使用 @Import,而是创建一个更高级别的 PlayGameConfig ,在这个类中使用 @Import 将两个配置类组合在一起:
package person;

import org.springframework.context.annotation.Import;

/**
 * Created on 2022/9/3.
 *
 * @author 国洪志
 */
@Configuration
@Import({GameConfig.class,UserConfig.class})
public class PlayGameConfig {
}

在JavaConfig中引用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" >

    <bean id="game" class="person.Lol">
    </bean>

</beans>
package person;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/**
 * Created on 2022/9/3.
 *
 * @author 国洪志
 */
@Configurable
@Import(UserConfig.class)
@ImportResource("classpath:person.xml")
public class PlayGameConfig {
}

在XML中引用XML

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

    <bean id="game" class="person.Lol">
    </bean>

</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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd" >
    <import resource="game.xml" />
    <bean id="user" class="person.User">
        <constructor-arg ref="game"/>
    </bean>

</beans>

在XML中引用JavaConfig



<bean id="user" class="person.User">
    <constructor-arg ref="game"/>
</bean>

标签:XML,springframework,public,JavaConfig,引用,import,org,Import,annotation
来源: https://www.cnblogs.com/guohongzhi/p/16653101.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有