Notes related

Bean common comment?

Bean defined annotations  

@ Controller / @ Controller ( "Bean's name.")

Defining control layer Bean, such as Action

@ Service / @ Service ( "Bean's name.")

Defined business layer Bean

@ Repository / @ Repository ( "Bean's name.")

DAO layer is defined Bean

@Component

The definition of Bean, a bad use classified

 

Bean's automated assembly notes  

@Autowired

Bean assembled by name

@Autowired(required=true)

Be sure to find the matching Bean, otherwise throw an exception. The default value is true

@ Autowired @ Qualifier ( "bean name")

By name assemble Bean, @ Qualifier used in combination with @Autowired solve Bean by type matching to find more problems.

@Resource

By type assembly Bean

 

Bean defined scope and life processes  

@Scope("prototype")

Five kinds of values, commonly used in two ways: Singleton: Singleton, multiple cases: prototype request, session, globalSession

 

The other three @Scope annotations

request

Again request, session, and global session type is only useful in web applications, and is usually XmlWebApplicationContext common use.

<bean id ="requestPrecessor" class="...RequestPrecessor" scope="request" />

Spring container, the creation of a new RequestPrecessor objects XmlWebApplicationContext requests for each HTTP, after the end of the request, the life cycle of the object comes to a close, as java web in the request lifecycle . At the same time there is a time when 100 HTTP request comes in, the container will create 10 new RequestPrecessor for this example were 10 requests, and that they do not interfere with each other, simple terms, request the prototype can be seen as a special case, In addition to the scene is more specific, almost semantically.

session

For web applications, into the session the most common is the user's login information to put this information in the session, we can use the following form to develop scope for the session:

<bean id ="userPreferences" class="...UserPreferences" scope="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

<bean id ="userPreferences" class="...UserPreferences" scope="globalsession" />

Only applications in the global session-based porlet web applications makes sense, it is mapped to the session global scope of porlet, if an ordinary servlet web application used in this scope, the container will scope it as an ordinary session of treatment .

@Resource comment?

@Resource effect equivalent to @ Autowired, @ Resource default automatically injected by byName.

@Resource has two attributes are more important, points that name and type, Spring will @Resource annotation name attribute resolves to the bean's name, the type attribute is resolved to the type of bean.

  1. If you specify the name and type, from the Spring context to find a unique match of bean assembled, can not find an exception is thrown   

  2. If the name is specified, from the context, find the name (id) match bean assembled, can not find an exception is thrown   

  3. If the type is specified, from the context that matches the type of bean find a unique assembled, can not find or find more, will throw an exception   

  4. If neither the specified name, and no specified type, according to the automatic assembly byName manner; if there is no match, then the match is a backoff primitive type, if a match is automatically assembled;

The difference @AutoWired and @Resource?

1, @Autowired @Resource and can be used to assemble bean. Can be written in the field (filed), or written on the setter method.

2, @Autowired default type press fitting (comment this industry are of the spring), by default in claim dependent objects must be present,

To allow a null value, it is required attribute can be set to false, such as: @Autowired (required = false)

@Autowired according to the type of assembly, if no match is found, or when more than one match is found, it will throw an exception (BeanCreationException), can be combined with the use of @Qualifier annotation, @ Qualifire annotation specifies the name (this time specify the type and name). as follows:

@Autowired()@Qualifier("baseDao")
private BaseDao baseDao;

3, @ Resource (this annotation belongs to a J2EE), default assembled by name, the name can be specified by the name attribute, if you do not specify the name attribute, when the notes written on the field, by default take the field names for installation name lookup, if Notes written in the property setter methods to take default name for assembly. 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, can not find throw an exception.

@Resource(name="baseDao")
private BaseDao baseDao;

Recommended @Resource

@Resource annotations on the field, so do not write the setter methods, and this comment is part of J2EE, reducing the coupling of the spring. Since this code is relatively elegant look.

Springmvc in @RequestMapping property usage induction?

@RequestMapping

RequestMapping is a process for annotation request address mapping, it can be used for class or method.

For the class, all the methods in response to the request class are represented in the address as the parent path.

RequestMapping notes six attributes (divided into three categories explained) with six basic usage,

1、 value, method;

value: Specifies the physical address of the request, the address may be specified URI Template mode;

method: method specifies the type of request, GET, POST, PUT, DELETE and the like;

2、 consumes,produces;

consumes: submit a request specifying process of the content type (Content-Type), for example, application / json, text / html;

produces: content type of the returned request only if the request header (the Accept) containing the specified type will return to type;

3、 params,headers;

params: Specifies the request must contain certain parameter values, before allowing the processing method.

headers: Specifies the request must contain certain specified header value, in order for this method to process the request.

 

Guess you like

Origin www.cnblogs.com/lvhouhou/p/11983950.html