The next summary of the first Spring lesson

         IoC and DI are the basis of Spring. IoC is the abbreviation of Inversion of Control, which is translated as "control inversion", and others are translated as "control inversion" or "control inversion".

        Spring manages through the IOC container and gives the control permission of creating objects to the IOC container.

The first Spring program

  Implementation steps:

1. In the dependent package, through the address“ http://repo.spring.io/simple/libs-release-local/org/springframework/spring/4.3.6.RELEASE/ ”Download the framework. The compressed package name is spring-framework-4.3.6.RELEASE-dist.zip.

         JAR packages in the libs directory are divided into three categories:

  1. The compressed package of the Spring framework class file ends with RELEASE.jar.
  2. The compressed package of Spring framework API documents ends with RELEASE-javadoc.jar.
  3. The compressed package of the Spring framework source file ends with RELEASE-sources.jar.

In the libs directory, there are four Spring basic packages, which correspond to the four modules of the Spring core container:

Spring 4 basic packages
Package nameexplain
spring-core-4.3.6.RELEASE.jarIt contains the basic core tool classes of the Spring framework. Other Spring components need to use the classes in this package
spring-beans-4.3.6.RELEASE.jarJAR packages used by all applications include all classes related to accessing configuration files, creating and managing beans, and performing IoC or DI operations
spring-context-4.3.6.RELEASE.jarSpring provides extension services on basic IoC functions and supports many enterprise level services, such as task scheduling, JNDI positioning, EJB integration, remote access, caching, mail services and encapsulation of various view layer frameworks
spring-expression-4.3.6.RELEASE.jarDefines the expression language of Spring

2. Prepare configuration file

        Write the Spring configuration file and create the src file spring-config.xml in the root path of the project. Create an instance of the hellosppring class in the Spring configuration file and inject attribute values into the hello attribute in the class.

<!--    bean In, properties id The value of must be unique, property class The value of is the full class name of the class-->
    <bean id="helloWorld" class="bdqn.bean.HelloWorld">
<!--        Through label property Reflection mechanism call class HelloWorld of seter Method and assign a value to the parameter-->
        <property name="message">
            <value>Watermelon is delicious(#^.^#)</value>
        </property>
    </bean>

  3. Write test code

        The key code of attribute injection through Spring is as follows:

 public static void main(String[] args) {
        //Load the xml file, read the JavaBean, and obtain the instance object of the class through reflection
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
        //Get the HelloWorld instance object of the specified class through the id of the bean
        HelloWorld helloWorld= (HelloWorld) context.getBean("helloWorld");
        System.out.println(helloWorld.getMessage());
        HelloServiceImpl helloService = (HelloServiceImpl) context.getBean("helloService");
        helloService.addMessage();
        logger.info("log:info  "+helloWorld.getMessage());
    }

How Spring initializes the running environment:

 //ClassPathXmlApplicationContext reads the configuration file in the src directory
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
        // FileSystemXmlApplicationContext is the path of system files and the directory of files.
        // (Note: if your xml file is placed in the WEB-INF folder, you need to use this, otherwise the file will not be found)
        ApplicationContext context2=new FileSystemXmlApplicationContext("D:spring-config.xml");

Get the instance object by id or class

HelloWorld helloWorld= (HelloWorld) context.getBean("helloWorld");
        System.out.println(helloWorld.getMessage());

Through the first Spring program, we have a basic understanding of Spring, realized the creation of an instance of a Java class through Spring, and assigned its attributes in the way of "injection". Then, if the attribute type of the Java class is JavaBean, such as UserDao in UserService, how to implement it? Continue to explain in detail in the next class. (#^.^#)

Keywords: Java Spring

Added by tsinka on Fri, 17 Sep 2021 06:55:20 +0300