Spring Annotations Learning Notes

Source: Internet
Author: User

to declare a bean's annotations :

    • @Component: Component, no explicit role
    • @Service: Used at the Business Logic Layer (service tier)
    • @Repository: Used in the Data Access Layer (DAO layer).
    • @Controller: Used in the presentation layer (MVC--SPRINGMVC)
notes injected into the bean:
    • @Aautowired: Annotations provided by spring.
    • @Inject: Annotations provided by JSR-330
    • @Resource: Annotations provided by JSR-250
notes for the configuration file:
    • @Configuration: Declares that the current class is a configuration class, equivalent to a spring-configured XML file.
    • @ComponentScan (Cn.test.demo): Automatically scans all classes that use @Component @Service @Repository @Controller under the package and registers as a bean
    • @WiselyConfiguration: Combined annotations can replace @Configuration and @componentscan
    • @Bean: Annotations on a method, declares that the return value of the current method is a Bean.
      • @Bean (initmethod= "AA", destroymethod= "BB")--Specify the AA and BB methods after construction. Executed before the bean is destroyed.
AOP Facets programming Annotations:
    • @Aspect: Declares that this is a facet
    • @After @Before. @Around defining facets, you can directly use the Intercept rule (pointcut PointCut) as a parameter
    • @PointCut: Specifically define the interception rules and then @Before in the @After. Called in @Around
    • @Transcational: Transaction Processing
    • @Cacheable: Data caching
    • @EnableAaspectJAutoProxy: Turn on spring support for this facet (Aspect)
    • @Target (Elementtype.type): meta-annotations, which specify the member of the callout adornment class--Specify the Intercept rule
    • @Retention (Retentionpolicy.runtime)
      • ---> When the @retention of the defined annotations is runtime, the annotations .--> specified interception rules can be processed by the reflection mechanism at runtime
Spring Common configuration:
  • @import: Import Configuration Class
  • @Scope: An instance of a new bean @Scope ("prototype") declares that Scope is prototype
  • @Value: Attribute Injection
    • @Value ("I Love You")--normal string injection
    • @Value ("#{systemproperties[' Os.name '}")--inject operating system properties
    • @Value ("#{T (Java.lang.Math). Random () * 100.0}")--Injection expression results
    • @Value ("#{demoservice.another}")--inject other bean properties
    • @Value ("Classpath:com/wisely/highlight_spring4/ch2/el/test.txt")--Inject file resources
    • @Value ("http://www.baidu.com")--inject URL resources
    • @Value ("${book.name}")--injection config file Note: use $ instead of #
  • @PostConstruct: Executes after the constructor finishes executing
  • @PreDestroy: Executes before the Bean is destroyed
  • @ActiveProfiles: The profile used to declare an activity
  • @profile: Provides support for different configurations in different environments
    • @Profile ("Dev") .... To provide an instantiated bean for a method named Dev-xxxx
  • @EnableAsync: Turn on support for asynchronous tasks (multithreading)
  • @Asyns: Declares that this is an asynchronous task that can be declared at the class level and at the method level.
  • @EnableScheduling: Turn on support for scheduled tasks (timers)
  • @Scheduled: Declare this is a scheduled task that supports a variety of scheduled tasks, including Cron. Fixdelay fixrate
    • @Scheduled (Dixeddelay = 5000) timed updates via annotations
  • @Conditional: Conditional annotations, creating a specific bean based on satisfying a particular condition
  • @ContextConfiguration: Load configuration file
    • @ContextConfiguration (classes = {Testconfig.class})
    • @ContextConfiguration used to load ApplicationContext
    • The Classes property is used to load the configuration class
  • @WebAppCofiguration: Specifies that loading ApplicationContext is a webapplicationcontext
@Enable * Notes:
    • @EnableAsync: Turn on support for asynchronous tasks (multithreading)
    • @EnableScheduling: Turn on support for scheduled tasks (timers)
    • @EnableWebMVC: Turn on configuration support for Web MVC
    • @EnableAaspectJAutoProxy: Turn on spring support for this facet (Aspect)
    • @EnableConfigurationProperties turn on support for @configurationproperties annotation configuration bean
    • @EnableJpaRepositories: Open support for spring Data JAP Repository
    • @EnableTransactionManagement open support for annotated things
    • @EnableCaching open annotations are supported for caching.
    • @EnableDiscoveryClient Let the service discover the server, using the server. Spring Cloud realizes service discovery
    • @EnableEurekaServer Registration Server Spring Cloud implements service registration @
    • @EnableScheduling allows spring to schedule tasks similar to namespaces in the Spring.xml file <task:*>
    • @EnableCaching open cache cache support;
