一维b5 2016-11-17 04:52 采纳率: 60%
浏览 4308

Spring无法获取ApplicationContext上下文对象怎么解决?

涉及到的类包括:
1. ITestService.java
2. TestServiceImp.java
3. autoWiringService.java
以及Spring配置文件:
4.autoWiring.xml

对应的源码为:
1. ITestService.java

 package com.z.test.annotation;

/**
 * Created by Z on 2016/11/16.
 */
public interface ITestService {
    public void annoTest();
}

2.TestServiceImp.java

 package com.z.test.annotation;
import org.springframework.stereotype.Service;

/**
 * Created by Z on 2016/11/16.
 * 如何引入注解
 */
@Service("testServiceImp")
public class TestServiceImp implements ITestService {
    public void annoTest(){
        System.out.println("注解调用");
    }
}

3.autoWiringService.java

 package com.z.test.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;

/**
 * Created by Z on 2016/11/16.
 */
public class AutoWiringService {

    @Resource(name ="testServiceImp")
    public  ITestService iTestService;

    public void autoTest(){
        iTestService.annoTest();
    }


    public static void main(String[] args){

        ApplicationContext context =new ClassPathXmlApplicationContext("autowiring.xml");
        AutoWiringService autoW=(AutoWiringService)context.getBean("autoWiringService");
        autoW.autoTest();

    }
}

使用了@Service 和 @Resource注解。目录结构:
图片说明

在AutoWiringService运行主方法进行测试时,总是有错误如下:
图片说明

猜测是否是无法读取xml文件?还是哪里没有配置好?

  • 写回答

3条回答 默认 最新

  • 一维b5 2016-11-17 04:54
    关注

    autoWiring.xml文件是这样配置的:

     <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cotent="http://www.springframework.org/schema/cache"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context
            ">
        <!--Bean的自动装配-->
        <!--<bean id="autoWiringDAO" class="com.z.autowiring.AutoWiringDAO" />-->
    
        <bean id="autoWiringService" class="com.z.test.annotation.AutoWiringService" />
    
        <!-- 扫描 哪些包或者类 需要用到注解 -->
        <context:component-scan base-package="com.z.test.annotation" />
        <!-- 使用注解 -->
        <cotent:annotation-driven/>
    
    </beans>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型