Home  >  Article  >  Java  >  What are the annotations for spring?

What are the annotations for spring?

(*-*)浩
(*-*)浩Original
2019-05-16 14:35:042356browse

spring annotations can reduce xml configuration; annotation configuration has many advantages over XML configuration: it can make full use of Java's reflection mechanism to obtain class structure information, which can effectively reduce configuration work. For example, when using JPA annotations to configure ORM mapping, we do not need to specify PO attribute names, types and other information. If the relationship table fields and PO attribute names and types are consistent, you do not even need to write task attribute mapping information - because this information is Can be obtained through Java reflection mechanism.

Recommended course: Java Tutorial.

What are the annotations for spring?

Before using annotations, you must enable the automatic scanning function

where base-package is the package (including sub-packages) that needs to be scanned.

<context:component-scan base-package="cn.test"/>

@Configuration treats a class as an IoC container. If @Bean is registered on one of its method headers, it will be used as a Bean in the Spring container.

@Scope annotation scope

@Lazy(true) indicates delayed initialization

@Service is used to annotate business layer components,

@Controller is used to Annotate control layer components (such as action in struts)

@Repository is used to annotate data access components, that is, DAO components.

@Component generally refers to components. When components are difficult to classify, we can use this annotation to annotate them.

@Scope is used to specify scope scope (used on classes)

---------------jsr250----

@PostConstruct is used to specify the initialization method (used on the method)

@PreDestory is used to specify the destruction method (used on the method)

@Resource is assembled by name by default. When looking for No bean matching the name will be assembled by type.

----------

@DependsOn: Define the order in which Beans are initialized and destroyed

@Primary: When multiple Beans appear during automatic assembly When a candidate is selected, the bean annotated with @Primary will be the preferred candidate, otherwise an exception will be thrown

@Autowired is assembled by type by default. If we want to use assembly by name, it can be used in conjunction with the @Qualifier annotation. As follows:

@Autowired @Qualifier("personDaoBean") There are multiple instances used together

@PostConstruct initialization annotation

@PreDestroy destruction annotation Default singleton Loaded at startup? ?

To call @Async asynchronous method, you need to add the following code:

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10"/>
<property name="maxPoolSize" value="300"/>
</bean>
<task:annotation-driven/>

In order for the @Async annotation to take effect, you also need to configure @EnableAsync

in the main program of Spring Boot. In Spring, methods based on the @Async annotation are called asynchronous methods;

These methods will be executed in independent threads when executed.

The caller does not need to Wait for it to complete and

can continue with other operations.

Do not define the function modified by @Async as a static type, so the asynchronous call will not take effect

@ComponentScan(basePackages = "com.xzc.")

@EnableAutoConfiguration

@SpringBootApplication

@PropertySource({"classpath:application.properties", "classpath:xzc.properties"})

@ImportResource("classpath:ws-client .xml")

@EnableRedisHttpSession

@EnableAspectJAutoProxy

@EnableCaching

@EnableAsync

@Configuration

@EnableScheduling starts the scheduled task

@Entity annotation specifies that this is an entity Bean

@SuppressWarnings annotation?

1. @PathVariable

When using @RequestMapping URI template style mapping, that is, someUrl/{paramId}, the paramId at this time can be passed through the @Pathvariable annotation binding it The value of the variable ownerId and the value of petId in the URI template are bound to the parameters of the method.

If the method parameter name is inconsistent with the variable name in the uri template that needs to be bound, you need to specify the name in the uri template in @PathVariable("name").

New features of Spring 4.2 - Use @Order to adjust the loading order of configuration classes

------------------- ----------------------------------

lombok simplifies understanding of java code annotations

lombok annotations:

lombok provides not many annotations. You can refer to the official video explanations and official documents.

Lombok annotation online help document: http://projectlombok.org/features/index.

Here are some commonly used lombok annotations:

@Data: Annotation On the class; provides the getting and setting methods for all properties of the class. In addition, it also provides the equals, canEqual, hashCode, and toString methods

@Setter: annotated on the property; provides the setting method for the property

@Getter: Annotated on the attribute; provides a getting method for the attribute

@Log4j: Annotated on the class; provides a log4j log object with an attribute named log for the class

@NoArgsConstructor: Annotation On the class; provide a no-parameter constructor for the class

@AllArgsConstructor: annotated on the class; provide a full-parameter constructor for the class

======== ==========================

The above is the detailed content of What are the annotations for spring?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn