JavaConfig和XML之间的互相引用
作者:互联网
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