Original link:http://www.8qiu.cn/archives/1172
Annotation | Package
Detail/import Statement |
@Service |
Import Org.springframework.stereotype.Service; |
@Repository |
Import Org.springframework.stereotype.Repository; |
@Component |
Import org.springframework.stereotype.Component; |
@Autowired |
Import org.springframework.beans.factory.annotation.Autowired; |
@Transactional |
Import org.springframework.transaction.annotation.Transactional; |
@Scope |
Import Org.springframework.context.annotation.Scope; |
Spring MVC Annotations |
@Controller |
Import Org.springframework.stereotype.Controller; |
@RequestMapping |
Import org.springframework.web.bind.annotation.RequestMapping; |
@PathVariable |
Import org.springframework.web.bind.annotation.PathVariable; |
@RequestParam |
Import Org.springframework.web.bind.annotation.RequestParam; |
@ModelAttribute |
Import Org.springframework.web.bind.annotation.ModelAttribute; |
@SessionAttributes |
Import org.springframework.web.bind.annotation.SessionAttributes; |
Spring Security Annotations |
@PreAuthorize |
Import org.springframework.security.access.prepost.PreAuthorize; |
@Service, @Repository, @Controller, @Component
@Service, @Repository, @Controller, @Component these four are all beans used to annotate spring, and they are equivalent at the point of view of the program. But from the name, it is easy to see that @service is the bean–service of the annotation business layer, @Repository is the Bean–dao of the annotations persistence layer, @Controller is the controller of the annotated MVC, @Component Used to annotate ordinary beans, when the bean is poorly categorized, use @component.
@Autowired
Automatic injection of the value, as follows automatic injection of Companydao. The premise is to ensure that Companydao exists in the spring context.
@Servicepublic class Companyserviceimpl implements Companyservice {@Autowired
Private Companydao Companydao;
...
}
@Transactional
Add @transactional to a service class, stating that all methods of the service support transaction management, and if you add @transactional on a method, only declare that the method supports transactions. A transaction is opened before the method that supports the transaction begins execution, and is rolled back when an exception is encountered. Spring's default configuration is that transaction rollback occurs when RuntimeException is encountered.
@Scope
corresponding to the scope of <bean scope= "prototype"/>, its values are singleton, prototype, Request,session,global session and Custom mode.
@RequestMapping
Use this annotation on a class or method to set the URL access address. It has two properties, value specifies the access path, method specifies the request method, and the request method is defined in the Requestmethod class, all in constant form, which uses the GET request by default. It can also specify only the access path, as shown below
The following {Context.path}/comany/addcompany map to the Addcompany method.
@Controller
@RequestMapping
(
"/company"
)
public
class
CompanyController {
@Autowired
private
CompanyService companyService;
@RequestMapping
(
"/addCompany"
)
Public Modelandview Addcompany (Company C) { .... }
}
@PathVariable
Gets the URL access path variable. In the following example, the access path is the value obtained by the/company/techferry,companyname is Techferry.
@Controller
@RequestMapping
(
"/company"
)
public
class
CompanyController {
@Autowired
private
CompanyService companyService;
@RequestMapping
(
"{companyName}"
)
public
String getCompany(Map<String, Object> map,
@PathVariable
String companyName) {
Company company = companyService.findByName(companyName);
map.put(
"company"
, company);
return
"company"
;
}
...
}
@RequestParam
The parameter on the URL is bound to the parameter on the Controller method, and the value of Pagenum is derived from Request.getparameter ("Pagenum").
@[email protected] ("/company") public class Companycontroller {@Autowired
Private Companyservice Companyservice;
@RequestMapping ("/companylist")
Public String listcompanies (map<string, object> Map, @RequestParam int pagenum) {
Map.put ("Pagenum", pagenum);
Map.put ("Companylist", Companyservice.listcompanies (Pagenum));
return "Companylist";
}
...
}
@ModelAttribute
When there are many parameters, the code of the method is very long and very ugly, as defined directly on the method. It is common practice to define a formbean and then use @modelattribute annotations before Formbean. Spring MVC automatically sets the parameters of the URL to the Formbean based on the matching field name.
@Controller
@RequestMapping
(
"/company"
)
public
class
CompanyController {
@Autowired
private
CompanyService companyService;
@RequestMapping
(
"/add"
)
public
String saveNewCompany(
@ModelAttribute
Company company) {
companyService.add(company);
return
"redirect:"
+ company.getName();
}
...
}
@SessionAttributes
Declare a variable that is stored in the session and can be shared between multiple requests.
@PreAuthorize
Permission validation
In the following example, only the user's ROLE contains "role_admin" to delete the contact.
@Transactional
@PreAuthorize
(
"hasRole(‘ROLE_ADMIN‘)"
)
public
void
removeContact(Integer id) {
contactDAO.removeContact(id);
}
Spring annotations (Spring Annotations)