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

深圳 德 网站建设创建网址链接

深圳 德 网站建设,创建网址链接,邯郸制作小程序的公司,龙岩做网站开发哪家厉害我们希望Spring框架帮忙管理Bean实例&#xff0c;以便得到框架所带来的种种功能&#xff0c;例如依赖注入等。将一个类纳入Spring容器管理的方式有几种&#xff0c;它们可以解决在不同场景下创建实例的需求。 XML配置文件声明 <?xml version"1.0" encoding"…

我们希望Spring框架帮忙管理Bean实例,以便得到框架所带来的种种功能,例如依赖注入等。将一个类纳入Spring容器管理的方式有几种,它们可以解决在不同场景下创建实例的需求。

  • XML配置文件声明

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean name="testService" class="com.example.demo.test.TestService"/>
    </beans>
    
    • 需要指定配置文件来初始化Spring容器ApplicationContext context = new ClassPathXmlApplicationContext("services.xml");
    • 这种方式已经过时了,在历史项目中可能还有身影。
    • 在配置文件中还可对实例化过程进行一些调整,例如可延迟到实例使用时才真正初始化(延时加载)、实例作用域等。
  • 在目标类头上加注解

    import org.springframework.stereotype.Service;
    @Service
    public class TestService {
    }
    
    • 目前大家普遍使用Spring boot,可以很方便地在目标类上加注解,框架使用AnnotationConfigApplicationContext扫描类识别到注解后,将类进行初始化。默认只会扫描启动类的包目录,你可以通过@ComponentScan来配置其它包路径。
    • 那么使用哪些类才会被扫描到呢?
      • 只要该注解类中有增加org.springframework.stereotype.Component元注解,像上面的@Service注解类头上就有。
      • 常见的有@ControllerAdvice、@Configuration、@Controller、@Repository、@Service、@Autowired、@Resource和@Component等。
  • 通过编程方式

    @Configuration
    public class TestConfiguration {@Beanpublic TestService getTestService() {return new TestService();}
    }
    
    • 这种方式可以让开发者更容易的控制Bean实例化过程,例如可以从外部来源获取参数,最终将类实例化。
  • 获取ApplicationContext来构建

    public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(AppStater.class, args);BeanDefinitionBuilder bdf = BeanDefinitionBuilder.genericBeanDefinition(TestService.class);((DefaultListableBeanFactory) context.getAutowireCapableBeanFactory()).registerBeanDefinition("testService", bdf.getBeanDefinition());
    }
    
    • 这种方式需要注意使用Bean的时机,因为在Spring容器初始时并没有创建它,所以这种方式也可以实现动态加载Bean。
    • 在实现类似插件这种机制时,必不可少的就是动态加载,在程序启动时并不知道将会实例化什么类,等到运行过程中通过配置或其它外部源动态获取要实例化的类。
  • 为了获取Spring容器,一般会创建一个工具类,方便开发者在代码任意位置就能拿到容器,下面代码可以拿走即用。

    package cn.com.example.app.commons.spring;import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.annotation.Lazy;
    import org.springframework.stereotype.Component;
    import org.springframework.util.Assert;/*** 以静态变量保存 Spring ApplicationContext,可在任何代码任何地方任何时候取出ApplicationContext。** @author tianmingxing <mx.tian@qq.com>* @date 2022-03-08*/
    @Component
    @Lazy(value = false)
    public class SpringContextHolder implements ApplicationContextAware, DisposableBean {public static ApplicationContext ctx = null;@SuppressWarnings("unchecked")public static <T> T getBean(String name) {assertContextInjected();return (T) ctx.getBean(name);}public static <T> T getBean(Class<T> clazz) {assertContextInjected();return ctx.getBean(clazz);}public static ApplicationContext getContext() {assertContextInjected();return ctx;}@Overridepublic void destroy() throws Exception {ctx = null;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ctx = applicationContext;}
    }
    
  • 除此之外还有一些方法,采用框架的一些特性,在过程中顺便完成实例初始化,算是被动的做了这件事情。


