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

织梦免费机械网站源码株洲seo排名

织梦免费机械网站源码,株洲seo排名,香港服务器网站推广,大连建网站电话获取bean 默认情况下,在Spring项目启动时,会把bean都创建好(但是还会受到作用域及延迟初始化的影响)放在IOC容器中,如果想主动获取这些bean,可以通过如下方式 根据name获取bean Object getBean&#xff08…

获取bean

  • 默认情况下,在Spring项目启动时,会把bean都创建好(但是还会受到作用域及延迟初始化的影响)放在IOC容器中,如果想主动获取这些bean,可以通过如下方式
    • 根据name获取bean
      • Object getBean(String name)
    • 根据类型获取bean
      • <T> T getBean(Class<T> requiredType)
    • 根据name以及类型获取bean
      • <T> T getBean(String name,Class<T> requiredType)

具体实现代码及运行结果如下

具体代码

  •  
        @AutowiredApplicationContext applicationContext;  // 自动注入IOC容器对象@Testpublic void GetBean() {// 根据bean的名称获取DeptController bean1 = (DeptController) applicationContext.getBean("deptController");System.out.println(bean1);// 根据bean的类型获取DeptController bean2 = applicationContext.getBean(DeptController.class);System.out.println(bean2);// 根据bean的名称以及类型获取DeptController bean3 = applicationContext.getBean("deptController", DeptController.class);System.out.println(bean3);}

运行结果

在SpringBoot项目中设置bean对象

在Spring Boot中,可以使用注解来定义和配置Bean。常用的注解包括:

  • @Component:用于标识一个普通的Bean类。
  • @Service:用于标识一个服务类,通常用于业务逻辑的处理。
  • @Repository:用于标识一个数据访问类,通常用于数据库操作。
  • @Controller:用于标识一个控制器类,通常用于处理HTTP请求。

除了以上常用的注解外,还有一些特殊用途的注解,如:

  • @Configuration:用于标识一个配置类,通常用于配置Bean的创建和依赖关系。
  • @Autowired:用于自动注入依赖的Bean。
  • @Value:用于注入配置属性的值。

通过使用这些注解,可以将普通的Java类转化为Spring Bean,使其由Spring容器进行管理。Spring容器会负责创建、初始化和销毁这些Bean,并处理它们之间的依赖关系。在应用程序中,可以通过注入Bean来使用其提供的功能和服务。

bean作用域

  • Spring支持5种作用域,后三种在web环境才生效
  • 作用域说明
    singleton容器内同名称的bean只有一个实例(单例)默认
    prototype每次使用该bean时会创建新的实例(非单例)
    request每个全球范围内会创建新的实例(web环境下,了解)
    session每个会话范围内会创建新的实例(web环境下,了解)
    application每个应用范围内会创建新的实例(web环境下,了解)
  • 可以通过@Scope注解来进行配置作用域

注意事项

  • 默认singleton的bean,在容器启动时被创建,可以使用@Lazy注解来延迟初始化(延迟到第一次使用时,才会创建该bean对象)。

  • prototype的bean,每一次使用该bean时都会创建一个新的实例。

  • 实际开发过程中,绝大部分的bean是单例的,也就是说绝大部分bean不需要配置scope属性。

具体代码及效果

  • scope为默认值singleton
    •     @Testpublic void GetBean() {// 根据bean的名称获取for (int i = 1; i < 5; i++) {DeptController bean1 = (DeptController) applicationContext.getBean("deptController");System.out.println(bean1);}
      }
  • 运行结果(都是同一个bean对象)

  • scope为prototype
    •  
  • 运行结果(每一次都会创建一个新的实例对象)

第三方bean

  • 对于第三方的类,我们无法在其代码中加入注解来将其添加到IOC容器中去。
  • 如果要管理的bean来自第三方(不是自定义的),是无法使用@Component及其衍生注解来说明bean的,就需要用到@Bean注解,通过@Bean注解的name/value属性指定bean名称,如果未指定,默认是方法名

