Simple introduction to Spring framework (1)

introduce

    The Spring framework is a lightweight JAVA EE framework. The so-called lightweight means that it can run without relying on the container. Struts and Hibernate are also lightweight.

    Lightweight frameworks are relative to heavyweight frameworks. Heavyweight frameworks must rely on specific containers. For example, EJB frameworks must run in Glassfish, JBOSS and other containers that support EJB, but cannot run in TOMCAT.

    SPring takes IOC and AOP as the main ideas, among which IOC, inversion of controller refers to inversion of control or reverse control. In the Spring framework, we create class objects through configuration, and Spring instantiates and assembles objects during the runtime. AOP Aspect Oriented Programming, aspect-oriented programming, its ideological history executes other codes before executing some codes, making the program more flexible and scalable, and you can add or delete some functions at will. Filter in Servlet is a The realization of an AOP idea.

    Spring is also a "one-stop" framework, that is, in the three-tier framework of JAVAEE (presentation layer, business logic layer, and data access layer), Spring provides different solutions for each layer. as follows:

    Presentation layer: Spring MVC

    Business logic layer: IOC

    Data access layer: jdbcTemplate

Getting started with IOCs:

    1. Import the relevant jar packages in the Spring framework, here only import the jar packages under the Spring Core module (the Core module is the core class library of the framework), and the commons-logging and log4j jar packages that support log output.

    2. Create an ordinary java class, and create a method in this class as follows:

    User.java

public class User{
   public void add(){
       System.out.println("add...");
   }
}

    3. Create a Spring configuration file and configure the Bean

    The name and location of Spring's core configuration file is not fixed, but the official recommendation is to place the core configuration file in the src directory and name it applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/heans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframeword.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.sxd">
    <bean id="user" class="com.newcode.ioc.User"></bean>
</beans>

    4. Write test classes for testing, and create class objects through configuration files

    testIOC.java

public class TestIOC{
  public void testUser(){
     ApplicationContext context = new ClasspathXmlApplicationContext("applicationContext.xml");
     User user = (User)context.getBean("user");
     user.add();
  }
}

Bean instantiation method

    In Spring, objects are created through configuration files, and Bean is instantiated in three ways:

    1. Use the no-argument construction of the class to create, such as:

<bean id="user" class="com.newcode.ioc.User"></bean>

    2. Use static factory to create

    If a Bean cannot be instantiated directly through new, but is created through a static method of the factory, the class attribute of <bean> needs to be configured as a pseudo-factory class, such as:

<bean id="user" class="com.newcode.ioc.UserFactory" factory-method="createInstance"></bean>

    3. Use the instance factory to create

    If a bean cannot be instantiated directly through new, but is created through an instance method of the factory class, you need to configure the <bean> tag of the factory first, and then configure the factory-bean attribute of the bean tag of the object to be created as the factory class Object, the factory-method attribute is configured as the method of producing the instance, such as:

<bean id="userFactory" class="com.newcode.ioc.UserFactory"></bean>
<bean  id="user" factory-bean="userFactory" factory-method="createInstance"></bean> 

    Common properties of Bean tags

    1. The id attribute is used to specify the name of the configuration object and cannot contain special symbols

    2. class attribute, the full path of the class where the object is created

    3. The name attribute has the same function as the id attribute, but the name attribute can contain special symbols

    4. scope property

        (1) singleton: default value, in singleton mode, there is only one instance in the program. In non-singleton mode, every time the bean is requested, a new object will be generated

        (2) prototype: many cases

        (3) request: After creating the object, store the object in the request field

        (4) session: After creating the object, store the object in the session domain

        (5) globalSession: After creating the object, store the object in the globalSession domain

Property injection:

    Property injection refers to setting property values ​​​​to properties of class objects when creating objects.

    In the Spring framework, set method injection and parametric constructor injection are supported, that is, after the object is created, set properties through set or use a parametric constructor to create and initialize the object

Inject properties using a parameterized constructor

    Demo1.java provides a parameterized construction method

public class Demo1{
  private String demoName;
  public Demo1(String demoName){
    this.demoName = demoName;
  }
  public void out(){
    System.out.println(demoName);
  }
}

    bean configuration

<bean id="demo1" class="com.newcode.Demo1">
   <constructor-arg name="demoName" value="demo1"></constructor-arg>
</bean>

    Inject properties using the set method

        case:

        Demo2.java provides the set method of the property

        

public class Demo2{
  private String demoName;
  public void setDemoName(String demoname){
    this.demoName = demoName;
  }
  public void out(){
    System.out.println(demoName);
  }
}

    bean configuration

<bean id="demo2" class="com.newcode.ioc.Demo2">
    <property name="demoName" value="Demo2"></property>
</bean> 

    p namespace injection attribute

        I mentioned a property injection method of the set method before, and here I will introduce another method of property injection, called p namespace injection

<bean id="demo2" class="com.nwcode.ioc.Demo2" p:demoname="demo2"></bean>


    

Guess you like

Origin blog.csdn.net/JiayaoXu/article/details/80432987