Springmvc commonly used annotations:
  • @Controller: Annotations Declare this class as a Controller in SPRINGMVC and declare it as a spring bean.
  • @RequestMapping: You can annotate Web requests (access paths and parameters) on classes and methods
    • @RequestMapping (value= "/convert", produces+{"application/x-wisely"}) set the Access URL return value type
  • @ResponseBody: Supports putting the return value into response instead of returning a page (a group of data is returned)
  • @RequestBody: The request parameter is allowed in the request body instead of directly attached to the address after the annotation is placed before the parameter
  • @Path Variable: Used to receive path parameters such as/test/001,001 as parameters, the second note is placed before the parameter
  • @RestController: @Controller + @ResponseBody combination annotations
  • @ControllerAdvice: The global configuration of the controller can be placed in the same location via @controlleradvice
  • @ExceptionHandler: Exception for global processing controller
    • @ExceptionHandier (Value=exception.class)--through the Value property to filter the interceptor condition to intercept all exceptions
  • @InitBinder: Used to set Webdatabinder, Webdatabinder is used to automatically bind the foreground request parameters to the model.
  • @ModelAttrbuute: Binding key-value pairs into model,
  • @RunWith: Runner
    • @RunWith (junit4.class) means to run with JUNIT4
    • @RunWith (Springjunit4classrunner.class), let the test run in the spring test environment
    • @RunWith (Suite.class) is a set of tests,
  • @WebAppConfiguration ("Src/main/resources"): Annotations on classes that declare the loaded Applicationcontex to be a webapplicationcontext, Its properties specify the location of the Web resource, the default is Src/main/webapp, and the customization is modified to resource
  • @Before: Initialize before xxx
Spring Boot Annotations:
  • @SpringBootApplication: Is the core annotation of the spring Boot project The main purpose is to turn on automatic configuration
    • @SpringBootApplication annotation is a combination of annotations, the main combination of @configuration [email protected][email protected]
  • @Value: Attribute injection, read properties in properties or Yml file
  • @ConfigurationProperties: Associating the Properties property with a bean and its properties for type-safe configuration
    • @ConfigurationProperties (prefix = "Author", locations = {"Classpath:config/author.properties"})
    • By @configurationproperties load configuration, specify the configuration prefix via the prefix property, specify the profile location
  • @EnableAutoConfiguration Note: The role is to have spring Boot automatically configure the spring framework based on the dependencies declared by the application
    This note tells spring boot to guess how you want to configure spring based on the added jar dependency. With spring-boot-starter-web the addition of Tomcat and Spring MVC, Auto-configuration will assume that you are developing a web app and setting up spring accordingly.
  • @ Configuration @EnableAutoConfiguration (Exclude={xxxx.class}) disables specific autoconfiguration
  • @Configuration, @EnableAutoConfiguration and @ComponentScan.
@SuppressWarnings Annotations
    • @SuppressWarnings ("Unchecked")
      • Tells the compiler to ignore unchecked warning messages, such as non-parameterized warnings using list ArrayList
    • @SuppressWarnings ("Serial")
      • If the compiler appears with this warning message: The serializable class Wmailcalendar does not declare a static final serialversionuid field of type long Use this comment to remove the warning message.
    • @SuppressWarnings ("deprecation")
      • If you use a method that uses @deprecated annotations, the compiler appears with a warning message. Use this comment to remove the warning message.
    • @SuppressWarnings ("Unchecked", "deprecation")
      • Tells the compiler to ignore both unchecked and deprecation warning messages.
    • @SuppressWarnings (value={"Unchecked", "deprecation"})
      • Equivalent to @suppresswarnings ("Unchecked", "deprecation")
Case
@Entity @table (name = "S_produceinfo") @Data @noargsconstructor@allargsconstructor Public classproduceinfoentity {@Id @Column (name = "App_name", unique =true, length = 50)PrivateString name; @Column (name = "status") @Enumerated (enumtype.STRING)PrivateProducestatus status; @Column (name = "Create_time", updatable =false) @Temporal (Temporaltype.TIMESTAMP) @CreationTimestampPrivateDate Createtime; @Column (name = "Update_time") @Temporal (Temporaltype.TIMESTAMP) @UpdateTimestampPrivateDate UpdateTime;
@Entity: Mapping database entity classes@Table (name = "S_produceinfo"): The table is named "S_produceinfo" @Id: Declares the primary key id@column (name = "App_name", unique = true, length = 50): Corresponding database field, attribute @enumerated (enumtype. STRING): Interactive @Temporal with enumeration value types and database fields: the time Format mapping database is given a date in the specified time format @Enumerted (enumtype.string) HH:MM:SS format @Enumerte D (enumtype.date) acquired date Yyyy-mm-dd

@Enumerted (enumtype.time) Gets the time Division HH:MM:SS

Blog reprinted from: https://www.cnblogs.com/jianliang-Wu/p/5652629.html

Spring Annotations Learning Notes

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.