Basic attributes of Spring XML Bean tags

Basic attributes of Spring XML Bean tags

1. Introduction to label attributes:

  • id

Bean name, the default is the class name;

  • class

The class of the proxy class;

  • name:

Equivalent to alias, bean alias, multiple aliases are separated by',';

  • parent:

First, parent does not refer to the inheritance relationship of the bean object, it refers to the inheritance relationship of the label, that is, a label can inherit all the data of another label through the parent, avoiding the repeated writing of some repeated data;

  • scope:

Scope indicates the scope of the bean. The default is singleton, prototype, which means singleton and multiple instances. The Spring Web container adds three scopes: request, session, and global-session;

singleton(default) Singleton
prototype Multiple cases
request Each HTTP request corresponds to a bean
session Each session corresponds to a new bean object
global-session The global session is only meaningful when applied in a porlet-based web application. It is mapped to the global scope of the porlet. If this scope is used in an ordinary servlet web application, the container will treat it as the scope of an ordinary session. .
  • abstract:

true or false, indicating whether this is an abstract bean that cannot be instantiated. Generally speaking, we can combine to extract public configuration data;

  • lazy-init:

true or false, whether to delay loading;

  • depends-on:

This bean will depend on the bean. When the bean is initialized, it will determine whether depends-on is initialized. If not, the depends-on bean will be initialized first;

  • autowire:

Automatic injection method, no (default), byName, byType, constructor, autodetect; default no does not inject attributes; constructor is still byName, but it is injected through constructor; if autodetect has a default construction method, pass The construct method is automatically assembled, otherwise the byType method is used for automatic assembly;

  • autowire-candidate:

true or false, the default is true, if the autowire-candidate attribute is set to false, this bean will not be considered when other bean properties are injected.

  • primary:

true or false, the default is false. If the primary is true and there are multiple types of the same bean, the bean whose primary is true will be selected during automatic injection;

  • init-method:

Initialization method, when the bean is created, spring will call invokeInitMethods to execute the initialization method;

  • destroy-method:

When the bean is destroyed through spring, the destroy-method method will be executed;

  • factory-method:

Create a bean factory method, the method has two forms, a static method, as shown below:

<bean id="stu" class="com.bin.pojo.StuDO" factory-method="getStuDo"/>
public static StuDO getStuDo() {
    
    
    StuDO stu = new StuDO();
    stu.id = idBuilder++;
    return stu;
}

The other is to use a common method as a bean factory, but it requires a collection of factory-beans to use, which is to specify one as a bean factory:

<bean id="stu" factory-bean="stuOne" factory-method="getStuDo"/>
  • factory-bean:

As shown above, indicating that this bean is a factory bean

2. Sub tags:

  • property:

Some basic properties of the bean;

  • constructor-arg:

Construct method to inject data

  • lookup-method:

Method rewrite, specify the name of the method to be written from, and the bean to be returned. The bean returned by lookup-method is a multi-instance bean, so we can get some multi-instance beans in a singleton bean;

  • replaced-method:

Like lookup-method, it is also method rewriting, but the point is that the bean of replace-method must implement the MethodReplacer interface;

  • qualifier:

When injecting by type, there will be multiple beans of the same type. At this time, we can use the name to inject through the qualifier;

  • meta:

Class metadata;

Guess you like

Origin blog.csdn.net/qq_36845919/article/details/107452678