Spring-based annotations and XML hybrid approach of using

Learning Based IoC configuration notes, first of all there must be a recognition that the configuration annotations and xml configuration functions to be implemented are the same, are to reduce the coupling between the programs. Configuring form just is not the same.
When bargain comment configuration, before the Spring xml configuration content gradually changed to use annotations.
Use IoC annotations

  • The first step: spring configuration file, configuration context: component-scan tag
    Here Insert Picture Description
  • Step two: the top class annotated @Component, or derived his notes @ Controller, @ Service, @ Repository

Here Insert Picture Description
Common Annotations
IoC annotations (create an object)
is equivalent to:

<bean id="" class=""></bean>

Component Notes

  1. Role:
    to let spring to manage resources. Bean corresponds to a configuration in xml

  2. Property
    value: Specifies the bean id. If you do not specify a value attribute, the default bean id of the current class is the class name, the first letter lowercase

Controller & Service & Repository annotation
them three notes are derived for comment @Component of their role and attributes are exactly the same. They simply provide a more explicit semantic.
Note: If there is one and only one annotation attribute to assign, and the name is value, value can not write at the time of assignment.

  • @Controller: General performance for the annotation layer
  • @Service: general business for the annotation layer
  • @Repository: generally used for annotation persistence layer

DI annotation (DI)
corresponds to:


@Autowired

  • @Autowired default by type mounting (the byType)
  • @Autowired is implemented by the class AutowiredAnnotationBeanPostProcessor
  • @Autowired is spring comes notes
  • @Autowired default claim dependent objects must exist a case where, if the need to allow a null value, he can set the required property is false, if: @Autowired (rerequired = false)
  • If you want to assemble by name (byName) can be use in conjunction with @Qualifier comment

@Qualifier

  • In accordance with the type automatically based on the injection, the injection according to the id Bean
  • He can not be independent when to use field injection, must be used with @Autowired
  • However, when a method of injection parameters, can be used independently

@Resource

  • @Resource default (byName), you can specify the name by the name attribute @Resource assembly by name, if you do not specify the name attribute, when the notes written on the field, take the default field name lookup by name, when the name can not be found bean match when assembled in accordance with the type.
  • @Resource belongs to achieve J2EE JSR250 specification
  • However, note that if the name attribute if specified, will only be assembled by name

We recommended @Resource notes, because he belongs to J2EE, reducing the coupling of the Spring. This code looks more elegant.

@Inject

  • According to the type of automatic assembly, the assembly if required by name, it is necessary with @Name
  • May act on the variable, the setter method, constructor,
  • @Inject JSR330 is the specification, need to import javax.inject.Inject; implemented injection.

@value

  • A basic type and injection type String Value
  • A placeholder values ​​acquired in the attribute file
@value(“${name}”)//name是properties文件中的key
private String name 

@ Autowired, @ Resource, @ Inject distinction
1. @ Autowired is spring comes, @ Inject JSR330 specification is achieved, @ Resource JSR250 specification is implemented, need to import different packages
2. @ Autowired, @ Inject use of the base same, except that there is a request attribute @Autowired
3. @ Autowired, @ Inject default according to the type of matching, @ Resource accordance with matching names
need to be matched with the needs and @Qualifier 4. @ Autowired if used by name, @ Inject and used with @Name

Bean changing scope notes

  • @Scope: Specifies the scope of the bean, is equivalent to the following configuration:
<bean id="" class="" scope="">
  • Attribute:
    value: value of the specified range. Value: singleton prototype request session globalsession

Lifecycle

  • @PostConstruct
  • @PreDestory
    equivalent to:
<bean id="" class="" init-method="" destroy-method=""/>

About the choice of notes and XML

  • Advantages annotation:
    simple to configure, easy to maintain (find the class, which is equivalent to find a corresponding configuration).

  • XML advantage:
    When modifications do not change the source code. It does not involve recompiling and deploying.

  • Compare Spring Bean management approach
    Here Insert Picture Description

Published 13 original articles · won praise 3 · Views 1001

Guess you like

Origin blog.csdn.net/qq_33358408/article/details/104048115