Spring study notes (2)

2.  Spring - Notes

2.1.  Annotated principles

Notes on the principle of this blog is very well written: https://www.iteye.com/blog/freewxy-1149128  , and I quote it:

 

Annotation ( Annotation ) provides a mechanism for the safety of similar comments, provides a formal method for us to add information in the code so that we can again some time later facilitate the use of these data (used by parsing comment these data) for any information or metadata with program elements (classes, methods, member variables, etc.) associated. In fact, more intuitive and more straightforward explanation, these instructions information and business logic does not matter, and is designated for the use of a tool or framework. Annotation like one kind of modifier, as applied to the package, the statement affirms type, constructor, method, member variables, parameters and local variables.

Annotation is a kind of interface. By java -related reflection of API to access the Annotation information. Related classes (class frame or tool) according to this information to decide how to use the program elements or change their behavior. Java language interpreter at work ignores these Annotation , so the JVM these Annotation is "ineffective", and can only be through supporting these Annotation tool types of information access and processing.

 

In simple terms, you can make annotations Spring configuration work has become more concise, providing annotation-based configuration XML alternative methods.

2.2.  Notes Basic Configuration

Notes injected XML performed before injection. Thus, the XML configuration overwrites comment properties connected by two methods.

Create a spring of xml configuration file and open support for annotations , you need more import an import constraint context constraints namespace .

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd"> 

<bean>...</bean>
</beans>

 

 

Said before the comment on the nature of the interface , the XML injection annotations inside bean, IOC container in order to identify the content to load annotations. First, <context: Annotation-config> , which is a context configuration namespace provides a method of using this statement implicitly equal to the spring registered vessel . 4 th the BeanPostProcessor :

AutowirdAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessorRequiredAnnotationBeanPostProcessor

This can @Resource , @PostConstruct , @PreDestroy , @PersistenceContext , @Autowired , @Required other annotations, automatic injection.

 

Followed by <context: component-scan base- package = "..."> statement , in addition to annotation-config function outside , Scan scans the base-package the package scanning and registration Javabean . Simultaneously with @Component , @Service , @Repository , @Controller object is registered to the other annotation spring vessel

 

Therefore, when using context: component-scan after , you can be context: annotation-config removed.

 

A point worth noting is the annotation-config does not provide functionality scan package, use @Component and other annotations declared bean objects only with component-scan to scan and register container.

 

Details Reference: https://www.cnblogs.com/zhangsonglin/p/11181064.html

 

2.3.  Common notes

@Component

Components Notes, the resources for spring to manage. Equivalent xml configuration one of the bean . Required <context: Component Base-Package-Scan = "..." /> category annotated to effect scanning ,

Attributes:

value : Specifies the bean of the above mentioned id . If you do not know the value attribute, the default bean 's id is the current class of the class name. The first letter lowercase.

 

@Controller@Service@Repository

Controller annotations, service notes, warehouse notes are of three @Component annotation of a derivative , the role and attributes and @Component is the same , but provides a more explicit semantic. ( The Controller means controlling layer, -Service refers business layers, the Repository refers persistence)

 

@Autowired

Press byType automatic injection mode, when the container is not present or a plurality of the same type of bean when an exception is thrown.

 

@Qualifier

When the automatic injector retrieval bean 's id

 

@Resource

Press byName automatic way of injection

 

@Value

Injection and basic data types String type data

 

@Scope

Specifies bean scope of default Singleton (singleton), with a value of the prototype (multiple patients), Request , the session , globalSession etc.

 

@PostConstruct

It is used to specify initialization method

 

@PreDestroy

Used to specify the methods of destruction

 

@Configuration

The current class is used to specify a spring configuration class will be loaded from the class notes when creating container. When the vessel is required to obtain AnnotationApplicationContext (there @Configuration annotated class .Class ).

 

@ComponentScan

Specifies the spring when the initialization container to scan packages. The role and context: component-scan is the same .

Properties : basePackage

 

@Bean

The only written comment on the method , showed that the use of this method to create an object , and into spring container .

Attributes : name ( i.e. bean 's id)

@PropertySource

For loading .properties file configuration . Often have the configuration data sources .

 

@Import

Introducing other classes , when introduced into other configurations classes , you can not write @Configuration annotations .

 

Guess you like

Origin www.cnblogs.com/HJYDR/p/12343717.html