SpringMVC common knowledge annotation summary

1.@Controller

Notes to the class name indicates the class is the controller.

2.@RequestMapping("/xxxx")

Can be placed on top of the class name / method name, url indicates when a request method. If the method is the class name has @RequestMapping, RequestMapping the method of accessing the url = RequestMapping project name + class + method.

3.@Resource

It acts as @Autowired, but @Autowired automatically injected by type, and by default @Resource byName injection.

@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.

So if you use the name attribute is used byName automatic injection strategy, and is used when using the type attribute byType automatic injection strategy.

If neither type attribute name is assigned, the automatic injection strategy case byName by using reflection.

Example: If there is more than one context Spring UserDao of bean, BeanCreationException exception will be thrown;

If UserDao type of bean in the Spring context does not exist, an exception will be thrown BeanCreationException.

We can use @Qualifier with @Autowired to resolve.

① When there are multiple bean, solved.

@Autowired

@Qualifier("userServiceImpl")

public IUserService userService;

When solved ② does not exist.

@Autowired(required = false)

public IUserService userService

4.@Service

@Service ( "Bean name") in which the double quotes is optional, you can not fill, fill up on behalf of custom business name. Overall meaning defined business layer bean.

@Service used in the class name of the implementation class. Such as

@Service("alarmManageService")

public class AlarmServiceImp implements AlarmManageService{}

5.@Repository

@Repository ( "Bean's name.")

DAO layer is defined Bean

6.@Component

The definition of Bean, using a bad classification. That addition to the usual classification, this does not belong to any of them Bean can modified with Component.

7. @ InitBinder

For binding data, such as the front page has two objects, a Student, a Course, cut They attribute names are the same, can now use the annotation.

@Controller

@RequestMapping("/classtest")

public class TestController {

@InitBinder("student")

public void initBinderUser(WebDataBinder binder) {

binder.setFieldDefaultPrefix("student.");

}
@InitBinder("course")

public void initBinderAddr(WebDataBinder binder) {

binder.setFieldDefaultPrefix("course.");

}

@RequestMapping("/methodtest")

@ResponseBody

public Map<String,Object> test(@ModelAttribute("student") Student student,@ModelAttribute("course") Course course){

Map<String,Object> map=new HashMap<String,Object>();

map.put("student", student);

map.put("course", course);

return map;

}

 

Guess you like

Origin www.cnblogs.com/chxwkx/p/11120124.html