Spring study concluded (2) - automatic assembly

  Mentioned above, the IOC is injected where needed to provide two dependencies, the definition of class one, the second is needed to describe the spring configuration. Put the second automatic assembly removed, i.e. we rely only necessary to provide in the class, then the management objects to the container to complete the injection. In the actual development, dependencies between classes is usually described at great length, if you save a lot of automated assembly configuration, and if the dependent object is updated, we do not need to update the configuration, but also brought some Shortcomings.

  It means you can declare the bean, as for the relationship between objects that reference each other to get by the Spring itself. .

  Use: no (not enabled), byName (by name), byType (depending on the type), constructor (the constructor)

<beans ....  
     default-autowire="byType">

</beans>

  Advantage of automatic assembly reference document: https: //docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-autowire

  Shortcoming reference documentation: https: //docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-autowired-exceptions

  byType assembly process: Spring first step will have all bean are loaded into the container, found in the second step has a dao Analytical Service dependent, will find their dependence property is the class or superclass is the Dao Dao implanting class attributes.

  If there is the same interface implementation class 2, and also using it will interface injection being given, it is necessary to byName embodiment, or injection according to the implementation class.

  Also, in the bean can specify an individual tag is assembled manner:

<bean id="ClassOne" class="****" autowired="byType">

 

injection mode annotation automatic assembly of:

  Use annotations @Autowired default is transferred with a way to use that byType, if not found will use byName, has not been found to be assembled like an error.

  @Resource byName default annotation, according to confirm the attribute name, can be specified by the name of a specific type, such as @Resource (type = "IndexImpl")  

 

 

 

spring lazy loading

  Increase in the Spring configuration file the following statement to indicate Spring is not all injected at the start of the project, but will be injected in use, default false

<beans .....     
  default-lazy-init="true"> </beans>

 

SpringBean scope

  singleton: Singleton

  prototype: standard, every time a new object

  request: defining each Http request is a new object

  session: The defined range is defined as an individual bean HTTP session lifecycle. Spring ApplicationContext only in the context of the web effectively perceptible.

  applicaiton: The defined range is defined as an individual bean ServletContext lifecycle. Spring ApplicationContext only in the context of the web effectively perceptible.

  websocket: The range is defined as an individual bean WebSocket defined lifecycle. Spring ApplicationContext only in the context of the web effectively perceptible.

 

Guess you like

Origin www.cnblogs.com/huanshilang/p/11618673.html