English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Component Scanning Based on Annotations

When using component scanning, it is necessary to specify the scanning path in the XML configuration

<context:component-scan back-package="yangjq.test">

The container instantiation will scan all component classes under the package yangjq.test and its sub-packages.

Only when the component class is defined with the following annotation tags, will these component classes be scanned into the Spring container
- @Component general annotation
- @Name general annotation
- @Repository annotation for persistence layer component
- @Service annotation for business layer component
- @Controller annotation for control layer component

Naming

During the scanning process, a default id value (lowercase class name starting) will be generated for the component. This id value can also be customized in the annotation tag, for example:

//This is the default id, with the value of OracleUserDao
@Repository
public class OracleUserDao implements UserDao{
}
//This is a custom id, with the value of loginService
@Service("loginService")
public class UserService{
}

Component Scope

The default scope of Spring-managed components is usually "singleton". If other scopes are needed, the @Scope annotation can be used, and only the name of the scope needs to be provided in the annotation.

@Scope("prototype")
@Repository
public class OracleUserDao implements UserDao{
}

Initialization and Destruction

@PostConstruct and @PreDestroy annotation markers are used to specify callback methods for initialization and destruction, such as:

public class ExampleBean{
	@PostConstruct
	  public void init(){
		//.......initialization
	}
	@PreDestroy
	  public void destroy(){
		//........destruction
	}
}

For annotations with dependency injection relationship Bean

It can use any of the following to implement relationship injection

- @Resource
- @AutoWired/@Qualifier
- @Inject/@Named

We usually use @Resource more, just say @Resource, and search for others when used
@Resource annotation can be used before field definition or setter method definition, and it defaults to match injection by name first, then type matching injection

public class UserService{
	//@Resource This is used in field definition
	private UserDao userDao;
	@Resource  //This is used before the setter method
	public void setUserDao(UserDao dao){
		this.UserDao = dao;
	}
}

When multiple matching Beans are encountered, injection errors may occur. You can explicitly specify the name, for example, @Resource(name = "exampleDao").

Summary

This is the full content of this article about the detailed explanation of component scanning based on annotations. I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site. Welcome to leave a message if there is anything insufficient. Thank you for your support to this site!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like