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

怎么自己搭建一个网站软件项目交易平台

怎么自己搭建一个网站,软件项目交易平台,商贸公司寮步网站建设价钱,项目协同管理软件目录 Bean作用域 单例模式(默认可以不写) Spring下的 AutoWired 依赖注入 JaveEE下的 Resource 依赖注入 多例模式 Bean作用域 ​在Spring框架中#xff0c;Bean是按照作用域来创建的#xff0c;常见的作用域有两种#xff1a;Singleton 和 Prototype。Singleton (单例…目录 Bean作用域 单例模式(默认可以不写) Spring下的 AutoWired 依赖注入 JaveEE下的 Resource 依赖注入 多例模式 Bean作用域 ​在Spring框架中Bean是按照作用域来创建的常见的作用域有两种Singleton 和 Prototype。Singleton (单例)是指整个应用中只有一个实例并在第一次请求时创建实例 Prototype (多例)是指每次请求都会创建一个新的实例并返回每个实例之间是相互独立的。 Scope注解指定bean的作用域 取值含义Scope(singleton)(默认)在IoC容器中,在bean的对象为单实例Scoper(prototype)在IoC容器中,在bean中有多个实例 单例模式(默认可以不写) Spring下的 AutoWired 依赖注入 注意的是Autowired 不能以类的名字来寻找对应的Product的实体类,需要通过Qualifier来查找对应的接口实体类,不确定对应的实体类的名会报NoUniqueBeanDefinitionException异常。 这个异常表明Spring容器中有多个相同类型的bean候选者但Spring不知道应该选择哪一个来注入 // 一个接口 interface Product { void test(); } Component(productDealWith1) // 使用 Component 并指定 bean 名称 class ProductDealWith1 implements Product { Override public void test() { System.out.println(ProductDealWith1 test method called.); } } Component(productDealWith) // 使用 Component 并指定 bean 名称 class ProductDealWith implements Product { Override public void test() { System.out.println(ProductDealWith test method called.); } } Component class ProductOrder { Autowired Qualifier(productDealWith1) // 使用 Qualifier 指定要注入的 bean 名称 private Product product; public void doSomething() { product.test(); // 这将调用 ProductDealWith1 的 test 方法 } } public class TestProduct { public static void main(String[] args) { ApplicationContext context new AnnotationConfigApplicationContext(demo.test.product); ProductOrder order context.getBean(ProductOrder.class); // 获取 ProductOrder 类型的 bean order.doSomething(); // 这将间接调用 ProductDealWith1 的 test 方法 } } ProductDealWith1  和 ProductDealWith   类都使用了 Component 注解并且分别通过 value 参数指定了它们的 bean 名称。在 ProductOrder 类中通过Autowired 和 Qualifier 注解我们指定了要注入的  Product 类型的 bean 是名为 productDealWith1 的那个。在 TestProduct 的 main 方法中我们通过 context.getBean(ProductOrder.class) 来获取 ProductOrder 类型的 bean并调用其 doSomething 方法这将间接调用ProductDealWith1 的 test方法 JaveEE下的 Resource 依赖注入 这边讲解一下顺便讲解Resource依靠name找寻接口的实例 因为Resource是Java EE 的一部分,如果您指定了 name 属性Spring 将会查找与指定名称匹配的 bean如果没有指定name  属性Spring 将会查找与注入点类型匹配的 bean Configueration是将该类转变成配置类 其次使用配置类扫描工具ComponentScan,使其在运行编译过程中优先加载其类 并根据其中的配置创建并管理相应的 bean Configuration ComponentScan(demo.test.product) public class AppConfig { // 该配置类告诉Spring在此包及其子包中查找带有Component注解的类 } // 一个接口 interface Product { void test(); } Component(name productDealWith1) // 使用 Component 并指定 bean 名称 class ProductDealWith1 implements Product { Override public void test() { System.out.println(ProductDealWith1 test method called.); } } Component(nameproductDealWith) // 使用 Component 并指定 bean 名称 class ProductDealWith implements Product { Override public void test() { System.out.println(ProductDealWith test method called.); } } Component class ProductOrder { Resource(name productDealWith1) private Product product; public void doSomething() { product.test(); // 这将调用 ProductDealWith1 的 test 方法 } } public class TestProduct { public static void main(String[] args) { ApplicationContext context new AnnotationConfigApplicationContext(AppConfig.class); ProductOrder order context.getBean(ProductOrder.class); // 获取 ProductOrder 类型的 bean order.doSomething(); // 这将间接调用 ProductDealWith1 的 test 方法 } } 多例模式 //这边是多实例 Scope(valueprototype) //这边是单实例 //Scope(valuesingleton) Component class Product{} public class TestDBConnect {Testpublic void testScope(){ApplicationContext context new AnnotationConfigApplicationContext(demo.test.product);// 第一次获取Product product1 context.getBean(product.class);System.out.println(product1);// 第二次获取Product product2 context.getBean(product.class);System.out.println(product2);} } 多例模式运行 当为多例模式 prototype 时多次获取bean实例的地址是不同的 单例模式运行 当为单例模式 singleton 时多次获取bean实例的地址是相同的 单例模式和多例模式的区别 单例模式适用于需要共享数据并且需要避免重复创建实例的情况。 而多例模式适用于需要动态地创建对象并提供独立实例的情况。
http://www.zqtcl.cn/news/795122/

相关文章:

  • 武进网站建设代理商google官网下载
  • 简单网站开发流程图知乎怎么申请关键词推广
  • 成寿寺网站建设公司文登区做网站的公司
  • 建设一个网站用什么软件下载阿里外贸平台网站建设
  • 可信网站myeclipse网站开发
  • 做设计找素材的 网站有哪些网站建设实训个人总结
  • 浙江省建设厅继续教育官方网站网站做vr的收费
  • 建造网站 备案苏州手机网站设计
  • 做外贸卖小商品是哪个网站手机首页设计
  • 大连网站制作公司营销策划公司有哪些职位
  • 2019深圳网站设计公司排名网站设计的思想
  • 试客那个网站做的好seo管理平台
  • 增加网站关键词库网盟推广合作
  • 企业门户网站内容建设濮阳网络培训基地
  • 做亚马逊运营要看哪些网站免费咨询电脑问题
  • 如何用html制作网站app开发要多少钱
  • 中国搜索提交网站信息网络犯罪
  • 网站服务器做下载链接分销平台系统源码
  • 网站管理助手建站沈阳专业网站建设企业
  • 企业网站开发公司大全建筑工程培训
  • 免费网站开发模板云南省网站开发软件
  • dede小游戏php网站源码广州网站vi设计报价
  • 邯郸建设局网站资质申报wordpress 前端 插件
  • 关于asp_sql网站开发的书籍小程序跳转网页方法
  • 昆明网站开发公司电话建设手机银行的网站
  • 福州建设注册中心网站怎么做公司展示网站
  • 网络营销网站建设知识平面设计初中毕业能学吗
  • 2019销售网站开发与设计现状怎么在网上卖东西赚钱
  • 做网站前后端的发布流程如何管理wordpress网站模板下载
  • 网站历史频道怎么做网站修改标题有影响吗