Java 如果我们使用注释,我们是否需要在xml中指定bean

Java 如果我们使用注释,我们是否需要在xml中指定bean,java,spring,Java,Spring,如果我们使用annotation@aurowired,我们是否需要在xml中指定bean,如果我们使用annotation@aurowired,是否需要在xml中使用bean id public class TextEditor { @Autowired private SpellChecker spellChecker; public TextEditor() { System.out.println("Inside TextEditor constructor." );

如果我们使用annotation@aurowired,我们是否需要在xml中指定bean,如果我们使用annotation@aurowired,是否需要在xml中使用bean id

public class TextEditor {
  @Autowired
  private SpellChecker spellChecker;
  public TextEditor() {
     System.out.println("Inside TextEditor constructor." );
  }
  public SpellChecker getSpellChecker( ){
     return spellChecker;
  }
  public void spellCheck(){
     spellChecker.checkSpelling();
  }
}
应用程序上下文

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config/>

 <!-- Definition for textEditor bean without constructor-arg  -->
 <bean id="textEditor" class="com.tutorialspoint.TextEditor">
 </bean>

 <!-- Definition for spellChecker bean -->
 <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
 </bean>

</beans>

使用包将组件扫描添加到XML中,并在顶部用@component注释TextEditor和SpellChecker

<context:component-scan base-package="com.tutorialspoint" />

然后您不需要在XML文件上定义任何bean。

使用包将组件扫描添加到XML中,并在顶部用@component注释TextEditor和SpellChecker

<context:component-scan base-package="com.tutorialspoint" />

然后,您不需要在XML文件上定义任何bean。

不需要在应用程序上下文中声明每个bean。您可以使用组件扫描并提供包含所有bean的基本包

<context:annotation-config />
    <context:component-scan base-package="com.package" />

并为要自动连线的每个类使用@Service标记。

无需在应用程序上下文中声明每个bean。您可以使用组件扫描并提供包含所有bean的基本包

<context:annotation-config />
    <context:component-scan base-package="com.package" />

并对要自动连线的每个类使用@Service标记。

要定义托管bean,您可以

用XML定义它 在bean的类和适当的上下文上使用@Component或@Service注释:XML中的组件扫描 要使用它,你也可以

用XML声明依赖关系 使用@Autowired注释 如果要使用任何注释,则需要XML中的context:annotation-config


如果要通过ID引用bean,只需将ID分配给该bean,无论是在XML中还是在自动连接时在@Qualifier注释中

用XML定义它 在bean的类和适当的上下文上使用@Component或@Service注释:XML中的组件扫描 要使用它,你也可以

用XML声明依赖关系 使用@Autowired注释 如果要使用任何注释,则需要XML中的context:annotation-config


如果希望通过该ID引用bean,则只需将ID分配给该bean,无论是在XML中还是在自动连接时在@Qualifier注释中。

@aurowired默认情况下,自动按类型连接bean。如果你使用

<bean  class="com.tutorialspoint.SpellChecker"> 
</bean>
这将像spring按类型注入bean一样工作。 但是,如果您正在创建2个bean

<bean id ="spell1" class="com.tutorialspoint.SpellChecker">
 </bean>
<bean id ="spell2" class="com.tutorialspoint.SpellChecker">
 </bean>

这将不起作用,因为有两个相同类型的bean。您必须使用@qualifier.

@aurowired默认情况下自动按类型连接bean。如果你使用

<bean  class="com.tutorialspoint.SpellChecker"> 
</bean>
这将像spring按类型注入bean一样工作。 但是,如果您正在创建2个bean

<bean id ="spell1" class="com.tutorialspoint.SpellChecker">
 </bean>
<bean id ="spell2" class="com.tutorialspoint.SpellChecker">
 </bean>

这将不起作用,因为有两个相同类型的bean。您必须使用@qualifier.

org.springframework.beans.factory.beancreatitionException:创建名为“fascadebean”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动关联字段:private sms.spring.dao.StudentDaoImpl sms.spring.fascade.FascadeControllerImpl.StudentDaoImpl;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[sms.spring.dao.StudentDaoImpl]的符合条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件。依赖项批注:{@org.springframework.beans.factory.annotation.Autowiredrequired=truei有一个dao接口和一个用@service注释的实现类,设计模式是fascade,在FascadeImplic中,我声明了一个具有自动连接其属性的daoClass,并且出现了一个错误,如上所述我使用了上下文:组件扫描有任何可能的解决方案吗@shazinorg.springframework.beans.factory.BeanCreationException:创建名为“fascadebean”的bean时出错:自动关联依赖项注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动关联字段:private sms.spring.dao.studentdaimpl sms.spring.fascade.ControllerImpl.studentDaoImpl;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[sms.spring.dao.StudentDaoImpl]的符合条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件。依赖项批注:{@org.springframework.beans.factory.annotation.Autowiredrequired=truei有一个dao接口和一个用@service注释的实现类,设计模式是fascade,在FascadeImplic中,我声明了一个具有自动连接其属性的daoClass,并且出现了一个错误,如上所述我使用了上下文:组件扫描有任何可能的解决方案吗@shazinorg.springframework.beans.factory.BeanCreationException:创建名为“fascadebean”的bean时出错:自动关联依赖项注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动关联字段:private sms.spring.dao.studentdaimpl sms.spring.fascade.ControllerImpl.studentDaoImpl;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionExcept‌​离子:没有[sms.spring.dao.St]类型的合格bean
找到依赖项的udentDaoImpl]是否有任何可能的解决方案来消除此错误上下文:annotation config@Jiri tousek如何定义您的StudentDaoImpl实例?XML还是注释哪个注释?XML中是否同时存在注释配置和组件扫描?后者扫描的是什么包?我有一个dao接口和一个用服务注释的实现类,设计模式是fascade,在fascadeImpl中,我声明了一个具有自动连接其属性的daoClass,并且出现了一个错误,如上所述我使用了上下文:组件扫描是否有任何可能的解决方案@Jiri Tosekput所有相关代码作为对问题的编辑,这样我们就看不到您拥有什么了。org.springframework.beans.factory.beancreatitionException:创建名为“fascadebean”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动关联字段:private sms.spring.dao.StudentDaoImpl sms.spring.fascade.FascadeControllerImpl.StudentDaoImpl;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionException‌​ion:找不到[sms.spring.dao.StudentDaoImpl]类型的符合条件的bean作为依赖项:是否有任何可能的解决方案来消除此错误上下文:annotation config@Jiri TousekHow您的StudentDaoImpl实例是如何定义的?XML还是注释哪个注释?XML中是否同时存在注释配置和组件扫描?后者扫描的是什么包?我有一个dao接口和一个用服务注释的实现类,设计模式是fascade,在fascadeImpl中,我声明了一个具有自动连接其属性的daoClass,并且出现了一个错误,如上所述我使用了上下文:组件扫描是否有任何可能的解决方案@Jiri Tosekput所有相关代码作为对问题的编辑,这样我们就看不到你有什么了。