问答详情
源自:4-1 Spring Bean装配之Bean的定义及作用域的注解实现

为什么老是找不到bean的名字啊?一直提示报错

RT 就老是出现找不到bean的样子

http://img.mukewang.com/594aa3c0000132a905440249.jpg

http://img.mukewang.com/594aa3c000019cbc07750264.jpg

http://img.mukewang.com/594aa3c10001e30607480146.jpg

http://img.mukewang.com/594aa3c1000100bd07150035.jpg


提问者:er路 2017-06-22 00:51

个回答

  • er路
    2017-06-22 00:54:35

    //这个是java文件
    package com.imooc.beanannotation;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanAnnotation {
    	
    	public void say(String word){
    		System.out.println("BeanAnnotation:"+word);
    	}
    
    }
    //这个是配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    			http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    			http://www.springframework.org/schema/context
    			http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <context:component-scan base-package="com.imooc.beanannotation">
    </context:component-scan>		
    
    </beans>
    //这个是测试的文件,,我没安装老师那个方法,而是直接实例化
    @Test
    public void TestbeanAnnotation() {
            ApplicationContext alc=new ClassPathXmlApplicationContext("spring-beanannotation.xml");
    	BeanAnnotation bean=(BeanAnnotation)alc.getBean("beanAnnotation");
    	bean.say("fuck");
    		
    	}