Spring:no Bean named ' beanscope ' is defined

Source: Internet
Author: User

Beginner spring, "No bean named ' Beanscope ' is defined" This problem bothered me for a few hours, check the data without fruit, rewrite several times after the code found that the problem is the configuration file can not be placed in the package ... To be placed in the direct directory of SRC ... A heart-rending place ...

Using the Windows 10/eclipse 4.5.2/spring-framework-4.3.0/

Here is my spring Learning code:

First step: Download spring jar file, Portal: http://repo.spring.io/milestone/org/springframework/find the desired version and click Download

Step two: Go to the Tomcat official website to download the Commonts-logging.jar file, which is the jar package that must be used with spring, the Portal: http://commons.apache.org/proper/commons-logging/

Because I used JUnit to do the test, I also want to download Junit.jar, github can download: Https://github.com/junit-team/junit4/wiki/Download-and-Install, Note Oh, in this page in addition to download Junit.jar also download the Hamcrest-core.jar package, and join the project will not take effect.

The third step: Import the relevant jar package, I have imported:

Fourth step: Create a new Bean class with the following code

 Package Com.demo.bean;  Public class Beanscope {    publicvoid  say () {        System.out.println (this . Hashcode ());}    }

Fifth step: Create a new XML configuration file Spring-beanscope with the following code:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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"  >         <BeanID= "Beanscope"class= "Com.demo.bean.BeanScope"Scope= "Singleton"></Bean>  </Beans>

Sixth step: Create a new test base class Unittestbase with the following code:

 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=NewClasspathxmlapplicationcontext (Springxmlpath.split ("[, \\s]+")); } Catch(beansexception e) {e.printstacktrace (); }} @SuppressWarnings ("Unchecked")    protected<textendsObject>T Getbean (String beanid) {Try {            return(T) Context.getbean (Beanid); } Catch(beansexception e) {e.printstacktrace (); return NULL; }    }        protected<textendsObject> T Getbean (class<t>clazz) {        Try {            returnContext.getbean (Clazz); } Catch(beansexception e) {e.printstacktrace (); return NULL; }    }}

Seventh Step: Create a new test class testbeanscope with the following code:

 PackageCom.demo.test.bean;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;ImportOrg.junit.runners.BlockJUnit4ClassRunner;ImportCom.demo.bean.BeanScope;Importcom.demo.test.base.UnitTestBase; the @RunWith (Blockjunit4classrunner.class) Public classTestbeanscopeextendsUnittestbase { PublicTestbeanscope () {Super("Classpath*:spring-beanscope.xml"); } @Test Public voidTestsay () {Beanscope Beanscope=Super. Getbean ("Beanscope");    Beanscope.say (); }}

Final execution Success! Note that the configuration file can not be placed in the package, to be placed directly in the SRC directory, or will be error: No bean named ' beanscope ' is defined

Finally, this problem bothered me for a long time, make me search a lot of information, found a good spring learning platform, recommended, hahaha: http://www.tutorialspoint.com/spring/index.htm

Spring:no Bean named ' beanscope ' is defined

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.