V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
mikicomo
V2EX  ›  问与答

一个 Spring 的问题

  •  
  •   mikicomo · 2017-05-15 18:37:43 +08:00 · 1683 次点击
    这是一个创建于 2531 天前的主题,其中的信息可能已经有所发展或是发生改变。

    QQ 图片 20170515182628.png

    如图,我用注解自动装配不了,报空指针异常,debug 值为 NULL 现在我只能用构造函数来手动装配曲线救国,这是什么问题导致的自动装配失败的?

    可能是哪里出的问题?

    8 条回复    2017-05-15 19:21:31 +08:00
    LaudOak
        1
    LaudOak  
       2017-05-15 18:42:12 +08:00 via Android
    姿势不对吧,Bean 那边怎么定义的
    mikicomo
        2
    mikicomo  
    OP
       2017-05-15 18:44:51 +08:00
    @LaudOak
    ```
    @Service("ItemIndexService")
    public class ItemIndexServiceImpl implements ItemIndexService {

    @Autowired
    private ItemIndexMapper itemindexmapper;


    @Transactional
    public ItemIndex selectById(int id) {
    return itemindexmapper.selectById(id);

    }

    @Transactional
    public List<ItemIndex> findAll() {

    List<ItemIndex> findAll = itemindexmapper.findAll();
    return findAll;
    }

    @Transactional
    public int deleteItemIndex(int id) {
    return itemindexmapper.deleteItemIndex(id);

    }

    @Transactional
    public int insertItemIndex(ItemIndex itemindex) {

    return itemindexmapper.insertItemIndex(itemindex);
    }

    @Transactional
    public int updateItemIndex(ItemIndex itemindex) {

    return itemindexmapper.updateItemIndex(itemindex);
    }

    }
    ```
    acrisliu
        3
    acrisliu  
       2017-05-15 18:53:34 +08:00 via Android
    你这个类是 spring 组件么?加了 @Component 注解没
    mikicomo
        4
    mikicomo  
    OP
       2017-05-15 18:58:26 +08:00
    @acrisliu
    ```
    public class ExcelDataPackage {

    @Autowired
    @Qualifier("ItemIndexService")
    private ItemIndexService itemIndexService;

    @Autowired
    @Qualifier("ItemDetailService")
    private ItemDetailService itemDetailService;

    private List<ItemIndex> itemIndices;

    public int findIndexCache(ItemIndex index){
    }

    public void bindObject(){

    }

    public void insertObject(){

    }


    }

    ```

    我没有加 @Component 注解,我这里只要装配就好了,本身不需要作为一个组件来使用,不用加 @Component 的吧?
    而且我加了也依旧装不上
    stackboom
        5
    stackboom  
       2017-05-15 19:06:53 +08:00
    这个类 你 new 出来的吧 。
    mikicomo
        6
    mikicomo  
    OP
       2017-05-15 19:12:47 +08:00
    @stackboom
    卧槽...是的....我为了方便在 test 里面直接 psvm 里面 new 了,应该是要在 @Controller 或者别的 Compant 下面让 Spring 再把我这个类本身也装配进来是吧》。。。。
    stackboom
        7
    stackboom  
       2017-05-15 19:16:05 +08:00   ❤️ 1
    @mikicomo #6 test 使用 spring test+ junit 也可以注入, 如果你是 psvm 从 spring 容器取吧。这样 spring
    才会帮你注入。
    mikicomo
        8
    mikicomo  
    OP
       2017-05-15 19:21:31 +08:00
    @stackboom 嗯,还是在思维方式上还没有交给容器来管理这样的思维习惯。。。搞了好几个小时这个问题,谢谢大兄弟了!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2871 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 196ms · UTC 13:41 · PVG 21:41 · LAX 06:41 · JFK 09:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.