[Spring Notes] Development with annotations

After Spring 4, to use annotation development, the import of the aop package must be guaranteed

 To use annotations, you need to import context constraints, adding support for annotations

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


    <context:annotation-config/>
</beans>

example:

 

 

 @Component: A component, placed on a class, indicates that this class is managed by Spring.

@value:

derived annotation

@Component has several derived annotations. In web development, it will be layered according to the mvc three-tier architecture.

Dao layer: @Repository (the same function as Component)

Service layer: @Service (the same function as Component)

Controller layer: @Controller (the same function as Component)

The functions of these four annotations are the same, they are all assembling beans and registering a class in Spring

Scope:

@Scope("")

 summary:

xml is suitable for any occasion, and maintenance is simple and convenient

The annotation is not used by its own class, and the maintenance is relatively complicated

Best practices for xml and annotations:

XML is used to manage beans, and annotations are only responsible for completing attribute injection.

 

 

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124392843