Spring Framework Learning 3: bean element attributes

Spring's bean common attributes

1.scope

  scope to configure whether singleton object is a bean. Singleton pattern is one of 23 design patterns java, the object means that this project will only run in a class instantiated once, the factory-created classes are generally simple interest mode. Non-called multi-mode single embodiment or a prototype embodiment mode mode.

spring factory object with default interest are single mode, if you want arranged in a plurality embodiment mode, the bean scope attribute may be used, there are two commonly scope values:

  singleton: single embodiment mode, the default value

  prototype: Prototype mode, multi-mode embodiment

Singleton pattern is created when the factory initialization mode is to create more cases of the factory to create an object initialization time.

Illustration, there is also more than one computer E470 example to illustrate the first default singleton, created E470 objects:

import com.zs.entity.impl.E470;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    public void test1() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        E470 e470 = (E470) context.getBean("e470");
        E470 e4701 = (E470) context.getBean("e470");
        System.out.println(e470.equals(e4701));
    }
}
View Code

The result is true, the two objects point to the same address.

Then modify the multi-pattern e470:

Then execute the test class, the result is false.

2.autowire

autowire is used to configure the default spring assembly method object attributes. There are three values

  2.1 no default value is not enabled automatic assembly

  2.2 byType: Depending on the type of automatic assembly

  

  2.3 byName: The name of the automatic assembly, if the bean name attribute is not specified, the default value id

  

injection spring object properties in two ways:

  1. The set value of the injected

    Object provided injection requirement entity classes get and set methods must be generated, then the spring property can be used in plants to achieve the properties set value of the injected

  

  2. injected construction

  Constructor injection requirements:

    1. There must be generated constructor parameters,

    

    2. arranged in the factory configuration parameters

    

Set value of the injected: intuitive to use, in most cases after the project using setter injection, must be generated subject to get and set methods

Injection configuration: without generating get and set methods, less intuitive look.

Guess you like

Origin www.cnblogs.com/Zs-book1/p/10995642.html