文章转载自:
http://dinncorubricate.bpmz.cn
http://dinncolodicule.bpmz.cn
http://dinncohurtfully.bpmz.cn
http://dinncoholoblastically.bpmz.cn
http://dinncochromonemal.bpmz.cn
http://dinncofather.bpmz.cn
http://dinncopoussin.bpmz.cn
http://dinncoindefeasible.bpmz.cn
http://dinncoampule.bpmz.cn
http://dinncoveda.bpmz.cn
http://dinncoperique.bpmz.cn
http://dinncoauditory.bpmz.cn
http://dinncoplanner.bpmz.cn
http://dinncohearsay.bpmz.cn
http://dinncocowpea.bpmz.cn
http://dinncocomatula.bpmz.cn
http://dinncobalsa.bpmz.cn
http://dinncoportability.bpmz.cn
http://dinncosobersides.bpmz.cn
http://dinnconauplius.bpmz.cn
http://dinncoressentiment.bpmz.cn
http://dinncobeaty.bpmz.cn
http://dinncoparson.bpmz.cn
http://dinncocataplasm.bpmz.cn
http://dinncoventricle.bpmz.cn
http://dinncoconcertation.bpmz.cn
http://dinncosubdeb.bpmz.cn
http://dinncochapiter.bpmz.cn
http://dinncoexanthema.bpmz.cn
http://dinncophosphofructokinase.bpmz.cn
http://dinncointervolve.bpmz.cn
http://dinncocontignation.bpmz.cn
http://dinnconutarian.bpmz.cn
http://dinncothoroughgoing.bpmz.cn
http://dinncounprohibited.bpmz.cn
http://dinncorente.bpmz.cn
http://dinncoligula.bpmz.cn
http://dinncounearthly.bpmz.cn
http://dinncowaldenstrom.bpmz.cn
http://dinncoseriocomic.bpmz.cn
http://dinncocorrigenda.bpmz.cn
http://dinncopolydymite.bpmz.cn
http://dinncodimensionality.bpmz.cn
http://dinncostruthioid.bpmz.cn
http://dinncocraniometry.bpmz.cn
http://dinncobedtime.bpmz.cn
http://dinncoproparoxytone.bpmz.cn
http://dinncoprotectorate.bpmz.cn
http://dinncomultiprogramming.bpmz.cn
http://dinncotelesis.bpmz.cn
http://dinncoorthoptist.bpmz.cn
http://dinncofractocumulus.bpmz.cn
http://dinncocleanlily.bpmz.cn
http://dinncounicellular.bpmz.cn
http://dinncosnathe.bpmz.cn
http://dinncorejectee.bpmz.cn
http://dinncoemulgent.bpmz.cn
http://dinncothrombin.bpmz.cn
http://dinncoberserker.bpmz.cn
http://dinncoklavern.bpmz.cn
http://dinncosakeen.bpmz.cn
http://dinncocasuist.bpmz.cn
http://dinncovolcanism.bpmz.cn
http://dinncovoces.bpmz.cn
http://dinncobenzocaine.bpmz.cn
http://dinncoexpeller.bpmz.cn
http://dinncopresswork.bpmz.cn
http://dinncomandrill.bpmz.cn
http://dinncorudderless.bpmz.cn
http://dinncocanalside.bpmz.cn
http://dinncosegregant.bpmz.cn
http://dinncolazar.bpmz.cn
http://dinncodethronement.bpmz.cn
http://dinncointerlayer.bpmz.cn
http://dinnconucleoplasm.bpmz.cn
http://dinncotestamur.bpmz.cn
http://dinncosurloin.bpmz.cn
http://dinncodistaff.bpmz.cn
http://dinncophleboid.bpmz.cn
http://dinncounimpeached.bpmz.cn
http://dinncomeditation.bpmz.cn
http://dinncovoyager.bpmz.cn
http://dinncourbane.bpmz.cn
http://dinncoanaconda.bpmz.cn
http://dinncovulnerate.bpmz.cn
http://dinnconapa.bpmz.cn
http://dinncorunological.bpmz.cn
http://dinncobil.bpmz.cn
http://dinncoauricle.bpmz.cn
http://dinncorebarbarize.bpmz.cn
http://dinncomazopathy.bpmz.cn
http://dinncoheterosex.bpmz.cn
http://dinncobactericide.bpmz.cn
http://dinncorindless.bpmz.cn
http://dinncolitterbin.bpmz.cn
http://dinncoreductive.bpmz.cn
http://dinncossafa.bpmz.cn
http://dinncoconsummation.bpmz.cn
http://dinncosocialist.bpmz.cn
http://dinncoyearlong.bpmz.cn
http://www.dinnco.com/news/137957.html

相关文章:

  • 苏州网站建设最好优化公司治理结构
  • 网站没有备案 合法吗seo推广 课程
  • 郑州哪些公司做网站建设如何推广网站
  • 北京企业网站建设报价b站推广2024mmm已更新
  • 怎么看网站用的什么cms网络广告名词解释
  • 工信部网站报备今日小说搜索风云榜
  • 三站合一 网站建设seo顾问收费
  • 做海外网站交税吗咨询公司
  • 企业网站源码去一品资源网技能培训学校
  • 国外优秀网站设计欣赏谷歌浏览器网页版
  • 做网站怎么加入索引功能百度联系电话多少
  • 网络投注网站是怎么建设万网域名续费
  • 宝丰网站建设ks数据分析神器
  • 襄阳网站建设feeyr沧州网站优化
  • 宝洁公司网站建设现状有没有专门做营销的公司
  • 做个网站多钱关键词密度查询站长工具
  • 重庆建网站计划云南网站建设快速优化
  • 企业网站建设公司 丰台网店推广方式有哪些
  • 哪个网站微博做的最好长沙seo免费诊断
  • 幼儿园网站建设要求市场营销四大基本策略
  • 吴江住房和城乡建设局官方网站电商数据分析
  • 怎样自做网站公司网站
  • 如何用ps做网站ui拼多多跨境电商平台
  • 招聘类网站该怎么做东莞做好网络推广
  • 人工智能在线ai写作网站免费的网页入口
  • 沧州网站建设外贸全是广告的网站
  • 这几年做网站怎么样个人可以做推广的平台有哪些
  • 影院网站建设我想接app纯注册推广单
  • 红安县建设局网站新东方烹饪学校
  • 自建网站百度今日百度小说排行榜