spring Ioc and related DI

First, the container-related

The core IOC (inversion control) is DI (DI), in order to provide a simple manner dependency components (objects collaborators), and managing the entire lifecycle of these dependencies.

concept:

1. dependent objects: the need to get some components dependent objects, or call the target object.

Inversion of control type:

1. Dependency injection: precedence

(1) constructor injection: setting dependencies needed in the constructor, which is the constructor with no arguments. If there are no dependencies can not create object.

Notice how the constructor can choose, for example, you can specify the parameter type.

(2) injection setter dependency: providing specialized setter methods set dependencies. You can create an object without dependency of time, and then set dependencies through the setter method.

@Autowired can annotations on process (including constructor), the container is automatically set to the parameter-dependent, according to the parameter name.

(3) field injection

@Autowried

private Inspiration inspirationBean; the most common way.

@value annotation method to set an initial value parameter.

(4) Find Method Injection

Obtaining dynamic dependency, a single embodiment can solve the cited non-singleton. CGLIB using dynamic proxy implementation.

Principle: the core is to use <lookup-method> or @Lookup, with annotation method, for example. First using @Scope ( "prototype") to change the scope of a non-single dependencies embodiment, a method is provided and then returns a null in the target bean, and annotations using @Lookup

So call this method of using a spring like to get a dynamic dependencies. Note that the default bean container cases are single mode.

(5) The method of replacing

Any method of dynamically replacing bean, without changing the source code. CGLIB using dynamic proxy implementation.

Principle: The core is the use <replaced-method name = 'name is an alternative method' replacer = 'Alternatively bean'>, where replacer classes that implement the interface MethodReplacer, and registered as a bean.

Instructions:

   a. Other components must be created before you use constructor injection if the component is created, other general use setter injection.

   B. Note the difference in the components and configurations, this number will typically interface configuration, but does not provide dependency.

public interface NewsletterSender(){

      void setSmtpServer(String smtpServer);

      String getSmtpServer();

      void setFromAddress(String fromAddress);

      String getFromAddress();

      void send();

}

The above configuration is four front interface, configured to provide a last method, into the general interface to change the business logic can be defined interface. However, do not put a specific component interface, into subclasses declared set.

Components and configuration differences:

(1) configuration parameters are passive, usually called by other functions.

(2) information is typically configuration parameters, not the component

(3) a simple configuration parameters are usually set value or simple values.

c.setter inject dependencies can be modified without the parent component is created

2. Depending find:

(1) dependence pull: Take from Bean JNDI registry pull, bean container are registered in a registry, the acquisition time dependencies lookup from the registry.

(2) the context-dependent lookup: directly from the container, rather than from the registry. Components to achieve a specific interface, which has a specific way to obtain the current component.

Two: spring application

1. Dependency injection core BeanFactory interface, bean component. BeadFacotry management bean's entire life cycle.

2. The application must create an instance of BeanFactory implements the interface (commonly used is the ApplicationContext, BeanDefinitionReader need to implement the interface in order to obtain bean configuration information from the configuration), and use the bean configuration and dependency information (configured by the bean implements the interface BeanDefinition examples indicates, configuration bean bean itself, there are stored not only information it depends on the bean, typically configuration information to the outside, such as xml or property in the classes corresponding to different loading).

Example:

(1)

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();//创建beanfactory

XmlBeanDefinitionReadder rdr = XmlBeanDefinitionReader (factory); // create a bean configuration to load the class

rdr.loanBeanDefinitions (new ClassPathResource ( 'spring / xml-bean-factory-config.xml')); // bean loading configuration.

 (2)GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();

ctx.load ( 'spring / xml-bean-factory-config.xml'); // omitted compared with the second step above.

3. Use Java configuration

When applying third-party libraries, such as mybatis, when often need to create a lot of fixed bean, these generally will not change, this can be used to configure Java.

Use @Configuration configuration class annotation, and the annotation comprising @Bean methods that called directly by Spring Ioc container instantiated bean. bean name and the name of the method used to create it are the same .

Get Configuration class requires the use of bean

ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);

Get bean configuration there are other ways: @ComponentScan (basePackages = { "com.apress.prospring5.ch3.annotation"}) @ImportResource (locations = { "classpath: spring / app-context-xml.xml"});

4. By using SpEL, Spring can access any Spring managed bean properties and

@Value ( "bean.property # {}")
5.ApplicationContext can be nested, i.e. a plurality of containers can be declared, and the parent-child relationship is provided. And be able to use each other container bean.

6. @ Autowired If the object is a collection type, Spring will attempt to all objects of that type are injected into the current ApplicationContext come in, may cause unexpected dependency is injected. And if there is no change to the next bean, it will throw an exception.

The right way:

(1)@Resource(name="map")

(2)@Autowired

       @Qualifier("map")

The above are two ways to clear the injected bean name, to avoid injecting other dependencies.

7. bean scopes

(1) Scope singleton

(2) prototype scope

(3) Request Scope: instantiating a request for each HTTP bean

(4) session scopes: for each HTTP session (there may be multiple requests) to instantiate a bean

(5) global session scoped: Portlet shared among all applications.

(6) Scope thread: thread per instance of a bean

(7) a custom scope: interfaces implemented org.springframwork.beans.factory.config.Scope

 8.bean disposed dependency order

If it does not reflect a bean to use other bean, but you need to call in the code, then the spring will not be perceived in the configuration, which may cause bean can not be called. For this use annotation @DependsOn ( "bean name");

An automatic assembly mode

(1) byName mode: The name of the implanting

(2) byType mode: The type of injection

(3) Mode Constructor: byType with the same function, all depending on the type, except that instead of the constructor is performed setter injection. And matching the most argument constructor.

(4) default mode: automatic selection between the constructor and byType mode.

Description:

(1) bean automatic assembly must be guaranteed to be unique, otherwise it will report org.springframework.beans.factory.NoSuchBeanDefinitionException exception.

(2) If there is a plurality of the same type when the bean can be used @Qualifier ( "bean name") to specify a particular bean. Usually this comment followed @Autowired.

 

 

 

 

 

 

 

 

 

key problem:

1. How to resolve circular dependencies.

Key processes:

 

Guess you like

Origin www.cnblogs.com/fymc/p/11082083.html