常用注解的区别
@Bean与@Autowired的区别:
@Bean 修饰的方法表示初始化一个对象并交由Spring IOC去管理,@Bean 只能和@Component @Repository @Controller @Service @Configration 配合使用.
@Autowired 可修饰变量和方法,用于完成自动装配(将需要的外部资源注入)
@Resource和@Autowired:
首先,出身不同:
Resource竟然不是spring框架里的,而Autowired则是spring里面的:
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
其次呢,Autowired只按照类型注入,不匹配name进行注入。而Resource就比较灵活,默认的话按照name匹配(即示例中的bean的id),Resource可以通过配置它的两个属性:name和type来指定按照类型还是名称注入。

浙公网安备 33010602011771号