The real meaning of Spring Framework's object creation time, initialization and destruction, and the combination of IOC and DI...

In a trance, the second year of sophomore year has passed, and I am about to enter the junior year. The first blog was written when I was a sophomore, and now I feel that the quality of the blog is not as good as before. I have been busy, but I feel that I am not busy. What is the effect, more blog and less, just paste some codes, less text description, I feel that I have become perfunctory, it seems that I should reflect on what I have done this year. Well, not much nonsense, let's post a summary of this recent study of spring.

1. What is Spring and what is Spring used for ?

Spring is a very active open source framework; it is a framework for building multi-layer JavaEE systems based on Core , and its main purpose is to simplify enterprise development .

Spring manages your code in a non-intrusive way. Spring advocates "least intrusion", which means that you can install or uninstall Spring when appropriate

 Spring is a container

2. Spring configuration file 

     By default is the applicationContext.xml file. Many xml files can be created, which are generally configured in the project.

3. When Spring creates the object

a. ( By default , objects are created when the Spring container is started )

Advantage : Because the object is created when the Spring container starts , as long as the configuration file is written incorrectly, the error can be found at the beginning (the web container starts )

    b. Lazy loading lazy-init=true  ( the object is only created when context.getBean )

   

<bean id=”” class=”” lazy-init=”true”></bean>

 

 

 

4. How to prove whether the Spring container is singleton or multiple

 

  a. We can write a small example, look at the address, the object in the Spring container is a singleton by default, because the object is a singleton, so as long as you declare an attribute on the class, the attribute contains data, then the attribute is Global (it is very dangerous to do so), as shown below

   <bean id="helloWorld" class="com.zkx.spring.scope.HelloWorld"></bean>

Test :

@Test
public void testScope_Default(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");

HelloWorld helloWorld2 = (HelloWorld)context.getBean("helloWorld");

System.out.println(helloWorld);

System.out.println(helloWorld2);

}

 

 

result com.zkx.spring.scope.HelloWorld@43763e0b

  com.zkx.spring.scope.HelloWorld@43763e0b

 Proves that objects in the Spring container are singletons by default

 

 

  b. If the scope is " prototype " , the objects generated by the spring container are multi-instances. No matter what the value of lazy-init is, the object is only created when context.getBean is used.

  <bean id="helloWorld2" class="com.zkx.spring.scope.HelloWorld"

   scope="prototype"></bean>

 

The test result is 2 different addresses, so the test code will not be listed here.

 

Note : In web development, the Srvice layer and the Dao layer are usually put into the Spring container and integrated into a singleton, so that you enter a url in the browser , one thousand visits, ten thousand visits, Action calls Service, Service If Dao is called , only one object of the Srvice layer and one object of the Dao layer will be generated, so the pressure on the JVM to destroy these objects is greatly reduced

 

5. What is an IOC?

IOC: Inversion of Control

IOC is Spring 's inversion of control: the creation, initialization, and destruction of objects are handed over to the spring container. The life cycle of objects is controlled by the spring container.

 

 

6. Spring initialization and destruction

applicationContext.xml文件中,加上下面的代码

<bean id="helloWorld" class="com.zkx.spring.initdestroy.HelloWorld"

init-method="init"

destroy-method="destroy"></bean>

初始化方:init-method

销毁方法 :destroy-method

 

public class HelloWorld {

public HelloWorld(){

System.out.println("aaaa");

}

public void hello(){

System.out.println("hello world");

}

public void init(){

System.out.println("init");

}

public void destroy(){

System.out.println("destroy");

}

}

 

 

 

@Test

public void testInitDestroy(){

ApplicationContext context = 

new ClassPathXmlApplicationContext("applicationContext.xml");

HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");

helloWorld.hello();
ClassPathXmlApplicationContext applicationContext = (ClassPathXmlApplicationContext) context;
applicationContext.close();//spring容器关闭

}

 

 

测试结果:

aaa

Init

Helloworld

destory

Spring容器关闭时,调用销毁方法,如果spring容器没有执行close方法,则不执行销毁方法,如果spring容器执行了close方法,在执行该方法之前要执行销毁方法;在构造方法之后,立刻执行init方法(destroy用途不是很大,init方法用途很多)

7.Spring的执行流程

 

 

8.DI?

DI:Dependency Injection 依赖注入

 依赖注入简单的说就是给属性赋值,有2种注入的方法:

 a.使用构造器注入

使用xml的注入方式

   通过参数的顺序

<constructor-arg index="0">

      <value>张三</value>

</constructor-arg>

<constructor-arg index="1">

       <value>56</value>

 </constructor-arg> 

 

 

  通过参数的类型

<constructor-arg type="java.lang.Integer">

              <value>56</value>

       </constructor-arg>

       <constructor-arg type="java.lang.String">

              <value>张三</value>

</constructor-arg>

 

 

b.使用属性set方法进行注入

使用xml的注入方式:

   简单Bean的注入

简单Bean包括两种类型:包装类型和String

<bean id="personService"   class="com.zkx.bean.impl.PersonServiceImpl">
<!-- 基本类型,string类型 -->
<property name="age" value="20"></property>
            <property name="name" value="张无忌"></property>    
</bean>
        <!--  引用其他Bean -->
               <bean id="person" class="com.zkx.bean.Person" />
               <bean id="personService"  class="com.zkx.bean.impl.PersonServiceImpl">
            <property name="person" ref="person" />

</bean>

 

 

 

 

 

9.IOCDI结合的真正意义

 a.不完全的面向接口编程(客户端还得关心这个接口是由哪个类实现的)

如下图就是不完全的面向接口编程(WordDoment等类实现了Document接口,具体类的实现我就不列出来了),包括我以前那篇关于面向接口编程的博客,也是不完全的面向接口编程

@Test

public void testDocument_NOSpring(){

//为不完全的面向接口编程

Document document = new WordDocument();

DocumentManager documentManager = new DocumentManager();

documentManager.setDocument(document);

documentManager.readDocument();

documentManager.writeDocument();

}

 

 

   bdocumentManager,wordDocument,excelDocument,pdfDocument放入到spring容器中

  

   

<bean id="documentManager" class="com.zkx.spring.iocdi.document.DocumentManager">

   <property name="document">

   <ref bean="wordDocument"/>

   </property>

   </bean>

   <bean id="wordDocument" class="com.zkx.spring.iocdi.document.WordDocument"></bean>

   <bean id="excelDocument" class="com.zkx.spring.iocdi.document.ExcelDocument"></bean>

   <bean id="pdfDocument" class="com.zkx.spring.iocdi.document.PDFDocument"></bean>

 

 

 

Java代码端的测试

@Test

public void testDocument_Spring(){

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

DocumentManager documentManager = (DocumentManager)context.getBean("documentManager");

documentManager.writeDocument();

documentManager.readDocument();

}

 

 

没有出现一个WordDocment等,也就是说java代码这头,只需要完全面向接口编程就行了,只需要关心DocmentAPI实现什么样的功能,至于说这个Docment具体是谁,我不关心

这就是IOCDI结合的真正意义:

Java代码端完全的面向接口编程(现在很多容器都有这功能,都是模仿Spring)

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326668184&siteId=291194637