The difference between @autowired and @resource in Spring annotations

The difference between @autowired and @resource in Spring annotation


@resource
1. The @Resource annotation is used to activate the dependency injection of a named resource,
3. @Resource is automatically injected by name by default (the name matches)

@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}


The @Resource annotation doesn't even require an explicit string value, the domain name will be used as the default if no value is provided.
@Resource
private DataSource dataSource; // inject the bean named 'dataSource'



@autowired
1. @Autowired is automatically injected by Type (the type of the bean matches)


@Required
1. @Required annotation checks, but he only checks whether the attribute has been set and does not test whether the attribute is not empty
2. @Required can only be set in 3. @Required on the setter method
is to annotate whether the property has been set. If there is no set value, a BeanInitializationException will be thrown.



Refer to the original text: http://www.yiibai.com/spring/spring_required_annotation.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327027057&siteId=291194637
Recommended