spring framework study 1: up test

What 1.spring that?

If you write a small project, we can project one of you want to create an object to create an object, the direct use of the new method, creating an object, but for large projects, it may need to rely on hundreds of classes, with class relationships between classes is also very complicated, so we will not be able to create objects and maintain relationships between objects into the project, life can take the form of division of labor, the creation and maintenance of object-relational object to the spin-off, move a factory to do these things.

Spring is a core function of the factory model, the factory responsible for creating objects and relationships between objects maintain the image, it is the steward of the object.

The core idea of ​​the Spring Framework, Inversion of Control (IOC) and aspect-oriented (AOP).

Let's use a simple spring framework to create objects.

Be imported jar package:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.18.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!--springIOC和di的jar包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
spring core jar package

Simply create an object:

package com.zs.entity;

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Create a spring configuration file applactionContect placed in the resources folder:

<?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">

    <bean id="student" class="com.zs.entity.Student"/>

</beans>

Then create a test class to test whether an object can be created:

public class SpringTest {
    @Test
    public void test1(){
//        创建容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object student = context.getBean("student");
        System.out.println(student);
    }
}

Create objects success.

 

Guess you like

Origin www.cnblogs.com/Zs-book1/p/10994981.html