在applicationContext.xml中使用Spring @Autowired和bean声明
发布时间:2020-12-16 00:42:28 所属栏目:百科 来源:网络整理
导读:嗨,我对春天比较新.我正在使用注释. 我怀疑开始我上课了 如 public class MyClassA{@Autowiredprivate MyClassB variableClassB;// more code here... 在我的applicationContext.xml中 context:component-scan base-package="package containing MyClassB" /
嗨,我对春天比较新.我正在使用注释.
我怀疑开始我上课了 如 public class MyClassA{ @Autowired private MyClassB variableClassB; // more code here . . . 在我的applicationContext.xml中 <context:component-scan base-package="package containing MyClassB" /> 我的问题是我需要在applicationContext.xml中添加bean声明,如下所示 <bean id="classB" class="com.MyClassB" 或者是否足以拥有@Autowired注释
不它不是.
如果MyClassB注释了@Component,@ Service,@ Repository或@Controller等注释,则组件扫描将为bean工厂中的类创建一个bean. 如果您之前没有使用任何这些注释,则需要手动创建bean 例如: @Component public class MyClassB{ } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |