This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

Reviewing dependency Injection

  • For example, if you have two components A and B, A depends on B, let’s say A is A class, and A has A method that uses B for Hello
public class A {
    public void Hello(a) { B b = .... ;// get an instance of Bb.say(); }}Copy the code
  • To use B, A has to get an instance reference to B, which is fine if B is A classnewI can create an instance of B
  • If B is an interface and there are many implementations, then A is much less reusable

The practice of dependency injection

  • Dependency injection takes over the creation of objects (recall our previous section on proxy classes).
    • We can use the set method or other build method for injection
    • Look at the code
public class A {
    public void Hello(a) {
      // We don't need an instance of B at this point
        b.say();
    }
    public void setB(B b) {
        this.b = b; }}Copy the code
  • We added A new set method to inject an instance of B when called by the framework, so we don’t have to rely on A’s Hello method to call B’ssay()Method to create an instance of B

MVC

  • Model View Controller (MVC)
    • Views are responsible for displaying applications
    • The model encapsulates the application’s data and business logic
    • The controller is responsible for receiving input from the user

Simple request flow

  1. We tell the controller to execute the corresponding action via the URI
  2. The controller parses the URI and invokes the appropriate action
  3. Place the model formation in an area accessible to the view (in a layman’s sense, display the data on the browser)
  4. Controller using RequestDispatcher/HttpServlet jump to view

Common properties/annotations in springMVC

RequestMapping

  • This annotation maps a request to a method, and if a method uses this annotation, it becomes a request-handling method
    • And is called by the scheduler when it receives the corresponding URL request
    • A POST/GET request can also be indicated in its annotation parentheses
  • RequestParam – Used to get request parameter values
  • Param – Used to get the request parameter value
  • ApplicationScope – used to get the value of an applicationScope variable