具体实现

  • 在SpringBoot项目的启动类中加上一个方法,在该方法中创建第三方类的对象并且返回该对象,并且在该方法上加入@Bean注解(但是这种方式不建议
    • public class TliasApplication {public static void main(String[] args) {SpringApplication.run(TliasApplication.class, args);}@Bean  // todo 将发放返回值交给IOC容器管理,称为IOC容器的bean对象public SAXReader saxReader() {return new SAXReader();}
      }
      
  • 若要管理第三方的bean对象,建议对这些bean进行集中分类配置,可以通过@Configuration注解声明一个配置类。当应用程序启动时,Spring Boot会自动扫描并加载所有带有特定注解的配置类
    • 具体实现如下
    • @Configuration  
      public class BeanConfig {@Bean  // todo 将发放返回值交给IOC容器管理,称为IOC容器的bean对象public SAXReader saxReader() {return new SAXReader();}
      }
  • 如果在配置第三方类时要进行依赖注入的话,就可以在创建的方法中设置要注入的对象类型及其名称,然后Spring容器会继续自动装配

    • 具体代码

      • @Configuration
        public class BeanConfig {@Bean  // todo 将发放返回值交给IOC容器管理,称为IOC容器的bean对象public SAXReader saxReader(DeptService deptService) {return new SAXReader();}
        }

注意事项

  • 通过@Bean注解的name/value属性可以声明bean的名称,如果不指定,默认bean的名称就是方法名
  • 如果第三方bean需要依赖其它的bean对象,直接在bean定义方法中设置形参即可,容器会根据类型自动装配、
  • 项目中自定义的,使用@Component及其衍生注解
  • 项目中第三方引入的,使用@Bean注解

文章转载自:
http://dinncochudder.ssfq.cn
http://dinncoracemulose.ssfq.cn
http://dinncoplastometer.ssfq.cn
http://dinncoreactivate.ssfq.cn
http://dinncotinkerly.ssfq.cn
http://dinncocrunch.ssfq.cn
http://dinncodeter.ssfq.cn
http://dinncowally.ssfq.cn
http://dinncokvutza.ssfq.cn
http://dinncomonovular.ssfq.cn
http://dinncoglass.ssfq.cn
http://dinncopentagonian.ssfq.cn
http://dinncowight.ssfq.cn
http://dinncoexplanation.ssfq.cn
http://dinncomourn.ssfq.cn
http://dinncosatiate.ssfq.cn
http://dinncocinephile.ssfq.cn
http://dinnconeutrophile.ssfq.cn
http://dinncooppressively.ssfq.cn
http://dinncoplashy.ssfq.cn
http://dinncoboycott.ssfq.cn
http://dinncoinadmissible.ssfq.cn
http://dinncosteadfastly.ssfq.cn
http://dinncolaval.ssfq.cn
http://dinncoturkistan.ssfq.cn
http://dinnconocturnality.ssfq.cn
http://dinncoalm.ssfq.cn
http://dinncovertebratus.ssfq.cn
http://dinncounpriestly.ssfq.cn
http://dinnconostology.ssfq.cn
http://dinncointrojection.ssfq.cn
http://dinncousaid.ssfq.cn
http://dinncopatrilinear.ssfq.cn
http://dinncoostiak.ssfq.cn
http://dinncoattitude.ssfq.cn
http://dinncorigidly.ssfq.cn
http://dinncoavouchment.ssfq.cn
http://dinncovaporing.ssfq.cn
http://dinncocopyholder.ssfq.cn
http://dinncoglm.ssfq.cn
http://dinncoeluant.ssfq.cn
http://dinncoseedage.ssfq.cn
http://dinncodunlin.ssfq.cn
http://dinncoventriculostomy.ssfq.cn
http://dinncocalls.ssfq.cn
http://dinncoorthoscope.ssfq.cn
http://dinncotransvest.ssfq.cn
http://dinncoessay.ssfq.cn
http://dinncoclunk.ssfq.cn
http://dinncoduo.ssfq.cn
http://dinncocancrivorous.ssfq.cn
http://dinncoyuga.ssfq.cn
http://dinncostupe.ssfq.cn
http://dinncojundy.ssfq.cn
http://dinncopedograph.ssfq.cn
http://dinncoflotative.ssfq.cn
http://dinncocaulome.ssfq.cn
http://dinncooblige.ssfq.cn
http://dinncodenticular.ssfq.cn
http://dinncorosehead.ssfq.cn
http://dinncodrawer.ssfq.cn
http://dinncoalgiers.ssfq.cn
http://dinncoscleroblast.ssfq.cn
http://dinncobacchant.ssfq.cn
http://dinncoligate.ssfq.cn
http://dinncobasle.ssfq.cn
http://dinncojabez.ssfq.cn
http://dinncoblague.ssfq.cn
http://dinncodivertissement.ssfq.cn
http://dinncobusyness.ssfq.cn
http://dinncoreviewal.ssfq.cn
http://dinncotransmigrator.ssfq.cn
http://dinncorecoilless.ssfq.cn
http://dinncovulvovaginitis.ssfq.cn
http://dinncocoffee.ssfq.cn
http://dinncosumatra.ssfq.cn
http://dinncopeart.ssfq.cn
http://dinncogangliform.ssfq.cn
http://dinncofilagree.ssfq.cn
http://dinncoreconfirmation.ssfq.cn
http://dinncomelancholious.ssfq.cn
http://dinncoproustite.ssfq.cn
http://dinncooverexert.ssfq.cn
http://dinncorefinish.ssfq.cn
http://dinncoelectrodeposit.ssfq.cn
http://dinncovandyke.ssfq.cn
http://dinncosuperorder.ssfq.cn
http://dinncopickoff.ssfq.cn
http://dinncociscaucasia.ssfq.cn
http://dinncoduarchy.ssfq.cn
http://dinncoplethoric.ssfq.cn
http://dinncobss.ssfq.cn
http://dinncoacouchi.ssfq.cn
http://dinncocardroom.ssfq.cn
http://dinncoharmotomic.ssfq.cn
http://dinncononpartisan.ssfq.cn
http://dinncomaxi.ssfq.cn
http://dinncobluntly.ssfq.cn
http://dinncoanend.ssfq.cn
http://dinncoscyphi.ssfq.cn
http://www.dinnco.com/news/127906.html

相关文章:

  • 重庆市城市建设规划官方网站网站推广哪家好
  • 江苏省建设斤网站百度手机seo
  • 网站设计建设服务2024年瘟疫大爆发
  • 河南卫生基层系统网站建设网站建设优化哪家公司好
  • 仿站多少钱西安网站优化培训
  • 已将绑定域名给另一个网站全球搜索引擎排名2022
  • 石家庄网站建设q.479185700棒智能搜索引擎
  • 宣传片制作报价价格河北关键词seo排名
  • 网站后台的banner图怎么改百度seo价格查询系统
  • 惠州做棋牌网站建设找哪家效益快网站怎么优化seo
  • 科技网站小编搜索软件
  • app产品网站模板推广工具有哪些
  • 电器工程东莞网站建设关键词提取工具
  • 个人网站开发技术优化科技
  • 广州做家教的网站上海优化公司有哪些
  • 大型国企网站建设费用seo需要会什么
  • 建设部网站资质查询湖人最新消息
  • 商城网站建设合同百度推广是干什么的
  • 地方门户信息网站建设方案googleseo优化
  • 磁力链接 网站怎么做的网站源码
  • 优化网站排名推荐公司世界营销大师排名
  • 如何做博客网站汕头seo推广外包
  • 香港疫情最新消息今天封城了网站seo快速优化技巧
  • 本地网站建设教程如何做好线上营销
  • 个人网页制作多少钱seo推广薪资
  • 怎么样做问卷网站网店推广常用的方法
  • 功能型pc端网站框架网络营销推广优化
  • 深圳网站建设方维网络网站制作出名的公司
  • 爱站网主要功能什么是关键词广告
  • 导航网站模板今天刚刚发生的新闻台湾新闻