| Spring | javax.inject.* | javax.inject restrictions / comments |
|---|---|---|
| @Autowired | @Inject | @Inject has no 'required' attribute |
| @Component | @Named | |
| @Scope("singleton") | @Singleton | jsr-330 default scope is like Spring's
|
| @Qualifier | @Named | |
| @Value | - | no equivalent |
| @Required | - | no equivalent |
| @Lazy | - | no equivalent
|
使用@Named和@Inject的时候需要
配置文件中加上要扫描的包
<beans> <context:component-scan base-package="org.example"/> </beans>
Java-based container configuration
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }
等同于
<beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/> </beans>
本文探讨了JSR-330标准注解与Spring框架中的依赖注入注解之间的区别与联系。重点讲解了如何在Spring环境中使用JSR-330的@Inject和@Named注解,并指出了它们与Spring特有注解如@Autowired、@Component、@Scope等的不同之处。

被折叠的 条评论
为什么被折叠?



