Spring Common annotations

Source: Internet
Author: User

To turn on the automatic scanning feature before using annotations

Where Base-package is the package (with child packages) that needs to be scanned.

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

@Configuration use a class as an IOC container, and one of its method headers, if registered with @bean, is used as the Bean in the spring container.
@Scope Annotation Scopes
@Lazy (True) indicates deferred initialization
@Service for labeling business layer components,
@Controller for labeling control-layer components, such as action in struts
@Repository is used to label data access Components, or DAO components.
@Component refer to components, we can use this annotation when the component is poorly categorized.
@Scope used to specify scope scope (used on a class)

---------------jsr250----
@PostConstruct used to specify the initialization method (used on the method)
@PreDestory used to specify the method of destruction (used on methods)

@Resource is assembled by name by default, and the bean that matches the name is not found to be assembled by type.

----------
@DependsOn: Defines the order in which beans are initialized and destroyed
@Primary: When multiple bean candidates appear in automatic assembly, the Bean Annotated as @primary will be the preferred one, or an exception will be thrown
The @Autowired is assembled by type by default, and can be used in conjunction with @qualifier annotations if we want to use the assemble by name. As follows:
@Autowired @Qualifier ("Persondaobean") exist with multiple instances

@PostConstruct initialization annotations
@PreDestroy destroy annotations The default single-instance startup is loaded??
@Async an asynchronous method call, you need to add the following code:

12345 <bean id= "Taskexecutor"   class = " Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor " > Code class= "Java plain" ><property name= "corepoolsize"   value= /> <property name= "maxpoolsize"   value= "" /> </BEAN> <TASK:ANNOTATION-DRIVEN/>

In order for @async annotations to take effect, you also need to configure @enableasync in the main program of spring boot

In spring, a method based on @async notation is called an asynchronous method;

These methods will be executed in a separate thread at the time of execution,

The caller does not have to wait for it to complete,

You can continue with other operations.

The function that the @Async modifies is not defined as a static type, so that the asynchronous call does 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 Start a scheduled task




1. @PathVariable

When using the @requestmapping URI template style mapping, which is Someurl/{paramid}, then Paramid can bind the value passed to the method by @Pathvariable annotations

  1. @Controller
  2. @RequestMapping ("/owners/{ownerid}")
  3. Public class Relativepathuritemplatecontroller {
  4. @RequestMapping ("/pets/{petid}")
  5. public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {
  6. //Implementation omitted
  7. }
  8. }

The above code binds the value of the variable ownerid in the URI template and the value of the Petid to the parameter of the method.

If the variable names in the method parameter names and URI template that need to be bound are inconsistent, you need to specify the name in the URI template in @pathvariable ("name").

Spring 4.2 New Features-Adjust the configuration class load order using @order

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

Lombok simplifying Java Code Annotation comprehension

Lombok notes:
Lombok offers a few annotations, and can refer to the official video commentary and official documentation.
Lombok Annotations Online Help documentation: Http://projectlombok.org/features/index.
Here are a few of the Lombok annotations I commonly use:
@Data: Annotations are on classes, provide getting and setting methods for all properties of a class, and also provide equals, canequal, Hashcode, toString methods
@Setter: Annotations on attributes; providing setting methods for attributes
@Getter: Annotations on attributes; providing getting methods for attributes
@Log4j: Annotations are on a class, and a log4j log object with a property named log is provided for the class
@NoArgsConstructor: Annotations on a class, providing an argument-free construction method for a class

@AllArgsConstructor: Annotations are on a class, and a method for constructing a class with full parameters

This article transferred from: http://www.cnblogs.com/xingzc/p/5777814.html

Spring Common annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.