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

Linux哪个版本做网站好接网站做项目赚钱吗

Linux哪个版本做网站好,接网站做项目赚钱吗,搜索引擎优化免费,汽车保险网站欢迎观看《Spring Framework实战》视频教程 方法注入 在大多数应用场景中#xff0c;容器中的大多数bean都是单例#xff08;singletons#xff09;的。当单例bean需要与另一个单例bean协作或非单例bean需与另一非单例bean协作时#xff0c;通常通过将一个bean定义为另一个…欢迎观看《Spring Framework实战》视频教程 方法注入 在大多数应用场景中容器中的大多数bean都是单例singletons的。当单例bean需要与另一个单例bean协作或非单例bean需与另一非单例bean协作时通常通过将一个bean定义为另一个bean的属性来处理依赖关系。当bean的生命周期不同时就会出现问题。假设单例bean A需要使用非单例prototypebean B可能是在A的每次方法调用时。容器只创建单例bean A一次因此只有一次机会设置属性。容器不能在每次需要bean B时为bean A提供bean B的新实例。 一个解决方案是放弃一些控制反转。您可以通过实现ApplicationContextAware接口并在每次bean A需要时对容器进行getBean“B”调用请求通常是新的bean B实例使bean A知道容器。 以下示例显示了这种方法 Java package org.examples; import java.util.Map; public class Command {     private Map state;     public Map getState() {         return state;     }     public void setState(Map state) {         this.state state;     }     public String execute() {         return Command this execute, state this.state;     } } package org.examples; import java.util.Map; /**  * 当bean的生命周期不同时就会出现问题。  */ public class CommandManager {     public Command command;     public Command getCommand() {         return command;     }     public void setCommand(Command command) {         this.command command;     }     public String process(Map State) {         // 在希望是全新的Command实例上设置状态         this.command.setState(State);         return this.command.execute();     } } bean idcommandManager classorg.examples.CommandManager scopesingleton     !-- collaborators and configuration for this bean go here --     property namecommand refcommand/ /bean bean idcommand classorg.examples.Command scopeprototype     !-- collaborators and configuration for this bean go here --     property namestate         map             entry keystate1 valueaaa/             entry keystate2 valuebbb/             entry keystate3 valueccc/         /map     /property /bean CommandManager commandManager (CommandManager) context.getBean(CommandManager.class); Map map1 new HashMap(); map1.put(state, state111); System.out.println(commandManager.process(map1)); Map map2 new HashMap(); map2.put(state, state222); System.out.println(commandManager.process(map2)); package org.examples; // Spring-API imports import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import java.util.Map; /**  * A class that uses a stateful Command-style class to perform  * some processing.  * p  * 您可以通过实现ApplicationContextAware接口  * 并在每次bean A需要时对容器进行getBean“B”调用  * 请求通常是新的bean B实例使bean A知道容器。  */ public class CommandManager implements ApplicationContextAware {     private ApplicationContext applicationContext;     public String process(Map commandState) {         // grab a new instance of the appropriate Command         // 获取相应命令的新实例         Command command createCommand();         // set the state on the (hopefully brand new) Command instance         // 在希望是全新的Command实例上设置状态         command.setState(commandState);         return command.execute();     }     protected Command createCommand() {         // notice the Spring API dependency!         return this.applicationContext.getBean(command, Command.class);     }     public void setApplicationContext(             ApplicationContext applicationContext) throws BeansException {         this.applicationContext applicationContext;     } } bean idcommandManager classorg.examples.CommandManager scopesingleton     !-- collaborators and configuration for this bean go here -- /bean bean idcommand classorg.examples.Command scopeprototype     !-- collaborators and configuration for this bean go here --     property namestate         map             entry keystate1 valueaaa/             entry keystate2 valuebbb/             entry keystate3 valueccc/         /map     /property /bean 前面的内容是不可取的因为业务代码意识到并耦合到Spring Framework。方法注入是Spring IoC容器的一个稍微高级的功能可以让你干净利落地处理这个用例。 你可以在这篇博客文章中关于方法注入的动机。 查找方法注入 查找方法注入是容器覆盖容器管理bean上的方法并返回容器中另一个命名bean的查找结果的能力。查找通常涉及一个原型bean如前一节所述的场景。Spring Framework通过使用CGLIB库中的字节码生成来动态生成覆盖该方法的子类从而实现了这种方法注入。 为了使这种动态子类工作Spring bean容器子类的类不能是final的要重写的方法也不能是final的。对具有抽象方法的类进行单元测试需要您自己对类进行子类化并提供抽象方法的存根实现。组件扫描也需要具体的方法这需要具体的类来拾取。另一个关键限制是查找方法不适用于工厂方法特别是不适用于配置类中的Bean方法因为在这种情况下容器不负责创建实例因此无法动态创建运行时生成的子类。 在前面代码片段中的CommandManager类的情况下Spring容器动态覆盖createCommand方法的实现。CommandManager类没有任何Spring依赖项如重新编写的示例所示 Java package org.examples; // no more Spring imports! public abstract class CommandManager { public Object process(Object commandState) { // grab a new instance of the appropriate Command interface Command command createCommand(); // set the state on the (hopefully brand new) Command instance command.setState(commandState); return command.execute(); } // okay... but where is the implementation of this method? protected abstract Command createCommand(); } 在包含要注入的方法在本例中为CommandManager的客户端类中要注入的方式需要以下形式的签名 public|protected [abstract] return-type theMethodName(no-arguments); 如果方法是抽象的则动态生成的子类实现该方法。否则动态生成的子类会覆盖原始类中定义的具体方法。考虑以下示例 !-- a stateful bean deployed as a prototype (non-singleton) -- bean idmyCommand classorg.examples.AsyncCommand scopeprototype !-- inject dependencies here as required -- /bean !-- commandManager uses myCommand prototype bean -- bean idcommandManager classorg.examples.CommandManager lookup-method namecreateCommand beanmyCommand/ /bean 标识为commandManager的bean在需要myCommand bean的新实例时调用自己的createCommand方法。如果确实需要您必须小心地将myCommand bean部署为原型。如果它是单例则每次都会返回相同的myCommand bean实例。 或者在基于注释的组件模型中您可以通过lookup注释声明查找方法如下例所示 Java public abstract class CommandManager { public Object process(Object commandState) { Command command createCommand(); command.setState(commandState); return command.execute(); } Lookup(myCommand) protected abstract Command createCommand(); } 或者更习惯地说你可以依靠目标bean根据查找方法的声明返回类型进行解析 Java public abstract class CommandManager { public Object process(Object commandState) { Command command createCommand(); command.setState(commandState); return command.execute(); } Lookup protected abstract Command createCommand(); } 请注意您通常应该使用具体的存根实现声明此类带注释的查找方法以便它们与Spring的组件扫描规则兼容默认情况下抽象类会被忽略。此限制不适用于显式注册或显式导入的bean类。 访问不同作用域的目标bean的另一种方法是ObjectFactory/Provider注入点。请参阅作为依赖关系的作用域Bean。 您可能还会发现ServiceLocatorFactoryBean在org.springframework.beans.factory.config包中很有用。 任意方法替换 一种不如查找方法注入有用的方法注入形式是能够用另一种方法实现替换托管bean中的任意方法。您可以安全地跳过本节的其余部分直到您真正需要此功能。 使用基于XML的配置元数据您可以使用替换的方法元素将已部署bean的现有方法实现替换为另一个方法实现。考虑以下类它有一个我们想要重写的名为computeValue的方法 Java public class MyValueCalculator { public String computeValue(String input) { // some real code... } // some other methods... } 一个实现org.springframework.beans.factory.support的类。MethodReplacer接口提供了新的方法定义如下例所示 Java /** * meant to be used to override the existing computeValue(String) * implementation in MyValueCalculator */ public class ReplacementComputeValue implements MethodReplacer { public Object reimplement(Object o, Method m, Object[] args) throws Throwable { // get the input value, work with it, and return a computed result String input (String) args[0]; ... return ...; } } 部署原始类并指定方法重写的bean定义类似于以下示例 bean idmyValueCalculator classx.y.z.MyValueCalculator !-- arbitrary method replacement -- replaced-method namecomputeValue replacerreplacementComputeValue arg-typeString/arg-type /replaced-method /bean bean idreplacementComputeValue classa.b.c.ReplacementComputeValue/ 您可以在replaced method/元素中使用一个或多个arg type/元素来指示被重写方法的方法签名。只有当方法重载并且类中存在多个变量时才需要参数的签名。为方便起见参数的类型字符串可以是完全限定类型名称的子字符串。例如以下所有匹配项 java.lang.String : java.lang.String String Str 因为参数的数量通常足以区分每种可能的选择所以这个快捷方式可以节省大量的输入只让你输入与参数类型匹配的最短字符串。
http://www.pierceye.com/news/689093/

