Review
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 <BeansXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3 xmlns= "Http://www.springframework.org/schema/beans"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">6 <BeanID= "Person"class= "Com.test.bean.Person">7 < Propertyname= "Name"value= "Xingoo"/>8 < Propertyname= "Age"value= " a"/>9 </Bean>Ten </Beans>
3 Person class, where the set injection method is used
PackageCom.test.bean; Public classPerson {PrivateString name; Private intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } Public voidinfo () {System.out.println ("Let's eat spicy soup together!" "); System.out.println ("Name:" +getname () + "Age:" +getage ()); }}
4 Test class
Packagetestspring;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.test.bean.Person; Public classTest { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("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
"Java Development Series"--spring simple Getting Started example