Spring JAVA learning notes Encyclopedia

Spring section

1. Declare bean notes
@Component components, there is no clear role
@Service use in the business logic layer (service layer)
in the data access layer uses (dao layer) the @Repository
@Controller used in the presentation layer, a statement of the controller (C)

2. Inject bean annotation
@Autowired: provided by the Spring
@Inject: 330.-by by JSR
@Resource: provided by JSR-250
are set annotation on the methods and properties, recommended on the property annotation (clear, less writing code) .

3.java configuration class associated annotation
Spring configuration (based on) the current class is declared @Configuration class configuration, corresponding to the form of xml
@Bean annotation on the method, it returns the value of the current method declaration of a bean, the alternative embodiment xml ( the method)
@Configuration current class is declared class configuration, wherein the internal composition @Component annotation, indicating that this class is a bean (upper class)
@ComponentScan Component for scanning, corresponding to (based on the xml)
@WishlyConfiguration @Configuration combination with @ComponentScan annotations can replace the two annotations

4. section (AOP) comment about the
Spring supports AspectJ annotation style section of programming.
@Aspect declare a section (upper class)
using @ After, @ Before, @ Around defined suggestions (advice), can be directly blocking rules (cutting point) as a parameter.
@After performed (the method) after performing the method
@Before performed (the method) before the method is performed
@Around performed before the method is performed after (the method)
@Pointcut acoustic Mingqie points
used @EnableAspectJAutoProxy annotation java class open configuration Spring's support for AspectJ agent (the class)

5. @ Bean attributes support
how @Scope Spring container provided new Bean instance (the method, must have @Bean)
disposed types include:
the Singleton (single embodiment, only one container Spring bean instance, the default mode),
Protetype ( each call to create a new bean),
Request (Web project, each http request to create a new bean),
the Session (Web project, create a new one for each http session bean),
globalSession (to each create a new global http session bean instance)
@StepScope Spring Batch also relates to the
@PostConstruct provided by the JSR-250, after executing the constructor is performed, equivalent to the bean xml configuration file in the initMethod
@PreDestory provided by the JSR-250, destroyed before bean performing, in the xml configuration file is equivalent to the bean destroyMethod

6. @ Value annotation
@Value injection attribute value (the property)
to support the injection manner:
"injecting ordinary characters
@Value (" Michael Jackson ") String name;
" OS injection properties
@Value ( "# {systemProperties [ ' The os.name ']} ") String OSNAME;
" expression injection results
@Value ( "# {T (java.lang.Math) .random ()} * 100") String randomNumber;
"other injection bean property
@Value ( "domeClass.name # {}") String name;
"inject file resource
@Value (" the CLASSPATH: COM / HGS / the Hello / test.txt ") String resource file;
" injection site resources
@Value ( "http: // www .cznovel.com ") the Resource URL;
" injection profile
Value ( "$ {book.name}" ) String bookName;
injection configuration using the method:
① write configuration file (, test.properties)
Book. name = "three body"
② @PropertySource load configuration file (the class)
@PropertySource ( "CLASSPATH: COM / HGS / Hello / Test / test.propertie")
③ PropertySourcesPlaceholderConfigurer the need to configure a bean.

7. Environmental switching
@Profile configured to set the current context setting to use by setting the ActiveProfiles Environment. (Classes or methods)
@Conditional Spring4 defined annotations may be used in this condition bean words, by implementing the Condition interface and override matches method, to determine whether the bean is instantiated. (Methods)

8. Asynchronous Related
@EnableAsync class configuration, by opening this annotation support for asynchronous tasks narrative AsyncConfigurer interfaces (the class)
@Async bean used in the method of the annotation to the practical implementation of which is stated on an asynchronous task (or Method All classes will be asynchronous methods, need @EnableAsync enables asynchronous task)

9. The timing of task-related
@EnableScheduling classes used in the configuration, the opening support scheduled tasks (upper class)
@Scheduled to declare this is a task, including cron, fixDelay, fixRate the other type (method, program task must first open support )

10. @ Enable * annotations explain
these notes primarily to enable support for the xxx.
@EnableAspectJAutoProxy turn on automatic proxy support for AspectJ
open support asynchronous method @EnableAsync
@EnableScheduling open support for scheduled tasks
@EnableWebMvc open Web MVC configuration support
@EnableConfigurationProperties open configuration support for @ConfigurationProperties Bean notes
@EnableJpaRepositories open to the SpringData JPA Repository support
@EnableTransactionManagement open annotation transaction support
@EnableTransactionManagement open annotation transaction support
@EnableCaching open an annotation-caching support

11. The test-related annotations
@RunWith runner, Spring typically used to support the JUnit
@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration used to load configuration ApplicationContext, wherein the attribute is used to load configuration class classes
@ContextConfiguration (classes = {TestConfig. class})
SpringMVC part

1. @ EnableWebMvc support Web MVC open configuration in the configuration class, as some ViewResolver MessageConverter or the like, without sentence, WebMvcConfigurerAdapter override method (for the configuration SpringMVC).

2. @ Controller class is declared in the Controller SpringMVC

3. @ RequestMapping Web requests for mapping, comprising (a class or method) and the access path parameters

4. @ ResponseBody support return values ​​in the Response, rather than a page, the user typically returns json data (return value near or method)

5. @ RequestBody allowed request parameters in the request body, rather than directly connected after the address. (On the front of the parameter)

6. @ PathVariable parameters for the receive path, such @RequestMapping ( "/ hello / {name}") path before stated, the annotation on the parameters, to obtain the value, as is generally the interface method Restful.

7. @ RestController This note is a combination of notes, equivalent to a combination of @Controller and @ResponseBody, and notes on the class, which means all of the methods of the Controller will assume added @ResponseBody.

8. @ ControllerAdvice by the annotation, we can place the profile for the global controller in the same position, the annotation @Controller method, can be utilized @ ExceptionHandler, @ InitBinder, @ ModelAttribute annotation to the method,
which all annotations a method in a controller @RequestMapping effective.

9. @ ExceptionHandler global process for abnormalities in the controller

10. @ InitBinder to set WebDataBinder, WebDataBinder reception request parameters used to automatically bind the Model.

11. @ ModelAttribute original role is to bind key-value pairs in the Model, is to let the global @RequestMapping have access to the key set here in @ControllerAdvice in.
----------------
Disclaimer: This article is CSDN blogger "no coding" of the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/D102601560/article/details/104834173

Guess you like

Origin www.cnblogs.com/d102601560/p/12484518.html