您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Spring容器注入bean的方法有哪些”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Spring容器注入bean的方法有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
  @ComponentScan可以放在啟動類上,指定要掃描的包路徑;該包路徑下被@Component修飾的類,都會被注入到Spring容器中。
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages = "com.gs.beanRegister") public class BootStrap { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BootStrap.class); A bean = context.getBean(A.class); bean.say(); } }
  com.gs.beanRegister包下:
import org.springframework.stereotype.Component; @Component public class A { public void say() { System.out.println("這是a"); } }
  注:在SpringBoot中,由于其自動裝配的特性,所以@ComponentScan可以不加,只要@Component修飾的類和啟動類在同一包下或者在啟動類所在包的子包下。
  @Configuration用來聲明一個配置類,如果它的方法被@Bean修飾,那么該方法返回的對象也會被注入到Spring容器中。
  代碼方面,BootStrap 類不動,A類的@Component去掉,com.gs.beanRegister包下建個配置類:
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Bean; @Configuration public class MyConfiguration { @Bean public A a() { return new A(); } }
  這個注解可能平時大家接觸得不多,它有好幾種使用方式。
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @Import(A.class) public class BootStrap { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BootStrap.class); A bean = context.getBean(A.class); //B bean = context.getBean(B.class); bean.say(); } }
  A類不用添加任何注解:
public class A { public void say() { System.out.println("這是a"); } }
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Import; @Import(MyConfiguration.class) public class BootStrap { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BootStrap.class); A bean = context.getBean(A.class); bean.say(); } }
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; // 使用@Import導入配置類時,@Configuration可以不加 //@Configuration public class MyConfiguration { @Bean public A a() { return new A(); } }
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Import; @Import(MyImportSelector.class) public class BootStrap { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BootStrap.class); A bean = context.getBean(A.class); bean.say(); } }
import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata metadata) { // 返回要注入的bean的全路徑,A類不用任何注解修飾 // SpringBoot的自動裝配,就用到了這種方式: // 返回配置類的全路徑,配置類的@Bean方法返回的對象也能注入到容器中 return new String[] { A.class.getName() }; } }
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Import; @Import(MyImportBeanDefinitionRegistrar.class) public class BootStrap { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BootStrap.class); A bean = context.getBean(A.class); bean.say(); } }
import org.springframework.beans.factory.config.BeanDefinition; 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; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { // 構建bean的元數據,A類不用任何注解修飾 // spring-mybatis掃描mapper接口,生成代理類,就是用的這種方式 BeanDefinition definition = new RootBeanDefinition(A.class); registry.registerBeanDefinition("a", definition); } }
  實現FactoryBean接口的類,除了本身會被注入外,getObject方法返回的對象也會被注入到Spring容器中。
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Import; @Import(MyFactoryBean.class) public class BootStrap { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BootStrap.class); A bean = context.getBean(A.class); bean.say(); } }
import org.springframework.beans.factory.FactoryBean; public class MyFactoryBean implements FactoryBean { @Override public Object getObject() throws Exception { return new A(); } @Override public Class<?> getObjectType() { return A.class; } }
  在Spring容器啟動時,會調用該接口的postProcessBeanDefinitionRegistry方法,大概意思是等BeanDefinition(上面提到的bean的元數據)加載完成后,再對它進行后置處理。所以可以在此調整BeanDefinition,從而把對應的bean注入。
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class BootStrap { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); BeanDefinitionRegistryPostProcessor postProcessor = new MyBeanDefinitionRegistryPostProcessor(); context.addBeanFactoryPostProcessor(postProcessor); context.refresh(); A a = context.getBean(A.class); a.say(); } }
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.RootBeanDefinition; public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { BeanDefinition definition = new RootBeanDefinition(A.class); registry.registerBeanDefinition("a", definition); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } }
讀到這里,這篇“Spring容器注入bean的方法有哪些”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。