Spring (a) - to create objects with Spring IOC container

1. Spring Profile

As a framework for Java EE development can be achieved implantation, the object of transaction management, aspect oriented programming, and many other functions.

Is like a martial arts champion, has its own ecosystem, status is very high, we all must be proficient grasp it.

2. Create a Spring project

Simple way : with already configured, copy, do not have to manually create a step by step
as the project to copy from mavendemo
find the location of mavendemo
Here Insert Picture Description
copy mavendemo project, and modify project name
Here Insert Picture Description

Here Insert Picture Description
Click into the project springdemo
Here Insert Picture Description
modify pom.xml file
Here Insert Picture Description
into the idea of operating
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
the next deployment jetty server
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
test access results
Here Insert Picture Description
so far, a springdemo project will create success!

According to requirements, it can be imported into a different workspace
to create a workspace spring_course in the D drive, and just create a good project springdemo copy this workspace
Here Insert Picture Description
Here Insert Picture Description
open idea
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
after a new workspace is created, import springdemo project
Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
So far, springdemo projects under the new space has been created!

Also you need to add a dependency in dependencies pom.xml file: spring-context and spring-core
Here Insert Picture Description

3. Spring IOC to create objects

A method to create objects in Java
(. 1) new new
(2) clone: Entity class implements Cloneable interface override clone () method, and rights to public
(. 3) reflector
(4) deserialize objects: entity class implements Serializable

In addition to the above four, may be used to create Spring IOC container
(5) into a container using a spring ioc object. Do not use new, but also by resolving internal reflection + xml document to achieve

Code tests (to achieve the above five methods):

  • First, create an entity class Student in java folder
package com.zz.domain;

import java.io.Serializable;

public class Student implements Cloneable, Serializable {
    private int id;
    private String name;
    private String gender;
    private String birthday;
    private String major;

    public Student() {
    }

    public Student(int id, String name, String gender, String birthday, String major) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.birthday = birthday;
        this.major = major;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", birthday='" + birthday + '\'' +
                ", major='" + major + '\'' +
                '}';
    }
}
  • Create a test class IocDemo (pre-test four kinds) in the java folder
package com.zz.test;

import com.zz.domain.Student;

import java.io.*;
import java.lang.reflect.Constructor;

public class IocDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("dest.data");
        //new
        Student s1 = new Student(01,"刘一","男","1998-05-11","通信");
        System.out.println(s1);

        //clone()
        Student s2 = (Student) s1.clone();
        System.out.println(s2);

        //反射
        Class aClass = s1.getClass();  //创建类对象
        //反射构造方法,返回构造方法对象
        Constructor constructor = aClass.getConstructor(int.class, String.class, String.class, String.class, String.class);
        Student s3 = (Student) constructor.newInstance(new Object[]{02, "王二", "男", "1999-12-01", "英语"});
        System.out.println(s3);

        //对象反序列化
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
        out.writeObject(s1); //把刘一对象写入到文件中。

        ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
        Student s4 = (Student) in.readObject();
        System.out.println(s4);
    }
}
  • operation result
    Here Insert Picture Description

Next Spring created by the fifth method:

  • First, create a configuration file applicationContext.xml in rescourse folder and define a student objects inside
    Here Insert Picture Description
<?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="s1" class="com.zz.domain.Student">
        <property name="id" value="088"/>
        <property name="name" value="王思聪"/>
        <property name="gender" value="男"/>
        <property name="birthday" value="1988-07-18"/>
        <property name="major" value="谈恋爱"/>
    </bean>
</beans>
  • Then create a test class in the java folder IocDemo02
package com.zz.test;

import com.zz.domain.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IocDemo02 {
    public static void main(String[] args) {
        //使用了spring ioc 容器注入了一个对象。没有使用new.内部也是通过反射+xml文档的解析来实现的。
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student s1 = (Student) context.getBean("s1");
        System.out.println(s1);
    }
}
  • operation result
    Here Insert Picture Description

Spring IOC execution order:

  ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  Student s1 = (Student) context.getBean("s1");

(1) First of all resources loading into the applicationContext.xml document
(2) parsing the document
(3) getBean ( 's1' ), s1 parsed from the document to the object. Using java reflection mechanism, reflecting an object and returned

Simple one: the principle of internal Spring IOC achieve is xml parsing + reflection
Note: initialization properties by calling setXXX to achieve the objects created by calling the default constructor with no arguments to achieve, then the requirements must retain the default no-argument constructor, or spring ico container can not instantiate the object.

IOC summed object initialization step of:
(1) loading the applicationContext.xml
(2) corresponding to the parsed object id
(3) reflected by calling the default constructor with no arguments, instantiate an object
(4) by calling the setXXX, initialization properties

Published 62 original articles · won praise 2 · Views 2723

Guess you like

Origin blog.csdn.net/nzzynl95_/article/details/104327669