Home > Java > javaTutorial > What does @Autowired do?

What does @Autowired do?

(*-*)浩
Release: 2019-09-09 16:55:45
forward
9881 people have browsed it

@Autowired is an annotation that can mark class member variables, methods and constructors, allowing spring to complete the work of bean autowiring.

What does @Autowired do?

@Autowired defaults to matching by class, and @Qualifier specifies to assemble beans by name.

Common usage:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
 
import blog.service.ArticleService;
import blog.service.TagService;
import blog.service.TypeService;
 
@Controller
public class TestController {
 
	//成员属性字段使用 @Autowired,无需字段的 set 方法
	@Autowired
	private TypeService typeService;
	
	
	//set 方法使用 @Autowired
	private ArticleService articleService;
	@Autowired
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
 
	//构造方法使用 @Autowired
	private TagService tagService;
	@Autowired
	public TestController(TagService tagService) {
		this.tagService = tagService; 
	}
	
}
Copy after login

The above is the detailed content of What does @Autowired do?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template