Spring Annotations 01

Once the XML configuration:

<bean id="accountService" class="net.togogo.service.impl.IAccountServiceImpl" scope="singleton"
    init-method="init" destroy-method="destory"></bean>

1. used to create objects:
their role is and write a configuration file in XML <bean>tags realize the function is the same.

2. for injecting data:
their role is and labels XMl profile role in writing a label is the same.

3. For changing the scope of:
their role in it and <bean>use the label scopeproperty to achieve the same functionality.

And 4. lifecycle:
their role is and in <bean>the use of the label init-methodand destory-methodthe role is the same.

1. for creating an object:

1. Role: the class object for the current spring into a container; attribute : specifies the bean id. When we do not write, when he defaults for the current class name and the first letter lowercase. 2. (presentation layer), (business layer), (persistence layer) more than three notes their roles and attributes are exactly the same; three of them are spring provide a clear framework for the three notes we use to make our three layer objects more clearly.@Component

value
@Controller@Service@Repository
@Component

2. Data for injection

And their role is in the xml configuration file <bean>labels write a <property>tag effect is the same.
1. Role: automatic injection by type. As long as there is only one variable type matching the type of object and bean container to be injected, can be successfully injected; if ioc container without any variable type matching the type of bean to be injected and, if an error; if a plurality of types of container ioc when matched, the first found in the map table based on the type of multiple key (they have the same value), and then find that qqq➡ annotated according to the type of the variable names to be injected ( ) appears here: on variables may be, can It is the method. 2. The effect: by name and then injected on the basis of the class according to the above injection. He alone can not be used in the injection to class members (and should be used together). But when the injection parameters to methods. Attribute: : injection specifies the bean id. 3. action: direct injection according to the bean id, which can be used independently. Attribute: used to specify the bean id. More than three injection bean can only inject other types of data, and the basic types and String types can not be used to achieve the above comment. Further, the type of injection set can only be achieved by xml. 4. action: String for injecting basic types and data type; attribute: value for the specified data, it can use the SpEL spring (i.e. the spring expressions el).@Autowired


@Component("qqq")

@Qualifier
@Autowired
value
@Resource

name


@Value()

value

3. The range of action for changing

Their role is and re- <bean>label use of scopefunctional properties to achieve the same. Action: specifies the scope of the bean; attribute : value of the specified range. Common Values: Singleton: and more cases: . The default is not the time to write a single example.
@Scope

valuesingletonprototype

4. and lifecycle

And their role is in <bean>the use of the label init-methodand destory-methodthe role is the same. Action: specifies the initialization method. Role: used to specify the method of destruction.
@PostConstruct

@PreDestroy

Published 31 original articles · won praise 1 · views 257

Guess you like

Origin blog.csdn.net/weixin_41605945/article/details/104433820