1 Spring Basic characteristics
Spring is a very active open source framework; It is a framework based on core to frame multi-tier Java EE Systems, and its main purpose is to simplify enterprise development.
Spring is a non-invasive way to manage your code, and spring advocates "minimal intrusion," which means you can install or uninstall spring,spring to make Java shine. (Open – closure principle), here is the principle of closure.
2 tools needed to develop spring
(spring2.5 first, back 3.0)
2.1 Spring's Jar pack
Download spring to http://www.springsource.org/download , then unzip it, locate the following jar file in the Extract directory, and copy it to the Classpath
-spring's core class library is dist under the spring document Dist\spring.jar
-The introduction of the third party class libraries are under the Lib of the spring document, Lib\jakarta-commons\commons-logging.jar
-If you are using slice programming (AOP), you will need the following jar files Lib/aspectj/aspectjweaver.jar and Aspectjrt.jarlib/cglib/cglib-nodep-2.1_3.jar
-If you use annotations in JSR-250, such as @resource/@PostConstruct/@PreDestroy, you will need the following jar files Lib\j2ee\common-annotations.jar
Note: The JSR (Java specification request) is a formal request to the JCP (Java Community Process) to propose a new standardized technical specification. Anyone can submit a JSR (Java spec request) to add new APIs and services to the Java platform. JSR has become an important standard in the Java World
2.2 Spring configuration file
By default, it is the Applicationcontext.xml file. You can build a lot of XML files, which are typically configured in engineering. (built under SRC Catalogue)
3 Spring Basic features detailed
3.1 SPRINGIOC
Spring's control reversal: the creation, initialization, and destruction of objects are entrusted to the spring container. The spring container controls the life cycle of the object.
Steps:
A. Start the spring container
1, in the classpath to find the configuration file to instantiate the container
Copy Code code as follows:
ApplicationContext CTX = new Classpathxmlapplicationcontext (new string[]{"Applicationcontext.xml"});
XML files can be found throughout the Classpath
* Loaded in this way. You need to place the spring configuration file under the Classpath path of the current project
* The classpath path refers to the SRC directory of the current project, which is where the Java source files are stored.
2, in the file system path to find the configuration file to instantiate the container
Spring's configuration file can specify multiple, which can be passed through a string array.
Note: The first method is often used to start the container
B. Extracting objects from the spring container
Spring Container structure:
3.2 Aliases
<beans>
<alias name= "person" alias= "P"/>//alias here is the alias, you can get the person this bean through p.
<bean name= "Person" class= "Cn.itcast.aliasspring.Person"/>
</beans>
This configuration can be achieved by naming in one place and using different names in multiple places.
3.3 Spring Container Internal objects
1 How objects are created
1.1 Non-parametric constructors
<bean id= "HelloWorld" class= "Com.itheima10.spring.createobject.HelloWorld" ></bean>
1.2 Static factory methods
<bean id= "HelloWorld2" class= "Com.itheima10.spring.createobject.method.HelloWorldFactory"
Factory-method= "GetInstance" ></bean>
1.3 Instance Factory method
<bean id= "Helloworldfactory" class= "Com.itheima10.spring.createobject.method.HelloWorldFactory2" ></bean >
<bean id= "helloWorld3" factory-bean= "Helloworldfactory" factory-method= "getinstance" ></bean>
Remember the concept, the most used is the first method, but when integrated with other tools, use the instance factory pattern.
Example :
Configure Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.5.xsd "> <!--put a class into the spring container, which is called a bean--> <!--Bean describes the full name of a class ID unique to the class--> <bean id= "HelloWorld class=" com.itheima10.spring.createobject.h
Elloworld "></bean> <!-----------------------alias--------------------------------------> <!-- The property of name and the ID of the bean match the alias alias--> <alias name= "HelloWorld" alias= "dog egg"/> <alias name= "HelloWorld" alias= "XI"/
> <!-----------------------static Factory method--------------------------------------> <!-- Put Helloworldfactory into the Spring container Factory-method factory method--> <bean id= "helloWorld2" factory-method= "getinstance" cl ass= "Com.itheima10.spring.createobject.method.HelloWorldFactory" ></BEAN>
<!-----------------------example Factory method--------------------------------------> <!-- Putting HelloWorldFactory2 into the spring container Factory-bean indicates that the factory bean Factory-method indicates the method in the factory bean--> <bean id= "Helloworldfa Ctory "class=" Com.itheima10.spring.createobject.method.HelloWorldFactory2 "></bean> <bean id="
HelloWorld3 "factory-bean=" Helloworldfactory "factory-method=" getinstance "></bean> </beans>
Establish entity class HelloWorld
Package com.itheima10.spring.createobject;
public class HelloWorld {public
void Hello () {
System.out.println ("Hello");
}
Establish static factory Helloworldfactory
Package Com.itheima10.spring.createobject.method;
public class Helloworldfactory {public
static HelloWorld getinstance () {
System.out.println (' static method ') ;
return new HelloWorld ();
}
Establish a solid factory HelloWorldFactory2
Package Com.itheima10.spring.createobject.method;
public class HelloWorldFactory2 {
/**
* The factory object must be created before the method can be invoked
* @return/public
HelloWorld GetInstance () {return
new HelloWorld ();
}
}
Writing test Methods Createobjectmethodtest
Package Com.itheima10.spring.createobject.method;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext; /** * Second and third generation methods you can see it. * @author ZD * * */public class Createobjectmethodtest {/** * By default, the spring container calls the default construct of a class Function Creation Object */@Test public void Testcreateobject_default () {ApplicationContext context = new Classpathxmlapplicationcon
Text ("Applicationcontext.xml");
HelloWorld HelloWorld = (HelloWorld) context.getbean ("HelloWorld");
Helloworld.hello (); /** * Use static factory to create Object * <bean id= "helloWorld2" factory-method= "getinstance" class= "Com.itheima10.spring.createobje" Ct.method.HelloWorldFactory "></bean> Spring containers do: Invoke the GetInstance method using the Helloworldfactory class */@Test public VO ID testcreateobject_staticfactory () {ApplicationContext context = new Classpathxmlapplicationcontext ("
Applicationcontext.xml "); HelloWorld HelloWorld = (HelloWorld) context.getbeaN ("HelloWorld2");
Helloworld.hello (); /** * Instance Factory method Creation Object * <bean id= "helloworldfactory" * class= "COM.ITHEIMA10.SPRING.CREATEOBJECT.METHOD.HELLOWORLDFAC" Tory2 "></bean> * <bean id=" helloWorld3 "factory-bean=" helloworldfactory "factory-method=" "getinstance" & gt;</bean> Spring containers do things internally: 1. Create a Helloworldfactory object 2, Invoke getinstance by the object HelloWorld object/@Test public void Testcreateobject_instancefactory () {ApplicationContext context = new Classpathxmlapplicationcontext ("
Applicationcontext.xml ");
HelloWorld HelloWorld = (HelloWorld) context.getbean ("helloWorld3");
Helloworld.hello ();
}
}
2 Scope of objects
The scope of an object has two properties: Singleton and prototype. Singleton Representation single case
2.1 Singleton (default value)
In each spring IOC container, a bean definition has only one object instance (shared).
2.2 Prototype
Allows a bean to be instantiated multiple times (create an instance once with one). Spring cannot be responsible for the entire lifecycle of a prototype bean. This means that it is the client's responsibility to clearly prototype the scope object and release any expensive resources held by the prototype Bean.
<bean id= "HelloWorld" class= "Com.itheima10.spring.scope.HelloWorld" scope= "Singleton" ></bean>
<bean id= "HelloWorld" class= "Com.itheima10.spring.scope.HelloWorld" scope= "prototype" ></bean>
Establish HelloWorld class
public class HelloWorld {public
list<string> lists = new arraylist<string> ();
Public HelloWorld () {
System.out.println ("new instance");
}
public void Hello () {
System.out.println ("Hello");
}
Establish Test class Scopetest
Package com.itheima10.spring.scope;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Scopetest {/** * puts a bean into the spring container, the default is a single example * If a class is put into spring and this class is a single example, then the properties in that class will be shared//@Test Publ IC void Testcreateobject_scope_default () {ApplicationContext context = new Classpathxmlapplicationcontext ("Applicatio
Ncontext.xml ");
HelloWorld hello1 = (HelloWorld) context.getbean ("HelloWorld");
Hello1.lists.add ("AAAA");
Hello2= (HelloWorld) Context.getbean ("HelloWorld");
Hello2.lists.add ("bbbb"); System.out.println (HelloWorld.lists.size ());//2, and output only once for new instance}/** * If the configuration file for spring is as follows: * <bean id= "Helloworl D "class=" "Com.itheima10.spring.scope.HelloWorld" scope= "prototype" ></bean> then the Spring container will/@Test pub for creating multiple objects LIC void Testcreateobject_scope_prototype () {ApplicationContext context = new Classpathxmlapplicationcontext ("Applica Tioncontext.xML ");
HelloWorld hello1 = (HelloWorld) context.getbean ("HelloWorld");
Hello1.lists.add ("AAAA");
Hello2= (HelloWorld) Context.getbean ("HelloWorld");
Hello2.lists.add ("bbbb");
System.out.println (HelloWorld.lists.size ());//1, and output only two Times new instance}}
3 Initializing Bean timing
Lazy Loading-The bean is initialized when the container is started by default, but we can specify lazy-init= "true" of the bean node to delay the initialization of the bean, at which point only the first fetch of the bean will initialize the bean. Such as:
<bean id= "xxx" class= "Cn.itcast.OrderServiceBean" lazy-init= "true"/>
If you want to apply lazy initialization to all beans, you can set default-lazy-init= "true" at the root node beans, as follows:
<beans default-lazy-init= "true" ...>
Spring defaults to the early instantiation of all singleton beans at startup. Early instantiation means that, as part of initialization, ApplicationContext automatically creates and configures all singleton beans. This is usually a good thing. Because there are any errors in the configuration that can be found immediately.
Lazy-init for false,spring The container will make an error at startup (a better way)
Lazy-init for the true,spring container will make an error when calling the class.
Configure Applicationcontext.xml
<bean id= "HelloWorld"
class= "Com.itheima10.spring.createobject.when.HelloWorld" scope= "prototype"
></bean>
</beans>
Establish Test class Createobjectwhentest
Package com.itheima10.spring.createobject.when;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Createobjectwhentest {/** * Default order * 1, start spring container * 2, create HelloWorld Object * 3, Object Invoke method * * <bean id= "h Elloworld "class=" Com.itheima10.spring.createobject.when.HelloWorld "></bean> <bean id=" HelloWorld2 " class= "Com.itheima10.spring.createobject.when.HelloWorld" ></bean> because two beans are declared in the spring container, So the spring container is going to create two objects description: If the Struts2,hibernate,spring container is consolidated, if there is an error in the spring's configuration file, the Tomcat container starts out with an error, and the bug will show up very early if a bean Store a lot of data, this way is not good, it is possible to put the data prematurely in memory if a bean is not a single case, then no matter how the configuration, it is Context.getbean to create the object * * @Test public void Testcreateobj
Ect_when_default () {ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Context.getbean ("HelloWorld"); }/** * <bean id= "HelloWorld" class= "COM.ITHEIMA10.SPRING.CR"Eateobject.when.HelloWorld "lazy-init=" true ></bean> * sequence * 1, start spring container * 2, Context.getbean * 3, invoke constructor function to create object
* Description: If the Struts2,hibernate,spring container is consolidated, if an error occurs in spring's configuration file, it will only be used when the bean is applied. If a bean holds a large amount of data, it is necessary to load the data * * @Test public void Testcreateobject_when_lazy () {ApplicationContext context = new C
Lasspathxmlapplicationcontext ("Applicationcontext.xml");
Context.getbean ("HelloWorld");
}
}
4 Init, Destroy method
When spring Initializes a bean or destroys a bean, some processing is sometimes required, so spring can invoke the bean's two lifecycle methods when creating and removing the bean. You can specify a method to operate on.
<bean id= "foo" class= "... Foo "init-method=" Setup "destory-method=" teardown "/>
The Init-method method is called when Foo is loaded into the spring container. Call Destory-method when Foo is removed from the container (scope = Singleton valid)
Write HelloWorld
public class HelloWorld {public
HelloWorld () {
System.out.println ("new instance");
}
public void init () {
System.out.println ("Init");
}
public void Destroy () {
System.out.println ("destroy");
}
public void Hello () {
System.out.println ("Hello");
}
Writing test class Initdestroytest
Package Com.itheima10.spring.ioc.initdestroy;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Initdestroytest {
/**
* init-method= "init"
* Order of execution:
* 1, start spring container
* 2, create HelloWorld object
* 3, execute init method
* automatically executed inside Spring container
* 4, Object invocation method
* 5, Destroy method can only be executed if the spring container is shut off Prerequisites: The Bean is a single case
* This method is also called by the Spring container. *
Description:
* If a bean is not a single case, the spring container is not responsible for destroying the object.
* In the spring container, where only one bean is a single case, the spring container is responsible for the creation, initialization, and destruction of the object
* If a bean is not a single case, the spring container is only responsible for creating, initializing/
@Test public
void Testinitdestroy () {
ApplicationContext context =
New Classpathxmlapplicationcontext ("Applicationcontext.xml");
HelloWorld HelloWorld = (HelloWorld) context.getbean ("HelloWorld");
Helloworld.hello ();
Classpathxmlapplicationcontext ApplicationContext = (classpathxmlapplicationcontext) context;
Applicationcontext.close ();
}
Execution Order diagram:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.