這篇文章主要介紹“Spring容器中添加bean的方式有哪些”,在日常操作中,相信很多人在Spring容器中添加bean的方式有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Spring容器中添加bean的方式有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
@Configuration + @Bean
@Componet + @ComponentScan
@Import注解導入
@Import直接導入類
@Import + ImportSelector
@Import + ImportBeanDefinitionRegistrar
@Import + DeferredImportSelector
使用FactoryBean接口
使用 BeanDefinitionRegistryPostProcessor
小結
我們知道平時在開發中使用Spring的時候,都是將對象交由Spring去管理,那么將一個對象加入到Spring容器中,有哪些方式呢,下面我就來總結一下
這種方式其實,在上一篇文章已經介紹過了,也是我們最常用的一種方式,@Configuration用來聲明一個配置類,然后使用 @Bean 注解,用于聲明一個bean,將其加入到Spring容器中。
具體代碼如下:
@Configuration public class MyConfiguration { @Bean public Person person() { Person person = new Person(); person.setName("spring"); return person; } }
這種方式也是我們用的比較多的方式,@Componet中文譯為組件,放在類名上面,然后@ComponentScan放置在我們的配置類上,然后可以指定一個路徑,進行掃描帶有@Componet注解的bean,然后加至容器中。
具體代碼如下:
@Component public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } } @ComponentScan(basePackages = "it.chusen.spring.*") public class Demo1 { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); } }
結果輸出:
Person{name='null'}
表示成功將Person放置在了IOC容器中。
前兩種方式,大家用的可能比較多,也是平時開發中必須要知道的,@Import注解用的可能不是特別多了,但是也是非常重要的,在進行Spring擴展時經常會用到,它經常搭配自定義注解進行使用,然后往容器中導入一個配置文件。關于@Import注解,我會多介紹一點,它有三種使用方式,我會一一介紹。這是@Import注解的源碼,表示只能放置在類上。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import { /** * 用于導入一個class文件 * {@link Configuration @Configuration}, {@link ImportSelector}, * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import. */ Class<?>[] value(); }
代碼示例如下:
public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } } /** * 直接使用@Import導入person類,然后嘗試從applicationContext中取,成功拿到 **/ @Import(Person.class) public class Demo1 { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); } }
上述代碼直接使用@Import導入了一個類,然后自動的就被放置在IOC容器中了。注意
我們的Person類上 就不需要任何的注解了,直接導入即可。
其實在@Import注解的源碼中,說的已經很清楚了,感興趣的可以看下,我們實現一個ImportSelector的接口,然后實現其中的方法,進行導入。
代碼如下:
@Import(MyImportSelector.class) public class Demo1 { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); } } class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"it.chusen.spring.model.Person"}; } }
我自定義了一個 MyImportSelector 實現了 selectImports 方法,然后將我們要導入的類的全限定名寫在里面即可,實現起來也是非常簡單。
這種方式也需要我們實現 ImportBeanDefinitionRegistrar 中的方法,具體代碼如下:
@Import(MyImportBeanDefinitionRegistrar.class) public class Demo1 { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); } } class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { // 構建一個beanDefinition, 關于beanDefinition我后續會介紹,可以簡單理解為bean的定義. AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition(); // 將beanDefinition注冊到Ioc容器中. registry.registerBeanDefinition("person", beanDefinition); } }
上述實現其實和Import的第二種方式差不多,都需要去實現接口,然后進行導入。接觸到了一個新的概念,BeanDefinition,可以簡單理解為bean的定義(bean的元數據),也是需要放在IOC容器中進行管理的,先有bean的元數據,applicationContext再根據bean的元數據去創建Bean。
這種方式也需要我們進行實現接口,其實它和@Import的第二種方式差不多,DeferredImportSelector 它是 ImportSelector 的子接口,所以實現的方法和第二種無異。只是Spring的處理方式不同,它和Spring Boot中的自動導入配置文件 延遲導入有關,非常重要。使用方式如下:
@Import(MyDeferredImportSelector.class) public class Demo1 { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); } } class MyDeferredImportSelector implements DeferredImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { // 也是直接將Person的全限定名放進去 return new String[]{Person.class.getName()}; } }
關于@Import注解的使用方式,大概就以上三種,當然它還可以搭配@Configuration注解使用,用于導入一個配置類。
FactoryBean接口和BeanFactory千萬不要弄混了,從名字其實可以大概的區分開,FactoryBean, 后綴為bean,那么它其實就是一個bean, BeanFactory,顧名思義 bean工廠,它是IOC容器的頂級接口,這倆接口其實都很重要。
代碼示例:
@Configuration public class Demo1 { @Bean public PersonFactoryBean personFactoryBean() { return new PersonFactoryBean(); } public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); } } class PersonFactoryBean implements FactoryBean<Person> { /** * 直接new出來Person進行返回. */ @Override public Person getObject() throws Exception { return new Person(); } /** * 指定返回bean的類型. */ @Override public Class<?> getObjectType() { return Person.class; } }
上述代碼,我使用@Configuration + @Bean的方式將 PersonFactoryBean 加入到容器中,注意,我沒有向容器中注入 Person, 而是直接注入的 PersonFactoryBean 然后從容器中拿Person這個類型的bean,成功運行。
其實這種方式也是利用到了 BeanDefinitionRegistry,在Spring容器啟動的時候會執行 BeanDefinitionRegistryPostProcessor 的 postProcessBeanDefinitionRegistry 方法,大概意思就是等beanDefinition加載完畢之后,對beanDefinition進行后置處理,可以在此進行調整IOC容器中的beanDefinition,從而干擾到后面進行初始化bean。
具體代碼如下:
public class Demo1 { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); MyBeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor = new MyBeanDefinitionRegistryPostProcessor(); applicationContext.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor); applicationContext.refresh(); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); } } class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition(); registry.registerBeanDefinition("person", beanDefinition); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } }
上述代碼中,我們手動向beanDefinitionRegistry中注冊了person的BeanDefinition。最終成功將person加入到applicationContext中,上述的幾種方式的具體原理,我后面會進行介紹。
介紹了向spring容器中加入bean的幾種方式.
@Configuration + @Bean
@ComponentScan + @Component
@Import 配合接口進行導入
使用FactoryBean。
實現BeanDefinitionRegistryPostProcessor進行后置處理。
其實向Spring中加入bean的方式有很多種,這里簡要介紹了上面這幾種。
到此,關于“Spring容器中添加bean的方式有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。