相关文章:

  • 网站设计制作过程容桂做pc端网站
  • 宜昌市上海中学官网seo文章外包
  • 加强普法网站建设的通知制作婚恋网站
  • 北大荒建设集团有限公司网站网站添加在线qq聊天
  • 网站首页被k咋办上海市企业服务云登录
  • 长安镇网站建设公司大网站制作公司
  • 衡水做网站推广找谁廊坊百度推广排名优化
  • 网站建设毕业报告wordpress微信登录页面
  • 外包网站建设费用包括网站备份crm系统有哪些
  • 高端网站设计推广v信haotg8wordpress 付费后查看
  • cms管理手机网站长春做网站好的公司
  • 可信网站认证 技术支持单位沈阳又一烂尾项目复工
  • 南昌网站建设培训学校做幼儿网站的目标
  • 装饰网站建设的背景怎么做原创动漫视频网站
  • 电脑购物网站模板c2c商城网站建设二次开发
  • 自建站有哪些dz论坛网站源码
  • 湖南湘源建设工程有限公司网站牛商网网站做seo好么
  • 郑州网站制作建设南宁网站设计要多少钱
  • 网站地图制作怎么做云南建设网
  • 网站开发项目经验怎么写网站首页做一点开有动画
  • 乔拓云智能建站系统官网注册公司在哪个网站注册
  • lisp 网站开发什么是网站设计
  • 北京市残疾人网上服务平台网奇seo培训官网
  • 福建省建设环卫协会网站自动的小企业网站建设
  • 深圳关键词seo北京网络优化推广公司
  • 旅游网站建设课程设计报告内部优惠券网站建站
  • 郑州网站优化汉狮网络苏州互联网公司集中在哪里
  • 烟台网站建设精通臻动传媒ui培训设计学校
  • 网站推荐你了解我意思吧深圳响应式设计企业网站
  • 网站后期增加产品东平网页设计