初学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包,我导入的有:

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

package com.demo.bean;

public class BeanScope {
public void say() {
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,代码如下:

package com.demo.test.base;

import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils; public class UnitTestBase { private ApplicationContext context; private String springXmlpath; public UnitTestBase() {} public UnitTestBase(String springXmlpath) {
this.springXmlpath = springXmlpath;
} @Before
public void before() {
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 {
return context.getBean(clazz);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
} }

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

package com.demo.test.bean;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.demo.bean.BeanScope;
import com.demo.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase { public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
} @Test
public void testSay() {
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的更多相关文章

  1. 隐蔽的bean没有定义错误:No bean named 'SysJdTypeServiceImpl' is defined

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'SysJdTypeServiceImpl ...

  2. CXF和spring整合遇到的问题:No bean named 'cxf' is defined

    今天在做ws和spring整合的时候,很不幸的遇到了这个问题,百度了好久,竟然没人遇到这个问题,后来谷歌了一下,都是遇到这个问题的了...在看到一篇文章中提到了cxf.xml,所以我果断的打开这个配置 ...

  3. Spring Security使用报错 No bean named 'springSecurityFilterChain' is defined

    今天配置spring security时,运行报出No bean named 'springSecurityFilterChain' is defined错误,报错信息如下 严重: Exception ...

  4. spring集成shiro报错解决(no bean named 'shiroFilter' is defined)

    引言: 本人在使用spring集成shiro是总是报“no bean named 'shiroFilter' is defined”,网上的所有方式挨个试了一遍,又检查了一遍, 还是没有解决,最后,抱 ...

  5. Spring MVC集成Spring Data Reids和Spring Session实现Session共享出现:No bean named 'springSessionRepositoryFilter' available

    出现这个问题:No bean named 'springSessionRepositoryFilter' available的的原因: 1.Spring MVC加载了多个配置文件导致的,并不是版本问题 ...

  6. No bean named 'hibernateTemplate' is defined

    1.错误描述 WARN:2015-05-01 15:42:22[localhost-startStop-1] - Exception encountered during context initia ...

  7. 解决org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined

    在Spring配置文件中加入了支持注解,即<mvc:annotation-driven/> 重新启动服务器包 org.springframework.beans.factory.NoSuc ...

  8. 报错!!!!!!!!!!!org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSessionRepositoryFilter' is defined

    报错!!!!!!!!!!! 因用maven项目不是很熟练,经常在Maven转Web项目(为什么要转web项目?因为要在tomcat中跑起来.maven项目好像是可以直接部署到tomcat的,或集成to ...

  9. No bean named 'springSecurityFilterChain' is defined

    1.问题 本文讨论Spring安全配置问题 - 应用程序引导过程抛出以下异常: SEVERE: Exception starting filter springSecurityFilterChain ...

随机推荐

  1. .net 获取AppDomain创建了多少

    partial class HttpBrowser { public static string IsolateCall(PageContentHandler pHandler) { Contract ...

  2. JVM-字节码执行引擎

    虚拟机字节码执行引擎 一.运行时栈帧结构 1.栈帧(Stack Frame)是用于支持虚拟机进行调用和方法执行的数据结构,它是虚拟机运行时数据区中的虚拟机栈(Virtual Method Stack) ...

  3. 基于NodeJS的全栈式开发

    前言 为了解决传统Web开发模式带来的各种问题,我们进行了许多尝试,但由于前/后端的物理鸿沟,尝试的方案都大同小异.痛定思痛,今天我们重新思考了“前后端”的定义,引入前端同学都熟悉的 NodeJS,试 ...

  4. 将事件绑定在html标签中和js动态绑定的区别

    一:绑定在标签中: 能够一眼看出那些元素绑定了什么事件. 只能将元素和事件逐一实现绑定. 二js动态绑定: 可以一次动态的给多个元素绑定事件,批量绑定事件. html标签绑定的缺点: ①:可能有时间差 ...

  5. 11-10 CC150第一章

    题目: 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can n ...

  6. NAT模式下用secureCRT连接虚拟机

    VMWare制作学习系统,或布置模拟网络群组环境,已经比较流行. 注意主机端口要设置成为不同2122,虚拟机端口设置成22 笔者为了给项目组同事,提供一个练习ssh远程连接操作,及方便抓图交流的环境, ...

  7. 循环数据forin,foreach,for of

    最近一直在对数据处理,循环匹配之类的,我的mentor也已经多次指出一个问题,就是在循环里面用if去判断一个数值是否存在的时候,只要找到就要退出,这个时候就要对循环的语法提出了要求: 1.for in ...

  8. datawindow 创建操作报表。

    最近制作做了几个简单的报表,写下主要的代码.以便各位参考. public void FillDW(DataTable dtbResult) { try { DataRow dtFr; ; , j = ...

  9. unity中全屏背景图缩放

    using UnityEngine; using System.Collections; public class BgPicScript : MonoBehaviour { // Use this ...

  10. Neo4j Index Notes

    Motivation GraphDatabasesBook: Robinson I., Webber J., Eifrem E. Graph Databases. 2013. 这本该是入门概念性质的书 ...