spring and springmvc comment summary

spring and springmvc common Note:
Note: participation code compilation, beginning with @. It is to see to the application, the use of separate notes meaningless, must be used with used with the tool, the so-called tool is actually able to read the application notes of
Spring parts:
a statement Bean:
@Component: a common standard of spring Bean class.
@Controller: a controller component class label.
@RequestMapping can match notes on the class. Narrowing may request
@Service: a business logic component class label.
@Repository: a DAO component class label.
Bean instance name of default is the first letter lowercase Bean class, the other parts unchanged. When the class name begins with a capital letter or two more words, bean default name and class name remains the same, we can add annotations to force a name attribute specifies the name of the Bean example @Service ( "user") . In a future release of the spring, @ Controller, @ Service, @ Repository will carry more semantics. Try to consider the use of @ Controller, @ Service, @ Repository lieu of the generic @Component.
In the above configuration generally annotation when a packet needs to be configured to specify the path scanner spring of
<context: component-scan base- package = "com.java.app.service" />
automatically com.java.app scan comment under .service package.
Injection Bean:
@Autowired (spring annotations), automatic injection assembly, eliminating the setter method. Automatically locates and its context in the code match (the default is to match, then use the same type of a plurality of name lookup) the Bean, and automatically injected into the appropriate place.
@Autowired (required = false) required property defaults to true, the error will know less than bean, set to false, it will not be found set to null.
@Autowired
@ Qualifier ( "userJdbcImps") when the same type more time, Specifies the name lookup. You can also modify the name of the field or using injection @Qualifier specified.
@Resource (J2EE annotation): Default An assembly as names, names can be specified by the name attribute, if the attribute name is not specified, when the annotations written on the field, the default field name lookup taken by name, if the write annotations the default name is assembled on a get attribute setter methods. When that matches the name can not be found when assembled in accordance with the type of bean. However, note that if the name attribute if specified, will only be assembled by name.
@Inject (J2EE annotation)
the Java class annotation configuration:
@Configuration current class is declared class configuration, corresponding to the form Spring xml configuration (upper class)
@Bean annotation on the method, the method returns the current value of the statement of a bean, alternative xml in such a way (the method)
@Import annotation may allow loading configuration defined @Bean from other classes.
@Bean property supports:
the @Scope statement scopes IOC container, the default is a singleton. Property Value:
Singleton (singleton) and only one global instance
prototype (multiple cases) every acquisition Bean when there will be a new instance of the
last three can only be used for web applications, and is usually used together XmlWebApplicationContext
Request: spring container will be for each HTTP request to create a new RequestPrecessor object, when after completion of the request, the lifetime of the object comes to a close, as java web request in the life cycle.
Session: Spring container creates a separate session for each new UserPreferences example of their own, much longer than the request scope bean will survive, other aspects no difference, if java web in the session life cycle.
global session: only the application in the portlet Web-based application makes sense, it is mapped to the session global scope of the portlet. If the application manipulation in general servlet-based Web using this type of scope, its container will be treated as an ordinary session type of scope.
@PostConstruct by JSR-250, after executing the constructor is performed, equivalent to the bean xml configuration file in the initMethod
@PreDestory provided by the JSR-250, before performing the destruction Bean, equivalent to the bean xml configuration file destroyMethod

AOP related notes:
the @Aspect Declaring an aspect (the class) using the @ After, @ Before, @ Around custom notification (advice), can be directly blocking rules (point cut) as a parameter.
@After performed after performing the method (the method) which still need to specify a PointCut
@Before performed prior to performing the method (the method) which still need to specify a PointCut
@Around performed prior to performing the method (the method) is also after the PointCut need
@Pointcut acoustic Mingqie @EnableAspectJAutoProxy annotation point uses java class open configuration Spring proxy support for the AspectJ (upper class)
@Value annotation
@Value injection attribute value (the property) @Value ( "$ {}" ) and @Value ( "# {}") in two ways,
@Value ( "# {}") is generally used to obtain expression indicates SpEl bean properties, or call a method of the bean. Of course there may represent constants.
For example:
@Value ( "# {}. 1")
Private int Number; // Get number. 1
@Value ( "# { 'the Spring the Expression Language'}") // Get string constant
private String STR;
@Value ( "dataSource.url # {}") // Get bean properties
private String jdbcUrl;

@Value ( "$ {}") can get file attribute values corresponding attribute definition.
For example: Get the value of a properties file.
A properties file has user = Godv
in xml:

<context:property-placeholder location="classpath:exam.properties"/>

