Simple things-springmvc simple knowledge of injecting beans

Source: Internet
Author: User

Recently refactoring project code, in the process of collating application code based on SPRINGMVC technology, the principle of springmvc annotation is simply verified. Typically, our custom class uses SPRINGMVC annotations and does not make any special settings in the Spring-servlet.xml configuration, what about the bean instantiation process?

1 single case

By default, the same type of member variables are injected with @autowire, the same object is referenced, and the injection of that member variable is not set by invoking the Setxx method of the class. For example, having service annotations and referencing two classes of the same type, verifying that their reference members are the same instance (= = operation is true), the source code is as follows:

/** * * @title: Aeventhandler * @description: Top-level abstraction of the event-handling class * It relies on an observer handlerdispatcher and registers to add events when constructed * @updat e:2015-1-15 pm 1:19:05 * @author: Wang_ll * @version: 1.0.0 * @since: 2015-1-15 */@Servicepublic ABSTR Act class Aeventhandler implements ieventhandler{@Autowiredprivate Handlerdispatcher requesthandler;@ postconstructpublic void Initrequesthandler () {if (this.requesthandler!=null) {This.requestHandler.addEventHandler ( this); System.out.println ("Add msgservice OK");} Else{system.out.println ("Add Msgservice Bad");}} public void Setrequesthandler (Handlerdispatcher requesthandler) {This.requesthandler = Requesthandler;if ( This.requesthandler!=null) {This.requestHandler.addEventHandler (this);}} Public Handlerdispatcher Getrequesthandler () {return requesthandler;}}  /** * * @title: Amsghandler * @description: Top-level abstract class for message processing class * @update: 2015-1-15 pm 1:22:26 * @author: Wang_ll * @version: 1.0.0 * @since: 2015-1-15 */public abstract class AmsghaNdler implements imsghandler{@Autowiredprivate Handlerdispatcher RequestHandler; @PostConstruct public void Initrequesthandler () {if (this.requesthandler!=null) {This.requestHandler.addMsgHandler (this); System.out.println ("Add msgservice OK");} Else{system.out.println ("Add Msgservice Bad");}} Public Handlerdispatcher Getrequesthandler () {return requesthandler;} public void Setrequesthandler (Handlerdispatcher requesthandler) {This.requesthandler = Requesthandler;if ( This.requesthandler!=null) {This.requestHandler.addMsgHandler (this); System.out.println ("Add msgservice OK");} Else{system.out.println ("Add Msgservice Bad");}}}

Project startup results show that the member RequestHandler of these two classes is a reference to the same object, and that the initialization process for this member is not done through the Setrequesthandler () method. Annotation @postconstruct identifies a method that is executed when the entire class is instantiated, including the initialization of the member variable. When a member variable RequestHandler is referenced in the Initrequesthandler method, it is already instantiated, so it enters a non-empty branch. This annotation is very good for operations that depend on member variables.

2 Scope of annotations

The bean defined in spring's configuration file Applicationcontext.xml is loaded immediately by default, and when the spring container is started, all beans in the configuration file are instantiated, including the annotation class in the component scan package, and the annotation class in the SPRINGMVC configuration file only instantiates AC tion is the class that the controller annotations and its dependent classes. If you define a spring configuration file, Applicationcontext.xml contains the following scan configurations:

<beans><context:annotation-config/><context:component-scan base-package= "Com.wood.controller"/ ><context:component-scan base-package= "Com.wood.dao"/><context:component-scan base-package= " Com.bh.wexin.service "/><context:component-scan base-package=" com.bh.wexin.action "/>        <import Resource= "Dbconfig.xml"/>    </beans>

When the container starts, all the annotated classes are instantiated in the package that is being scanned. However, if the scan configuration described above is defined in the SPRINGMVC configuration file Spring-servlet.xml, when the container starts, only the action class is initialized and loaded into memory (that is, the @controller annotated class and its dependent class are instantiated).

This indicates that the scope of the SPRINGMVC annotation scan is not the same as that of the instantiated class, and the scope of the scan package in the SPRINGMVC configuration file is controlled in the project development to avoid unnecessary classes being initialized.

3 Simple knowledge of inheritance

When a subclass overrides a method of a parent class, the method signature cannot be changed, including access permission identifiers, return values, parameters, and so on, but the final modifier can be added so that some methods are no longer overridden by their subclasses. Simple example: If a defines method public void a (), and b inherits a, and overriding method A is public final void a (), this semantics is legal. In this case, the subclass of B cannot rewrite the method.

Simple things-springmvc simple knowledge of injecting beans

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.