Spring 核心技术(5)

  • 2019 年 10 月 3 日
  • 筆記

????Spring ?????4?

version 5.1.8.RELEASE

1.4.5 ???????

Spring ?????????? bean ?????????? Spring ???? ApplicationContext ??????? bean ????????bean?????????????

  • ?????????????????????????(????????????????bean?? ???????????)
  • ???????????????????????????????????????????????????????????????????????????????????????????????????

???? XML ????????????????????? <bean/> ??? autowire ??? bean ?????????????????????????? bean ??????????????????????????????????

?? ??
no ??????????Bean ????? ref ?????????????????????????????????????????????????????????????????
byName ??????????Spring ??????????????? bean?????? bean ??????????????????????? master ????????? setMaster(..) ????? Spring ????? master ? bean ????????????
byType ???????????????? bean??????????????????????????????????? bean ?? byType ???????????? bean???????????????????
constructor ??? `byType ????????????????????????????? bean??????????

?? byType ? constructor ????????????????????????????????????????????????????????????????????? String??????????? Map ??????? Map ??????????????? bean ????? Map ????????? bean ???

??????????

???????????????????????????????????????????????????????? bean ????????????

?????????????

  • property ? constructor-arg ????????????????????????????????????Strings?? Classes???????????????????????
  • ????????????????????????? Spring ??????????????????????????????Spring ?????????????????
  • ? Spring ???????????????????????
  • ?????? bean ?????? setter ?????????????????????????????? Map ???????????????????????????????????????????????????? bean ?????????

???????????????

  • ???????????????
  • ??????????? autowire-candidate ????? false??????bean?????????
  • ???? <bean/> ??? primary ????? true???? bean ???????????
  • ????????????????????????????????

???????? Bean

???? bean???????????? bean?? Spring ? XML ????? <bean/> ??? autowire-candidate ????? false??????? bean ???????????????????? @Autowired??

autowire-candidate ????????????????????????????????????? bean ???????????????????????????????????????? bean?

?????? bean ?????????????????????? <beans/> ??? default-autowire-candidates ????????????? ???????????????????? Repository ????? bean??????? *Repository???????????????????????????bean ??? autowire-candidate ???????????? true ? false????? bean?????????????

????????????????????? bean ? bean ?????????????? bean ??????????????????bean ?????? bean ?????????

1.4.6 ????

??????????????????? bean ???????? bean ???????? bean ?????? bean ????????? bean ??????????? bean ?????? bean ???????????? bean ?????????????????? bean A ???????????bean B????? A ??????????????????? bean A???????????????????? bean A ??????????? bean B ????

?????????????????????? ApplicationContextAware ??? bean A ??????????????? getBean("B") ???????? bean B ???????????????

// a class that uses a stateful Command-style class to perform some processing  package fiona.apple;    // Spring-API imports  import org.springframework.beans.BeansException;  import org.springframework.context.ApplicationContext;  import org.springframework.context.ApplicationContextAware;    public class CommandManager implements ApplicationContextAware {        private ApplicationContext applicationContext;        public Object 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.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;      }  }

???????????????????????? Spring Framework??????Spring IoC????????????????????????

????????????????????????

??????

???????????????? bean ???????????????? bean ?????????????????? bean????????????Spring Framework ???? CGLIB ??????????????????????????????

  • ???????????Spring bean ????????? final????????????? final?
  • ??? abstract ?????????????????????????? abstract ??? stub ???
  • ????????????????????????
  • ???????????????????????? @Bean ?????????????????????????????????????????????

?????????? CommandManager ??Spring ??????? createCommand() ??????? CommandManager ????? Spring ?????????????????

package fiona.apple;    // 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);

????? abstract????????????????????????????????????????????????

<!-- a stateful bean deployed as a prototype (non-singleton) -->  <bean id="myCommand" class="fiona.apple.AsyncCommand" scope="prototype">      <!-- inject dependencies here as required -->  </bean>    <!-- commandProcessor uses statefulCommandHelper -->  <bean id="commandManager" class="fiona.apple.CommandManager">      <lookup-method name="createCommand" bean="myCommand"/>  </bean>

???? myCommand bean ???????? commandManager ? bean ??????? createCommand() ??????????????? myCommand bean ??????????????????????? myCommand bean ???

??????????????????? @Lookup ?????????????????

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:

public abstract class CommandManager {        public Object process(Object commandState) {          MyCommand command = createCommand();          command.setState(commandState);          return command.execute();      }        @Lookup      protected abstract MyCommand createCommand();  }

????????????? stub ????????????????????? Spring ???????????????????????????????????????? bean ??

?????????? bean ??????? ObjectFactory/Provider ????????? Scoped Beans ???????

??????? ServiceLocatorFactoryBean?? org.springframework.beans.factory.config ??????

??????

??????????????????????????????????????? bean ?????????????????????????????????

???? XML ???????????? replaced-method ?????????????????? bean?????????????????? computeValue ??:

public class MyValueCalculator {        public String computeValue(String input) {          // some real code...      }        // some other methods...  }

?? org.springframework.beans.factory.support.MethodReplacer ????????????????????:

/**   * 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 id="myValueCalculator" class="x.y.z.MyValueCalculator">      <!-- arbitrary method replacement -->      <replaced-method name="computeValue" replacer="replacementComputeValue">          <arg-type>String</arg-type>      </replaced-method>  </bean>    <bean id="replacementComputeValue" class="a.b.c.ReplacementComputeValue"/>

?????????? <replaced-method/> ??? <arg-type/> ???????????????????????????????????????????????????????????????????????????????????? java.lang.String?

java.lang.String  String  Str

???????????????????????????????????????????????????????????