Spring:No bean named 'beanScope' is defined
初学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的更多相关文章
- 隐蔽的bean没有定义错误:No bean named 'SysJdTypeServiceImpl' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'SysJdTypeServiceImpl ...
- CXF和spring整合遇到的问题:No bean named 'cxf' is defined
今天在做ws和spring整合的时候,很不幸的遇到了这个问题,百度了好久,竟然没人遇到这个问题,后来谷歌了一下,都是遇到这个问题的了...在看到一篇文章中提到了cxf.xml,所以我果断的打开这个配置 ...
- Spring Security使用报错 No bean named 'springSecurityFilterChain' is defined
今天配置spring security时,运行报出No bean named 'springSecurityFilterChain' is defined错误,报错信息如下 严重: Exception ...
- spring集成shiro报错解决(no bean named 'shiroFilter' is defined)
引言: 本人在使用spring集成shiro是总是报“no bean named 'shiroFilter' is defined”,网上的所有方式挨个试了一遍,又检查了一遍, 还是没有解决,最后,抱 ...
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享出现:No bean named 'springSessionRepositoryFilter' available
出现这个问题:No bean named 'springSessionRepositoryFilter' available的的原因: 1.Spring MVC加载了多个配置文件导致的,并不是版本问题 ...
- No bean named 'hibernateTemplate' is defined
1.错误描述 WARN:2015-05-01 15:42:22[localhost-startStop-1] - Exception encountered during context initia ...
- 解决org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined
在Spring配置文件中加入了支持注解,即<mvc:annotation-driven/> 重新启动服务器包 org.springframework.beans.factory.NoSuc ...
- 报错!!!!!!!!!!!org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSessionRepositoryFilter' is defined
报错!!!!!!!!!!! 因用maven项目不是很熟练,经常在Maven转Web项目(为什么要转web项目?因为要在tomcat中跑起来.maven项目好像是可以直接部署到tomcat的,或集成to ...
- No bean named 'springSecurityFilterChain' is defined
1.问题 本文讨论Spring安全配置问题 - 应用程序引导过程抛出以下异常: SEVERE: Exception starting filter springSecurityFilterChain ...
随机推荐
- PCL中point cloud的数据类型
出处: http://wiki.ros.org/pcl/Overview 1.数据类型 1.1 ROS中point cloud数据类型 sensor mesgs::PointCloud sensor ...
- ActiveXObject函数详解
什么是 ActiveX 控件? ActiveX 控件广泛用于 Internet.它们可以通过提供视频.动画内容等来增加浏览的乐趣.不过,这些程序可能出问题或者向您提供不需要的内容.在某些情况下,这些程 ...
- UITabBarController常见代理方法的使用
//下面两个函数是成对出现的若第一个函数返回YES那么第二个方法自动触发执行 //是否允许选择不同的Item触发后续操作,YES是允许,NO是不允许 -(BOOL)tabBarController:( ...
- meta-analysis 到底是什么个意思类?
背景 科学研究应建立于许多实验结果的重复之上,除了少数新发现外,单个实验结果很难对科学的发展作出极为显著的贡献.所以为了阐明某一主题,在许多科学领域有众多研究者在对不同的实验对象或对同一对象在不同的实 ...
- 关于 edittext 软键盘退出监听解决办法
edittext 有个onCreateInputConnection 的方法,通过它可以自定义一个输入法连接器,那里连接器里面有个方法(finishComposingText)能监听输入完成的动作. ...
- hdu4497 GCD and LCM ——素数分解+计数
link:http://acm.hdu.edu.cn/showproblem.php?pid=4497 如果G%L != 0,说明一定无解. 把K = G / L质数分解,G / L = p1^t1 ...
- dedecms recommend 注入 exp
我看没人用python写过发过 所以我就发一下 喜欢用python的就用我这个吧 不喜欢的就用JAR那个或者PHP那个吧 #coding:GBK import re import urllib &q ...
- Discovering the Computer Science Behind Postgres Indexes
This is the last in a series of Postgres posts that Pat Shaughnessy wrote based on his presentation ...
- MongoDB快速上手
1. MongoDB简介 MongoDB是一个跨平台的基于Key_Value键值对形式保存数据的NoSQL文档类型数据库. NoSQL(not only sql)数据库,泛指非关系型数据库. 1.1 ...
- UILabel的使用
UILabel是iOS用于显示文本的控件. 基本属性如下代码所示: UILabel *lbl = [[UILabel alloc] init] ; //设置显示区域 lbl.frame = CGRec ...