Code:
@Value (value = "$ {} User")
Private String name;
can be a value obtained name Godv.
Test On a related note:
@RunWith is a runner, @ RunWith (SpringJUnit4ClassRunner.class), so that Spring test run in a test environment. Applies only to junit4
@ExtendWith notes can declare trigger the extension in the implementation of test methods and class, @ ExtendWith (SpringExtension.class) make a test run in spring testing environment, applies only to junit5.
@ContextConfiguration used to load configuration ApplicationContext, wherein the property is used to specify classes class loading configuration @ContextConfiguration ( "classpath: applicationContext.xml")
is not configured to read the default path name in the path of the current class: class name -context.xml profile.

@SpringJUnitConfig (locations = "classpath: app -context.xml"). Braces do not write in the current directory to find: the class name -context.xml applies only to junit5
equivalent to
@ExtendWith (SpringExtension.class)
@ContextConfiguration ( "the CLASSPATH: applicationContext.xml")

Junit5的demo
@ExtendWith(SpringExtension.class)
@ContextConfiguration(“classpath:app-context.xml”)
public class app {
@Autowired
private Bean bean;
@Test
public void test1() {
System.out.println(bean);
bean.say();
}
}
其中
@ExtendWith(SpringExtension.class)
@ContextConfiguration(“classpath:app-context.xml”)
可简写成:
@SpringJUnitConfig(locations=“classpath:app-context.xml”)
Junit4的demo:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“classpath:app-context.xml”)
public class app {
@Autowired
private Bean bean;
@Test
void test1 public () {
System.out.println (the bean);
bean.say ();
}
}
can not be abbreviated.
Note: we use unit testing: the same version of our attention to controlling the import version and the eclipse settings.
In junit4 using the @RunWith (SpringRunner.class) to set the runner. And the java runtime version in jdk5 or higher. The package is introduced import org.junit.Test; test method using only public, the other will not.
In junit5 using the @ExtendWith (SpringExtension.class) to set the runner. And the java runtime version in jdk8 or higher. Package is introduced import org.junit.jupiter.api.Test; private testing methods can not be used the other can.

Part Springmvc:
@RequestMapping a process for annotation request address mapping, can be used for class or method. For the class, all the methods in response to the request is represented in the class as a parent class path narrowing address request
Property Description:
value: specifying the requested physical address, the address may be specified URI Template mode (hereinafter will Description);
method: specifying the requested method type, GET, POST, PUT, DELETE and the like;
Consumes: specifying process submit requested content type (content-type), e.g. file application / JSON, text / HTML;
Produces: Specifies the content returned type, only if the rEQUEST request header (the Accept) containing the specified type will return to type;
the params: Specifies the request must include certain parameter values, so that only the processing method.
headers: Specifies the request must contain certain specified header value, in order for this method to process the request.
@ModelAttribute and @SessionAttributes
@SessionAttributes:
the annotation to the object attribute value HttpSession bindings, easy to use in the process parameters in.
The annotation has value, types two attributes, an attribute object can be specified to be used by name and type;
@ModelAttribute
the annotation has two uses, a method for the one for the parameter;
when used in the method : usually used before processing @RequestMapping, model binding need, request from the background;
When the parameters used: by name to a corresponding, binds the value corresponding to the parameter name annotation bean; values to be bound from:
the attribute object A) @SessionAttributes enabled;
B) for @ModelAttribute when specifying the model object methods;
C) above two cases are sometimes no, we need a new new bean object binding, and the request by name in a corresponding manner to bind the values to the bean.
@RequestHeader: to complete the processing method parameter is the method of binding a processor request header (header) transactions;
@CookieValue: parameter function to the method of treatment is completed the processor bound Cookie transactions;

@PathVariable: a request for the URL template variable parameter is mapped to a processing method, i.e. extraction uri template variable as a parameter.
@requestParam: mainly used in the acquisition parameters SpringMVC background controlling layer, a similar one is request.getParameter ( "name"), which has three common parameters: defaultValue = "0", required = false, value = "isApp"; defaultValue represents the set default values, required by boolean setting is what must be passed in parameter, value value indicates acceptance of an incoming parameter type.
@ResponseBody: The annotation for the object Controller method returns, by appropriate conversion HttpMessageConverter body after a specified format data area, the object is written to the Response. And generally used when the return json XML,
@RequestBody annotation used to read the body portion of the data requested Request, using the system default configuration HttpMessageConverter parse, then bind to the corresponding data on the object to return; then HttpMessageConverter returns the object bound to the data controller in method parameters.

Guess you like

Origin blog.csdn.net/qq_41703795/article/details/93308627