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

一个 Spring 的问题

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

    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     Solana     1172 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 19ms UTC 17:35 PVG 01:35 LAX 09:35 JFK 12:35
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86