[Learning Spring 1] IoC container

Brief introduction

General java program will have a lot of classes, class assignment statement and so needs its own manual, cumbersome. BeanIs a norm, it is also a normal java class, people want to write a program like loose coupling, each class can realize their functions, Bean is similar to a part of a program assembled by a number of Bean made.

Many classes, we often use to repeat, but initialization and assignment and other operations are repetitive, but also in the business code to find ways to find a place to do it.

By spring frame, configured bean, ioc container will automatically help you newout, and to give good value, automatically managing the lifecycle of bean. We just need to use only where declarations, ioc container will automatically help you put all configurations are done.

Bean

Creating Bean

It is an ordinary java bean class, member variables and their corresponding setter and getter methods composition.

package com.yww;

public class Message {
    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = "this mssage is : " + msg;
    }

}

Bean Lifecycle

实例化 -> 填充属性 -> 调用BeanNameAware的setBean()方法 -> 调用BeanFactoryAware的setBeanFactory()方法 -> 调用ApplicationContextAware的setApplication()方法 -> 

调用BeanPostProcessor的预初始化方法 -> 调用InitializingBean的afterPropertiesSet()方法 ->

调用自定义的初始化方法 -> 

调用BeanPostProcessor的初始化方法 -> 

使用Bean ->

(容器关闭)

调用DisposableBean的destroy()方法 ->

调用自定义的销毁方法

Probably the process is, bean instantiation, assignment, (incoming instance to the vessel), (call a custom method), use containers closed (call a custom method of destruction).

In parentheses after the corresponding interface need bean implementation, additional operations can be realized.

Bean scopes

Bean may be provided in the configuration file scopescope.

Possible values: singleton(singleton), prototype(every new), request(each new globe), session(per session sharing), global-session (for Protlet).

Bean definition of inheritance

bean can be specified parentto specify another bean is idback, so that it inherits.

Although there is no inheritance in the java file, but inherited effects can be achieved in the configuration file, or you can subclass members parent class. But subclass which requires a member of the parent class.

Bean PostProcessor

Bean through inheritance BeanPostProcessorinterface allows us to do some custom actions when Bean initialization and destruction.

IoC container

spring container is responsible for creating objects, assembling them, configure them, manage their entire life cycle.

spring comes to realize a plurality of containers, into two categories.

  • bean plant ( org.springframework.beans.factory.beanFactory) is the simplest container, provide the most basic DI support.
  • Application context ( org.springframework.context.ApplicationContext) based BeanFactory is created, providing the application framework level of service. (Such as information from a properties file and parse text event listener to publish application events of interest)

Spring BeanFactory container

BeanFactory need the help of other objects to find the .xmlfile.

// ...
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
Message msg = (Message)factory.getBean("messge1");
// ...

ClassPathIt is the src/path.

Spring ApplicationContext容器

There are many types of spring application context, such as:

  • AnnotationConfigApplicationContext: loading spring java application context from configuration class based.
  • AnnotationConfigWebApplicationContext: loading spring web-based application context from configuration java classes.
  • ClassPathXmlApplicationContext: xml configuration file is loaded from the context classpath.
  • FileSystemXmlapplicationContext: load context from the xml configuration file in the file system.
  • XmlWebApplicationContext: load context from xml configuration file in the Web application.
// ...
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
ApplicationContext context = new FileSystemXmlApplicationContext("/home/yww/beans.xml");
ApplicationContext context = new AnnotationConfigApplicationContext(MessageConfig.class);

Message msg = (Message)context.getBean("messge1");
// ...

Several examples of this application context, use in App, instead of the web.

AnnotationConfigWebApplicationContextCan be configured in web.xmlthe <context-param>middle, so that ContextLoaderListenerthe use of AnnotationConfigWebApplicationContextsubstitute XmlWebApplicationContext.

Guess you like

Origin www.cnblogs.com/maplesnow/p/11620259.html