1、創建bean的三種方式
使用bean注解直接注入
實現FactoryBean
在其他類中間接創建
1 @Configuration 2 public class MyConfig { 3 4 @Bean 5 @Scope("prototype") 6 public MyBean createBean(){ 7 return new MyBean(); 8 }
public class AppDemo { public static void main(String[] args){ AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class); System.out.println(context.getBean("Mybean")); context.close(); } }
public class MyBean { }
@Scope bean默認為單例的模式,使用Scop而注解可以更換為雙例
context.getBean() 可以通過類,和方法名進行獲取。方法名需要在Bean中加入方法名稱@Bean("createBean")
AnnotationConfigApplicationContext 源碼中 有多種注入類的方式,截取其中的2點
第二種方式
public class RunnableFactoryBean implements FactoryBean<jeep> { @Override public jeep getObject() throws Exception { return new jeep(); } @Override public Class<?> getObjectType() { return jeep.class; } @Override public boolean isSingleton() { return true; } }
@Bean public RunnableFactoryBean createRunnableFactoryBean(){ return new RunnableFactoryBean(); }
其中 FactoryBean 源碼如下,默認未單例模式
第三種方式
public class CarFactory { public cat createCat(){ return new cat(); } }
需要創建一個cat類
在CarFactory中創建cat
在配置文件中configration中注入bean
main方法輸出類
---------------------------------------------------
Bean的初始化方法
@使用Bean直接 @Bean(init-method=“”,destoryMethod=“”)
@使用注解PostContruts,PreDestory
@繼承InitialazingBean,DispoableBean 重寫其方法
第一種方式
public class Dag { public void init(){ System.out.println("進行數據的初始化"); } public void destory(){ System.out.println("進行數據的銷毀工作"); } }
配置文件添加其下
@Bean(initMethod="init",destroyMethod = "destory") public Dag createDog(){ return new Dag(); }
直接在main打印輸出即可
第二種方法
public class Dog { @PostConstruct public void init(){ System.out.println("進行數據的初始化"); } @PreDestroy public void destory(){ System.out.println("方法進行數據的銷毀工作"); } }
在配置文件中將Dog類進行注入
@Bean public Dog createDogs(){ return new Dog(); }
在main中獲取Dog的初始化
第三種方法
public class Car implements InitializingBean,DisposableBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet-------"); } @Override public void destroy() throws Exception { System.out.println("需要關閉容量才會執行的銷毀的方法"); } }
將Car類注入到容器
@Bean public Car creatCar(){ return new Car(); }
在main中打印輸出
-----------
Spring AnnotationConfigApplicationContext 掃包的方式
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) { this(); this.register(annotatedClasses); this.refresh(); } public AnnotationConfigApplicationContext(String... basePackages) { this(); this.scan(basePackages); this.refresh(); }
用第二種方式進行掃包
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(“加項目的路徑即可”);
列如
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(“com.demo.example”);
@ 如何排除不要的包
使用 componentScan注解
單獨創建一個配置類
添加掃描注解
添加配置注解
@ComponentScan(basePackages="com.example.demo", excludeFilters =@ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE,classes =cat.class )) @Configuration public class MyConfig2 { }
我們來看下@ComponentScan 底層代碼有哪些內容
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan { @AliasFor("basePackages") String[] value() default {}; @AliasFor("value") String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class; ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT; String resourcePattern() default "**/*.class"; boolean useDefaultFilters() default true; ComponentScan.Filter[] includeFilters() default {}; ComponentScan.Filter[] excludeFilters() default {}; boolean lazyInit() default false; @Retention(RetentionPolicy.RUNTIME) @Target({}) public @interface Filter { FilterType type() default FilterType.ANNOTATION; @AliasFor("classes") Class<?>[] value() default {}; @AliasFor("value") Class<?>[] classes() default {}; String[] pattern() default {}; } }
public enum FilterType { ANNOTATION, ASSIGNABLE_TYPE, ASPECTJ, REGEX, CUSTOM; private FilterType() { } }
里面包含了包含,以及 排除的方法,需要制定排除的類型有5種方法,怎末用可以看注解或者找度娘
我們選擇第二種方法需要排除一個配置類
exCludeFilter=@Filter(type="ASSIGNABLE_TYPE",classes="一個配置類
--------------------------------
AnnotationConfigAppliactionContext
使用完畢后一定要關掉,否則在生命周期的時候會不執行destory銷毀方法。
裝載bean的2種方式
@Configration
@Component,controller,service等