mysql - 关于spring,mybatis接口自动注入的问题
巴扎黑
巴扎黑 2017-04-17 13:25:12
[MySQL讨论组]

新手求助,各位,我在搭建一个spring,mybatis,mySQL的jar应用环境。但是遇见了接口无法自动注入的问题,求助!
1,jar应用的main函数

public class Main {
    public static void main(String[] args) {
//这里加载sping环境
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "ApplicationContext.xml");
//这里就是调用数据库
        ItemService itemService = new ItemService();
        List list = new ArrayList();
        list = itemService.getItemListWithWebnameAndChanneltitle("1","1");
        System.out.println(list.size());
    }

2,spring配置

?xml version="1.0" encoding="UTF-8"?>


    
    

    
    
        
            
                classpath:RSSAgent.properties
            
        
        
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
    

    
        
        
    

    
        
    
    
    

3,SqlMapConfig.xml




    
        
        
        
        
        
        
    

4,mapper.xml




    
        
            
            
            
            
            
            
            
            
        
    

    

5,ItemRepo.class

@Repository("itemRepo")
public interface ItemRepo {
    public List getItemListWithWebnameAndChanneltitle(String webName,String channelTitle);

}

6,ItemService.class

@Service
public class ItemService {

    @Resource(name="itemRepo")
    private ItemRepo itemRepo;

    public List getItemListWithWebnameAndChanneltitle(String webName,String channelTitle){
        List list = new ArrayList();
        list = itemRepo.getItemListWithWebnameAndChanneltitle(webName,channelTitle);
        return list;
    }
}

7,我的jar包

aopalliance.jar
aspectjweaver-1.6.jar
c3p0-0.9.1.jar
cglib-nodep-2.2.2.jar
commons-beanutils.jar
commons-codec.jar
commons-collections-3.2.1.jar
commons-dbcp-1.4.jar
commons-digester.jar
commons-fileupload-1.2.jar
commons-httpclient-3.0.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-logging-1.1.3.jar
commons-pool-1.4.jar
fastjson-1.1.26.jar
fluent-hc-4.3.1.jar
guava-16.0.1.jar
httpclient-4.3.6.jar
httpclient-cache-4.3.1.jar
httpcore-4.3.jar
httpcore-nio-4.3-beta2.jar
httpmime-4.3.6.jar
javassist-3.16.1-GA.jar
jdom-1.1.jar
joda-time-2.4.jar
jstl-1.2.jar
junit-4.8.2.jar
log4j-1.2.15.jar
mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.21.jar
rome-1.0.jar
servlet-api.jar
slf4j-api-2.0.99.jar
slf4j-log4j12-1.6.2.jar
spring-aop-4.0.5.RELEASE.jar
spring-aspects-4.0.5.RELEASE.jar
spring-beans-4.0.5.RELEASE.jar
spring-context-4.0.5.RELEASE.jar
spring-context-support-4.0.5.RELEASE.jar
spring-core-4.0.5.RELEASE.jar
spring-expression-4.0.5.RELEASE.jar
spring-jdbc-4.0.5.RELEASE.jar
spring-orm-4.0.5.RELEASE.jar
spring-tx-4.0.5.RELEASE.jar
spring-web-4.0.5.RELEASE.jar
spring-webmvc-4.0.5.RELEASE.jar

当运行的结果:

[10/25 02:25:25] [INFO] ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c1b77f8: startup date [Sun Oct 25 02:25:25 CST 2015]; root of context hierarchy
[10/25 02:25:25] [INFO] XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [ApplicationContext.xml]
[10/25 02:25:26] [INFO] PropertyPlaceholderConfigurer: Loading properties file from class path resource [RSSAgent.properties]
Exception in thread "main" java.lang.NullPointerException
    at com.zdsc.rssagent.service.cyfxb.ItemService.getItemListWithWebnameAndChanneltitle(ItemService.java:24)
    at com.zdsc.rssagent.Main.main(Main.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我debug的时候发现的问题是在

@Service
public class ItemService {

    @Resource(name="itemRepo")
    private ItemRepo itemRepo;

    public List getItemListWithWebnameAndChanneltitle(String webName,String channelTitle){
        List list = new ArrayList();
        list = itemRepo.getItemListWithWebnameAndChanneltitle(webName,channelTitle);
        return list;
    }
}

这里itemRepo是null,说明没有自动注入进来。
再次请问各位大虾,我的配置哪里有错误,或者我的使用方式是不是有误,我知道spring和mybatis是还有其他方式可以结合的,但是为什么别人可以使用的自动扫描接口的方式,我为什么不能,所以我还是希望能够时候配置出来,恳请各位帮忙了。

巴扎黑
巴扎黑

全部回复(2)
ringa_lee

其他代码没看,只看了【1,jar应用的main函数】就有问题。

ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
ItemService itemService = new ItemService();

ItemService 你居然用的是new!!!要是让你new出来还用Spring干嘛。也就是整个项目中没有用到Spring。你用Spring就是为了让Spring帮你new而不是自己new。

这样才对:

ItemService itemService = ctx.getBean(ItemService.class);
高洛峰

你的ItemRepo的实现呢?代码里给出的是interface。

@Reporitory加到ItemRepo的实现上面。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号