Getting Started with Spring

Configure pom.xml, which is the guide package

<dependencies>
        <!-- Spring related dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <!-- The spring web module provides a listener to start the spring container -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
    </dependencies>

2. Create a container, that is, the middleman between classes and classes, rely on him to maintain the relationship between the two

<?xml version="1.0" encoding="UTF-8"?>
<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">


    <!--beans.xml file, you can think of it as a spring container:
        The bean tag configures the class A, which means that the class A is handed over to the Spring container for management.
        The creation of class A is created by the container, and new can no longer be used
    -->
    <bean id="aId" class="com.xingxue.spring.beans.A"/>

    <bean id="bId" class="com.xingxue.spring.beans.B"/>

</beans>

Three write two classes

package com.xingxue.spring.beans;
public class A {

    // Created by the spring container, the default is to use the class's no-argument constructor to create
    public A(){
        System.out.println("Class A was created.");
    }


    public void  m1(){
        System.out.println("hello,spring!");
    }

}


package com.xingxue.spring.beans;

public class B {
    public B (){
        System.out.println("Class B was created.");
    }

    public void m2(){
        System.out.println("The m2 method in class b is executed..");
    }
}

Four test the relationship between the two

package com.xingxue.spring.test;

import com.xingxue.spring.beans.A;
import com.xingxue.spring.beans.B;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    public static void main(String[] args) {
        // 1. Start the spring container and create the container
       ApplicationContext  cxt =  new ClassPathXmlApplicationContext("beans.xml");
        // 2. Go to the container to get the bean object
        A a = (A) cxt.getBean("aId");

        a.m1();

        B bId = (B) cxt.getBean("bId");

        bId.m2();
    }
}
 If there are modifications such as cannot start process during execution, select run>>Edit congrriton>>$MODULE_DIR$, Apply--Ok

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688500&siteId=291194637