简单来说,4种方式
接下来我们针对每种方式,来演示一下
一般我们自己写的代码都是通过这种方式来实现的bean加载到ioc容器中
查考: Spring5源码 - Spring IOC 注解复习 @CompentScan 部分
通常我们初始化Redis 、数据库等等,都会使用这种方式,即 适用于导入第三方组件的类
举个例子
@Bean public JedisPool redisPoolFactory() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); return jedisPool; }
第三方的组件 可以使用这种方式
导入的组件的id为类的全路径名
【components】
package com.artisan.base.importTest.component; public class Bean7 { }
package com.artisan.base.importTest.component; public class Bean8 { }
【配置类】
package com.artisan.base.importTest.config; import com.artisan.base.importTest.component.Bean7; import com.artisan.base.importTest.component.Bean8; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(value = {Bean7.class, Bean8.class}) public class IMPConfig { }
【验证】
package com.artisan.base.importTest; import com.artisan.base.importTest.config.IMPConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author 小工匠 * @version 1.0 * @description: TODO * @date 2020/10/11 19:05 * @mark: show me the code , change the world */ public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(IMPConfig.class); for (String beanDefinitionName : ac.getBeanDefinitionNames()) { System.out.println(beanDefinitionName); } System.out.println("========================"); // beanname为全路径名 System.out.println(ac.getBean("com.artisan.base.importTest.component.Bean7")); } }
【自定義ImportSelector】
package com.artisan.base.importTest.importSelector; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; /** * @author 小工匠 * @version 1.0 * @description: * @date 2020/10/11 19:20 * @mark: show me the code , change the world */ public class ArtisanImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"com.artisan.base.importTest.component.Bean6666"}; } }
【测试】
package com.artisan.base.importTest.importSelector; import com.artisan.base.importTest.component.Bean7777; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; /** * @author 小工匠 * @version 1.0 * @description: TODO * @date 2020/10/11 19:26 * @mark: show me the code , change the world */ public class ArtisanBeanDefinitionRegister implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Bean7777.class); registry.registerBeanDefinition("bean7777",rootBeanDefinition); } }
【配置类】
【测试结果】
比如整合第三方框架,MyBatis
Spring5源码 - 08 BeanFactory和FactoryBean 源码解析 & 使用场景
【FactoryBean】
package com.artisan.base.factoryBean; import org.springframework.beans.factory.FactoryBean; /** * @author 小工匠 * @version 1.0 * @description: TODO * @date 2020/10/11 21:49 * @mark: show me the code , change the world */ public class ArtisanFactoryBean implements FactoryBean { @Override public Object getObject() throws Exception { return new Bean8(); } @Override public Class<?> getObjectType() { return Bean8.class; } @Override public boolean isSingleton() { return true; } }
【配置类】
package com.artisan.base.factoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FBConfig { // 实例化ArtisanFactoryBean @Bean public ArtisanFactoryBean artisanFactoryBean() { return new ArtisanFactoryBean(); } }
【pojo】
package com.artisan.base.factoryBean; public class Bean8 { public Bean8() { System.out.println("Bean8 Create"); } }
【测试】
package com.artisan.base.factoryBean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(FBConfig.class); System.out.println("========================="); // 调用FactoryBean的getObject方法 System.out.println(ac.getBean("artisanFactoryBean")); // & 获取FactoryBean本身 System.out.println(ac.getBean("&artisanFactoryBean")); } }