搭建SSH开发框架时autowired注入为空的问题

    最近在搭建SSH框架,使用@Autowired自动装配时,需要注入的对象是空,表明注入失败。

    尝试了很多次还是不行,现在感觉可能是spring没配置好,所以类里的注解写对也注入不进去。但是从log4j的debug信息里看到spring已经把需要注入的类给找到了。
这部分日志如下:

[QC] DEBUG [main] org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(217) | Creating shared instance of singleton bean 'userServiceImpl'
[QC] DEBUG [main] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(430) | Creating instance of bean 'userServiceImpl'
[QC] DEBUG [main] org.springframework.beans.factory.annotation.InjectionMetadata.<init>(60) | Found injected element on class [com.whk.service.impl.UserServiceImpl]: AutowiredFieldElement for private com.whk.dao.UserDao com.whk.service.impl.UserServiceImpl.userDao
[QC] DEBUG [main] org.springframework.beans.factory.annotation.InjectionMetadata.<init>(60) | Found injected element on class [com.whk.service.impl.UserServiceImpl]: AutowiredFieldElement for private com.whk.dao.UserDao com.whk.service.impl.UserServiceImpl.userDao
[QC] DEBUG [main] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(504) | Eagerly caching bean 'userServiceImpl' to allow for resolving potential circular references
[QC] DEBUG [main] org.springframework.beans.CachedIntrospectionResults.<init>(222) | Getting BeanInfo for class [com.whk.service.impl.UserServiceImpl]
[QC] DEBUG [main] org.springframework.beans.CachedIntrospectionResults.<init>(238) | Caching PropertyDescriptors for class [com.whk.service.impl.UserServiceImpl]
[QC] DEBUG [main] org.springframework.beans.CachedIntrospectionResults.<init>(250) | Found bean property 'class' of type [java.lang.Class]
[QC] DEBUG [main] org.springframework.beans.factory.annotation.InjectionMetadata.inject(85) | Processing injected method of bean 'userServiceImpl': AutowiredFieldElement for private com.whk.dao.UserDao com.whk.service.impl.UserServiceImpl.userDao

    从这里是不是可以得出结论,bean已经生成了,但是没有成功注入给接口?

    现在发现一个问题。struts2的action是每次访问时都会重新实例化的。我在Action,Service的实现类,Dao的实现类中都加入构造函数,在构造函数中用语句在控制台上输出类名,结果发现,在项目启动时,Action,Service的实现类,Dao的实现类都实例化了,然后再访问action,结果只有action进行了实例化。但是,本身service和dao就是单例的,没有实例化也不能说明什么吧。

    那会不会是各种包版本的问题?一开始下的maven版本比较新,所以用了JDK7。把JDK换成1.6,还是注入不进去。

    既然不是版本的问题,配置也没有问题,那是什么问题?要疯了。

    又检查了一遍包,发现居然没有struts2-spring-plugin。突然想起来,当初在跟着一个博客配maven+ssh时,有一步把这个依赖给注释了。去pom.xml文件中看,果然如此。把注释去掉,注入成功。

    这个问题的原因虽然很奇葩,但是最后解决了就好。


转载于:https://my.oschina.net/wanghongkai/blog/527084

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值