一维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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料