6.2 Java EE - Spring's starter program

The following demonstrates the use of the Spring framework through a simple entry program. It is required to print "Zhang San, welcome to Spring" on the console. The implementation steps are as follows.

1. Create a Maven project named chapter06 in IDEA, and then load the four basic Spring packages and Spring dependency packages to be used in the pom.xml file.

<dependencies>
        <!--Spring的基础包Spring-core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--Spring的基础包Spring-beans-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--Spring的基础包Spring-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--Spring的基础包Spring-expressinon-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--Spring的依赖包commons-logging-->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

2. Create a class named HelloSpring, and define the userName attribute and show() method in the HelloSpring class.

package com.mac;

public class HelloSpring {

    private String userName;

    public void setUserName(String userName){

        this.userName=userName; }

    public void show() { 

        System.out.println(userName+":欢迎来到Spring"); }

}

3. Create a new applicationContext.xml file as the configuration file of the HelloSpring class, and create a Bean with the id helloSpring in the configuration file.

<!-- 将指定类配置给Spring,让Spring创建HelloSpring

对象的实例 -->

<bean id="helloSpring" class="com.mac.HelloSpring">

        <!--为userName属性赋值-->

        <property name="userName" value="张">

        </property>

</bean>

Constraint information configuration of XML file

The XML file contains a lot of constraint information. If you write it yourself, it will not only waste time, but also be error-prone. The XML constraint information is as follows.

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

In fact, you can find these constraint information in Spring's help documentation.

Spring help documentation to obtain constraint information

        Open the docs folder under the Spring directory structure, and find the index.html file in the Spring reference file directory of the spring-framework-reference folder.

​​​​​​​

 

Open index.html with your browser.

        Click the "Core" link in step 2 to enter the Core Technologies page, and click 1.The IoC container→1.2.Container overview→1.2.1.Configuration Metadata directory to view the constraint information of the configuration file.

        Create a test class TestHelloSpring, initialize the Spring container in the main() method and load the applicationContext.xml configuration file, obtain the helloSpring instance of the HelloSpring class through the Spring container, and call the show() method in the HelloSpring class to output information on the console.

public class TestHelloSpring {

    public static void main(String[] args){

        // 初始化spring容器,加载applicationContext.xml配置

        ApplicationContext applicationContext=new 

     ClassPathXmlApplicationContext("applicationContext.xml");

        // 通过容器获取配置中helloSpring的实例

        HelloSpring helloSpring=

        (HelloSpring)applicationContext.getBean("helloSpring");

        helloSpring.show();// 调用方法 }

}

Start the test class TestHelloSpring in IDEA, and the console will output the results

Guess you like

Origin blog.csdn.net/W_Fe5/article/details/131662817