Spring是如何将service注入到Action中的?

代码在这个链接上Maven搭建SSH框架
可以看到的是,Spring并没有明确对Struts2的Action进行明确的实例化,我在Action里面也查找了一下Bean的名字,发现也没有Action的存在,那么Spring是如何对Action中的Service进行注入的?
这是我找到的Bean的名字,是用网上的那种找Bean的方式找的,代码太多,链接在这里:链接

org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0
flowBasicInfoService
flowBasicInfoDao
jobInfoDao
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalPersistenceAnnotationProcessor
dataSource
sessionFactory
transactionManager
transactionAdvice
org.springframework.aop.config.internalAutoProxyCreator
transactionPointcut
org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor

大概这里的@autowire是由struts2识别的。
问题关闭。

阅读 4.4k
1 个回答

注解本身没有意义,是需要通过一些机制来识别它的。那么在这里就是由Spring来对注解进行识别,其作用就是变成我们所谓的可注入的Bean。
@Autowired是用于将Bean注入的注解,那么意味Spring的扫描出来的Bean中必须有你这个Bean,不然就没法注入啦。于是我们在代码里可以看到:

private UserInfoService userInfoService;

接下来看一下UserInfoService。嗯,符合规范,这是一个接口。UserInfoServiceImpl实现了这个接口,并且添加了注解@Service("userInfoService") ,那么这个注解就是作为了被Spring扫描的“根据”。

有兴趣的话,你可以试试去掉这个注解,看看会发生什么。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题