[Java development series] - spring simple entry example

review

  1 the JDK installation

  2 Struts2 simple entry example

Foreword

  As the entry-level recording software, and not too much technical content, a simple configuration framework to build it. The mentioned spring, this should be the heavyweight SSH framework, which mainly consists of two elements: Inversion of Control \ dependency injection and AOP Aspect Oriented Programming.

  1 Inversion of Control IOC \ dependency injection DI, because of the different translation, so there are two names.

  Inversion of Control means is that when we call a method or class, we no longer have the initiative to create objects of this class, control over to someone else (spring).

  Dependency injection means is that, spring initiative to create an object is called class, and then injected into the object of our own class, so that we can use it.

  

  Here is a simple example, program ape overtime a month, tired, under want to relax, then went to eat "Spicy."

  Do not use traditional spring practice, through our own street street micro letters and other artifacts, take the initiative to find the target, spend a lot of manpower and material resources, following an agreement to apply "place" get down to business.

  The use of spring practice is very convenient, we go directly to a place, that place there is a direct target was waiting!

  What a fantastic features ah!

 

(This figure involving violence, 18 years old solely responsible)  

  2 AOP Oriented Programming

  We put a method is seen as a section, or in the front and rear section of the surrounding, you can set other treatment methods, some special handling.

  For example, a "cake" approach, need to "unpacking" Before this method, you need to "clean up", can be achieved by this method after programmatically.

 

  Then the following encoding process look it!

  1 First we need of a bean, by some method (injection requires setter function is provided, the need for injection constructor configured, the corresponding profiles are different bean.xml) injection mode is set.

  2 bean.xml, provided the relationship between the class of the bean, and the associated default value injection.

  3 get bean.xml file, create an instance of the object, call the method directly.

  You can see, we have developed the program "creates an instance of an object directly call the method" Only the third step, and did not initialize and other work for him, you can call its methods directly to get its value. In fact, in the spring when the program is initialized, it will for us to inject bean corresponding object come in and help us complete the initialization, so we just pass a reference to an object, you can directly call.

 

  Finally, we write a simple dependency injection of a small sample!

  1 jar package and file structure needed

  jar package Download: http: //pan.baidu.com/s/1sjDbzrR

  

  2 bean.xml configuration file, placed under the src directory

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 6     <bean id="person" class="com.test.bean.Person">
 7         <property name="name" value="xingoo"/>
 8         <property name="age" value="12"/>
 9     </bean>
10 </beans>

 

  3 Person class, provided herein is set injection mode

package com.test.bean;

public class Person {
    
    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;
    }
    public void info(){
        System.out.println("一起来吃麻辣烫!");
        System.out.println("name:"+getName()+" age:"+getAge());
    }
}

  4 test class

package testSpring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.bean.Person;

public class test {
    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");//读取bean.xml中的内容
        Person p = ctx.getBean("person",Person.class);//创建bean的引用对象
        p.info();
    }
}

  5 execution results

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545478

Guess you like

Origin blog.csdn.net/weixin_34007886/article/details/91989980