1、@Import注解
-
@Import注解:用于向IOC容器中导入一个Bean,被导入的Bean对象名称是全路径名称。
-
该注解只能使用配置类注解(@Configuration)或者组件注解(@Component)中使用,否则无效。
@Configuration //配置类或者组件注解一起使用都可以
@Import(value = {Green.class, Pink.class})
public class MyConfig {
}
2、@ImportResource注解
-
@ImportResource注解主要用于导入Spring的xml配置文件注册的Bean。
-
该注解只能使用配置类注解(@Configuration)或者组件注解(@Component)中使用,否则无效。
@Controller
@ImportResource(locations = "classpath:beans.xml")
@Import(value = {Green.class, Pink.class})
public class MyConfig {
@Bean("deepSkyBlue")
public DeepSkyBlue deepSkyBlue(){
return new DeepSkyBlue();
}
}
beans.xml配置文件
<?xml version="1.0" encoding="UTF8"?>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="yellow" class="com.splay.pojo.Yellow">
</bean>
</beans>