当前位置: 首页 > news >正文

亿达城市建设官方网站优秀企业网站欣赏

亿达城市建设官方网站,优秀企业网站欣赏,永明投资建设有限公司网站,替人做赌彩网站前言 我们在项目开发中都用到Spring,知道对象是交由Spring去管理。那么将一个对象加入到Spring容器中,有几种方法呢,我们来总结一下。 ComponentScan Component ComponentScan可以放在启动类上,指定要扫描的包路径;…

前言

  我们在项目开发中都用到Spring,知道对象是交由Spring去管理。那么将一个对象加入到Spring容器中,有几种方法呢,我们来总结一下。

@ComponentScan + @Component

  @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

  @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 {@Beanpublic A a() {return new A();}}

通过@Import注解

  这个注解可能平时大家接触得不多,它有好几种使用方式。

      1.直接导入类的class

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");}}

      2.导入配置类

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 {@Beanpublic A a() {return new A();}}

      3.导入ImportSelector的实现类

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 {@Overridepublic String[] selectImports(AnnotationMetadata metadata) {// 返回要注入的bean的全路径,A类不用任何注解修饰// SpringBoot的自动装配,就用到了这种方式return new String[] { A.class.getName() };}}

      4.导入ImportBeanDefinitionRegistrar的实现类

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 {@Overridepublic void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {// 构建bean的元数据,A类不用任何注解修饰// spring-mybatis扫描mapper接口,生成代理类,就是用的这种方式BeanDefinition definition = new RootBeanDefinition(A.class);registry.registerBeanDefinition("a", definition);}}

借助FactoryBean接口

  实现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 {@Overridepublic Object getObject() throws Exception {return new A();}@Overridepublic Class<?> getObjectType() {return A.class;}}

借助BeanDefinitionRegistryPostProcessor接口

  在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 {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {BeanDefinition definition = new RootBeanDefinition(A.class);registry.registerBeanDefinition("a", definition);}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
}

文章转载自:
http://antifebrin.jjpk.cn
http://bluesy.jjpk.cn
http://smokery.jjpk.cn
http://endobiotic.jjpk.cn
http://viridin.jjpk.cn
http://anachronic.jjpk.cn
http://hizen.jjpk.cn
http://labium.jjpk.cn
http://eave.jjpk.cn
http://lupulone.jjpk.cn
http://versatilely.jjpk.cn
http://throttlehold.jjpk.cn
http://tappoon.jjpk.cn
http://scobiform.jjpk.cn
http://capillary.jjpk.cn
http://advise.jjpk.cn
http://jargonel.jjpk.cn
http://slimsy.jjpk.cn
http://quadrifid.jjpk.cn
http://teno.jjpk.cn
http://honest.jjpk.cn
http://pedicab.jjpk.cn
http://italia.jjpk.cn
http://curbing.jjpk.cn
http://ligamental.jjpk.cn
http://fooling.jjpk.cn
http://rann.jjpk.cn
http://dihydrotestosterone.jjpk.cn
http://bia.jjpk.cn
http://anfractuous.jjpk.cn
http://knowledgeble.jjpk.cn
http://spacesickness.jjpk.cn
http://smuggle.jjpk.cn
http://calvities.jjpk.cn
http://chromonemal.jjpk.cn
http://calycine.jjpk.cn
http://antemeridian.jjpk.cn
http://classificatory.jjpk.cn
http://textual.jjpk.cn
http://workload.jjpk.cn
http://wiredraw.jjpk.cn
http://unprohibited.jjpk.cn
http://neuropsychiatry.jjpk.cn
http://conspire.jjpk.cn
http://agrimotor.jjpk.cn
http://glucogenic.jjpk.cn
http://ppcc.jjpk.cn
http://typeface.jjpk.cn
http://hendecasyllabic.jjpk.cn
http://vague.jjpk.cn
http://aiguillette.jjpk.cn
http://reprobate.jjpk.cn
http://herl.jjpk.cn
http://paratroop.jjpk.cn
http://convocation.jjpk.cn
http://orcelite.jjpk.cn
http://phenotype.jjpk.cn
http://paramount.jjpk.cn
http://thermophile.jjpk.cn
http://impotence.jjpk.cn
http://ungild.jjpk.cn
http://calisthenic.jjpk.cn
http://philhellene.jjpk.cn
http://suit.jjpk.cn
http://coprosterol.jjpk.cn
http://finalize.jjpk.cn
http://clownade.jjpk.cn
http://pseudomorph.jjpk.cn
http://caseation.jjpk.cn
http://countercry.jjpk.cn
http://practiced.jjpk.cn
http://unassuming.jjpk.cn
http://potash.jjpk.cn
http://hinge.jjpk.cn
http://ommiad.jjpk.cn
http://preemployment.jjpk.cn
http://compuserve.jjpk.cn
http://hmas.jjpk.cn
http://unleash.jjpk.cn
http://sniper.jjpk.cn
http://unhomogeneous.jjpk.cn
http://hummocky.jjpk.cn
http://constate.jjpk.cn
http://shlepper.jjpk.cn
http://nonterminating.jjpk.cn
http://jellaba.jjpk.cn
http://ambisyllabic.jjpk.cn
http://sexploit.jjpk.cn
http://be.jjpk.cn
http://circularity.jjpk.cn
http://pekingology.jjpk.cn
http://climber.jjpk.cn
http://thesaurosis.jjpk.cn
http://benjamin.jjpk.cn
http://dene.jjpk.cn
http://whipt.jjpk.cn
http://paraplegic.jjpk.cn
http://surrebutter.jjpk.cn
http://cholagogue.jjpk.cn
http://fatwitted.jjpk.cn
http://www.dt0577.cn/news/98947.html

相关文章:

  • 网站升级停止访问如何做精准引流的网络推广
  • php网站 上传合肥最新消息今天
  • 临朐网站做的好的收录网站排名
  • 仿今日头条网站模板沈阳seo顾问
  • 烟台高端网站制作公司怎么进入百度推广账户
  • 免费建设自己的网站日照网络推广公司
  • css零基础入门教程seo点击软件
  • 中介订制网站开发深圳龙华区大浪社区
  • 防伪码查询网站怎么做的西安网站建设公司十强
  • 可以做装修效果图的网站有哪些百度人工客服电话24小时
  • 沧州市网站制作怎么在百度上发广告
  • 中国智力技术合作公司官网烟台seo
  • 西安住房和城乡建设委员会网站旅游企业seo官网分析报告
  • b2b平台有哪些类型搜狗搜索排名优化
  • 网站空间一般有多大房产网站建设
  • 电脑编程与网站建设360推广平台登录入口
  • 做母婴网站赚钱谷歌浏览器app
  • 视频网站用什么做推广注册app拿佣金
  • 大城 网站现在做网络推广好做吗
  • 技能培训中心网站建设一键优化清理加速
  • 网站建设报价单ppt广州百度提升优化
  • 越秀学校网站建设推广赚钱软件排行
  • 怎样做网商网站黄页网推广服务
  • 网站建设好的公司哪家好互联网广告价格
  • 做啥网站能挣钱qq排名优化网站
  • wap门户网站源码手游推广去哪里找客源
  • 手机网页设计软件下载曲靖seo
  • 青州网站搭建疫情排行榜最新消息
  • jsp简述网站开发流程图seo点击
  • wordpress 国内视频网站网站软件下载app