Spring:No bean named 'beanScope' is defined

摘要:
初学Spring,“Nobeannamed'beanScope'isdefined”这个问题困扰了我好几个小时,查资料无果后,重写好几遍代码后发现问题居然是配置文件不能放在包里。。。使用的是  windows10/eclipse4.5.2/Spring-framework-4.3.0/下面是我的Spring学习代码:第一步:下载Spring的jar文件,传送门:http://repo.spring.io/milestone/org/springframework/找到想要的版本后点击下载第二步:去tomcat官网下载commonts-logging.jar文件,这是使用Spring必须的jar包,传送门:http://commons.apache.org/proper/commons-logging/因为我用了junit做测试,就也要去下载junit.jar啦,github里面就可以下载:https://github.com/junit-team/junit4/wiki/Download-and-Install,注意哦,在这个页面除了下载junit.jar还要下载hamcrest-core.jar包,同时加入项目才会生效。

初学Spring,“No bean named 'beanScope' is defined”这个问题困扰了我好几个小时,查资料无果后,重写好几遍代码后发现问题居然是配置文件不能放在包里。。。要放在src的直接目录下。。。心碎了一地。。。

使用的是  windows 10 / eclipse 4.5.2 /Spring-framework-4.3.0/

下面是我的Spring学习代码:

第一步:下载Spring的jar文件,传送门:http://repo.spring.io/milestone/org/springframework/找到想要的版本后点击下载

第二步:去tomcat官网下载commonts-logging.jar文件,这是使用Spring必须的jar包,传送门:http://commons.apache.org/proper/commons-logging/

因为我用了junit做测试,就也要去下载junit.jar啦,github里面就可以下载:https://github.com/junit-team/junit4/wiki/Download-and-Install,注意哦,在这个页面除了下载junit.jar还要下载hamcrest-core.jar包,同时加入项目才会生效。

第三步:导入相关jar包,我导入的有:

Spring:No bean named 'beanScope' is defined第1张

第四步:新建一个bean类,代码如下

packagecom.demo.bean;
public classBeanScope {
    public voidsay() {
        System.out.println("BeanScope say : " + this.hashCode());
    }
}

第五步:新建一个xml配置文件spring-beanscope,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
 <bean id="beanScope"class="com.demo.bean.BeanScope"scope="singleton"></bean> 
 </beans>

第六步:新建一个测试基类UnitTestBase,代码如下:

packagecom.demo.test.base;
importorg.junit.Before;
importorg.springframework.beans.BeansException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.util.StringUtils;
public classUnitTestBase {
    privateApplicationContext context;
    privateString springXmlpath;
    publicUnitTestBase() {}
    publicUnitTestBase(String springXmlpath) {
        this.springXmlpath =springXmlpath;
    }
    @Before
    public voidbefore() {
        if(StringUtils.isEmpty(springXmlpath)) {
            springXmlpath = "classpath*:spring-*.xml";
        }
        try{
            context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\s]+"));
        } catch(BeansException e) {
            e.printStackTrace();
        }
    }
    @SuppressWarnings("unchecked")
    protected <T extends Object>T getBean(String beanId) {
        try{
            return(T)context.getBean(beanId);
        } catch(BeansException e) {
            e.printStackTrace();
            return null;
        }
    }
    protected <T extends Object> T getBean(Class<T>clazz) {
        try{
            returncontext.getBean(clazz);
        } catch(BeansException e) {
            e.printStackTrace();
            return null;
        }
    }
}

第七步:新建一个测试类TestBeanScope,代码如下:

packagecom.demo.test.bean;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.junit.runners.BlockJUnit4ClassRunner;
importcom.demo.bean.BeanScope;
importcom.demo.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extendsUnitTestBase {
    publicTestBeanScope() {
        super("classpath*:spring-beanscope.xml");
    }
    @Test
    public voidtestSay() {
        BeanScope beanScope = super.getBean("beanScope");
        beanScope.say();
    }
}

最后执行成功!注意配置文件不能放在包里,要直接放在src目录下,否则会报错:No bean named 'beanScope' is defined

最后,当时这个问题困扰了我好久,搞得我搜了好多资料,发现了一个不错的Spring学习平台,推荐一下,哈哈哈:http://www.tutorialspoint.com/spring/index.htm

免责声明:文章转载自《Spring:No bean named 'beanScope' is defined》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇利用CSS三角形实现的冒泡框四十二、LAMP与LNMP web架构深度优化实战-第一部下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring Test 整合 JUnit 4 使用总结

转自:https://blog.csdn.net/hgffhh/article/details/83712924 这两天做Web开发,发现通过spring进行对象管理之后,做测试变得复杂了。因为所有的Bean都需要在applicationContext.xml中加载好,之后再通过@Resource去取得。如果每次都要整个业务流做的差不多了再去测试,这样效率...

泛型依赖注入出现required a single bean, but 2 were found

问题描述 项目使用了tkmybatis,然后在service层使用泛型抽取了公共的CRUD操作,结果在Controller层注入的时候提示 Field dao in com.gisquest.mmp.server.api.service.impl.BaseServiceImpl required a single bean, but 2 were foun...

[解决方法] spring-mongo mongodb 2.x 升级到 3.x 配置中出现的一些问题

问题1: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleDaoImpl': Injection of resource dependencies failed; nested exception is org.sprin...

消息队列之 ActiveMQ

简介 ActiveMQ 特点 ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效、可扩展、稳定、安全的企业级消息通信。 它的设计目标是提供标准的、面向消息的、多语言的应用集成消息通信中间件。ActiveMQ 实现了 JMS 1.1 并提供了很多附加的特性,比如 JMX 管理、主从管理、消息组通信、消息优先级、延迟接收...

对象Bean与Map互转问题

一、摘要 在实际开发过程中,经常碰到需要进行对象与map之间互转的问题,其实对于对象、Map 之间进行互转有很多种方式,下面我们一起来梳理一下: 利用 JSON 工具包,将对象转成字符串,之后再转成 Map,这种需要转换2次,相对来说效率比较底; 利用 Java 反射,获取 Bean 类的属性和值,再转换到 Map 对应的键值对中,相对来说这种方法效率高...

eclipse+maven+ssm框架搭建

eclipse+maven+ssm框架 0、系统环境 1)Windows 10 企业版 2)JDK 1.8.0_131 3)Eclipse Java EE IDE for Web Developers  Version: Neon.3 Release (4.6.3) 4)Tomcat 8.5 1、maven下载及配置 maven的下载地址:http:...