Spring-MVC annotation Injection

Source: Internet
Author: User

When we do not use annotations to write the spring configuration file, we will define the DaO layer bean in the spring configuration file,
In this way, we can directly call the DaO layer by writing the setdao method in the service layer,
After the annotation is used, you do not need to write the bean at the DaO layer in the configuration file,
You only need to add
@ Repository
Public class testdaoimpl (){}

Definition at the service layer
@ Autowired
Private testdao;
You can call Dao through the interface without writing the setdao method.

Add in service layer
@ Service
Public class testservice (){}
In this way, write directly in the action
@ Autowired
Private testservice;
You can call the service layer

Add in action Layer
@ Controller
Public class testaction (){}
You do not need to define the bean of action in the configuration file

@ Requestmapping is spring-MVC,
In the past, we used struts2. The configuration file must be used to define the action name,
In the browser, write XXX. Do to access action.

After srping-MVC is used, add
@ Controller
Public class testaction (){
@ Requestmapping ("XXX. Do ")
Public String findall (){
.................
}
}
In this way, you do not need struts. Simply enter XXX. Do in the browser to access the findall method.

 

Common annotation methods in hibernate, such as @ repository, and details:

@ Overriide: Specifies to override the parent class method. This annotation can only be used for methods.
@ Deprecated: indicates that a program element (class, method) is out of date.
@ Supperesswarnings: suppress compiler warnings.
@ Autowired is automatically assembled Based on the type. In the preceding example, if there is more than one userdao type in the spring context
@ Qualifier works with @ autowired to solve these problems.
@ Resource is equivalent to @ autowired, but @ autowired is automatically injected by bytype, while @ resource is automatically injected by byname by default. @ Resource has two attributes, namely name and type. Spring resolves the name attribute of @ resource annotation to the bean name, And the type attribute to the bean type. Therefore, if the name attribute is used, the automatic injection policy of byname is used, and the automatic injection policy of bytype is used when the type attribute is used. If neither name nor type is specified, the byname automatic injection policy is used through reflection.
@ Postconstruct (JSR-250)
Add the annotation @ postconstruct to the method. This method will be executed by the spring container after Bean initialization (Note: bean initialization includes instantiating bean, and assemble bean attributes (dependency injection )).

A typical application scenario is that when you need to inject an attribute defined in the parent class into the bean, and you cannot rewrite the attributes of the parent class or the setter method of the attribute.
@ Predestroy (JSR-250)
Add the annotation @ predestroy to the method. This method will be executed by the spring container after Bean initialization. We do not need to use it for the moment, so we will not describe it here. Its usage is the same as @ postconstruct ..
Use @ scope to define the bean Scope
When using XML to define beans, we may also need to define the scope of a bean through the scope attribute of bean. We can also use the @ scope annotation to complete this job.

These should be the spring annotation method to see if you want it. If you want to follow the spring annotation method to search Baidu, there are still many answers.

 

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.