"Java Development Series"--spring simple Getting Started example

Source: Internet
Author: User
Tags aop

1 JDK Installation

2 Struts2 Simple Getting Started example

Objective

As entry-level record posts, not too much technical content, simple configuration framework. This time about spring, this should be the heavyweight framework in SSH, which consists of two main things: control inversion \ Dependency injection, and AOP aspect-oriented programming.

1 Control inversion ioc\ Dependency Injection di, because the translation is different, so there are two names.

  inversion of control means that when we call a method or class, we no longer have the initiative to create the object of this class, and control is given to others (spring).

  Dependency Injection means that spring actively creates the object of the called class, and then injects the object into our own class so that we can use it.

  

To give a simple example, the program ape work overtime for one months, very tired, want to relax, so go find someone to eat "spicy soup."

The traditional way of not using spring is that we ourselves through the artifact such as Momo, actively looking for goals, spending a lot of manpower and material resources, to reach an agreement, apply for "place" to do business.

And the use of spring is very convenient, we go directly to a place, that place directly has a target to wait!

What a magical feature it is!

(This figure deals with violence and is self-responsible under the age of 18)

2 AOP-oriented tangent programming

We think of a method as a section, in front of or around the plane, can be set up other processing methods, to do some special processing.

For example, a "Eat cake" method, before this method needs to "unpack", the method after the need to "clean", can be achieved through this programming method.

Then look at the coding process below!

  1 First we need a bean, in some way (set injection requires a setter function, construction injection needs to have a constructor, the corresponding Bean.xml configuration file will be different) to set the injection mode.

2 Bean.xml, sets the relationship of the bean to the class and associates the default injected value.

3 Gets the Bean.xml file, creates an instance object, and calls the method directly.

As you can see, we develop a program that only has the third step of "create instance object, call method directly", and does not initialize for him, and so on, can call its method directly, get its value. In fact, when the program is initialized, spring will inject the corresponding object into the bean and help us initialize it, so we can call it directly through a reference object.

Finally, we write a simple sample of dependency injection!

1 Required jar packages and file structure

Jar Package: Http://pan.baidu.com/s/1sjDbzrR

  

2 bean.xml configuration file, placed in 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= "/> 9     </bean>10 </beans>

3 Person class, where the set injection method is used

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.) {        this.age = age;    }    public void info () {        System.out.println ("to eat spicy soup together!") ");        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 ");//Read the contents of the bean.xml person        p = ctx.getbean (" person ", person.class);//Create a Reference object for the Bean        P.info ();}    }

5 Execution Results

From:http://www.cnblogs.com/xing901022/p/3963962.html

"Java Development Series"--spring simple Getting Started example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.