zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    BZOJ 3709: [PA2014]Bohater
    BZOJ 3689: 异或之
    BZOJ 4385: [POI2015]Wilcze doły
    2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017) Solution
    Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3) Solution
    BZOJ 4320: ShangHai2006 Homework
    BZOJ 4318: OSU!
    2016-2017 ACM-ICPC CHINA-Final Solution
    BZOJ 5312: 冒险
    Codeforces Round #520 (Div. 2) Solution
  • 原文地址:https://www.cnblogs.com/miaoying/p/5793490.html
Copyright © 2011-2022 走看看