首页 > 编程语言 >JavaConfig和XML之间的互相引用

JavaConfig和XML之间的互相引用

时间:2022-09-03 19:11:06浏览次数:56  
标签: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
From: https://www.cnblogs.com/guohongzhi/p/16653101.html

相关文章

  • XML与HTML的区别
    前言:最近课上讲到webservice的接口,有接触到XML格式的参数,加上同学们有接触过HTML,感觉这两者很相似,多次问到这两者有什么区别,所以特此写一篇文章来阐述一下。一、认识XML1......
  • <dependencyManagement>正确使用方法 多个子项目都引用同一样依赖,则可以避免在每个使用
    <dependencyManagement>正确使用方法dependencyManagement正确使用方法一、介绍Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式。在dependencyMan......
  • 使用JavaConfig实现配置
    @Configuration在一个类上加了@Configration之后就类似<beans><beanid="xxx" class="xxxxxx"/></beans>这个也会被Spring容器托管,注册到容器中,因为他本来就是一个@......
  • log4j.xml、log4j2.xml、log4j.properties都是什么?
    0.背景项目中用的SpringMVC框架,这里面用的log4j家族的东西作为日志管理,但是发现代码配置里比较混乱,log4j.xml、log4j2.xml、log4j.properties都有。这篇文章就来简单区分......
  • 使用dom4j xml解析文件数据
    XML<?xmlversion="1.0"encoding="ISO-8859-1"?><contactList><contactid="1"vip="true"><name>潘金莲</name><gender>女</gender>......
  • Spring装配bean之通过 XML代码装配 bean
      在Spring中,对象无需自己查找和创建与其所关联的其他对象。相反,容易负责把需要相互协作的对象引用赋予各个对象。例如,一个订单管理的组件需要信用卡认证组件,但它不需要......
  • C++左值、右值引用
    概念左值与右值是相对于赋值运算符(=)与累加运算符(+=),以下内容统称为运算符。左值:存放于运算符左边的值,凡是能取地址(&)都是左值。右值:存放于运算符右边的值,不能取地址(&)都是......
  • 1.Mybatis-XML模板
    SELECT sr.ROLE_IDASroleId, sr.ROLE_NAMEASroleName, sr.IS_ACTIVEASisActive, sr.REMARKASremark, sr.CREATE_DATETIMEAScreateDatetime, CON......
  • c++常量引用容易踩的坑,修改数据不同步
    正常情况下被引用的对象改变,常量引用的值也跟着改变。i和j是同一个对象,所以是同步的:inti=42;constint&j=i;i=43;cout<<j<<endl;cout<<i<<endl;//......
  • 修改Android apk的二进制文件AndroidManifest.xml,并重新签名打包apk
    最近使用uni-app混合式框架开发一个App,需要把AndroidApp隐藏图标并隐式启动。有三种方案:1.使用uni-app离线打包。2.在不改源码的情况下,只修改apk的二进制文件AndroidM......