隐式的bean发现机制与自动装配后报空指针异常不知道错在哪求请教

package com.immoc.beanAutoWiring;


import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.stereotype.Component;


@Component

public class BeanAnnDemo {


@Autowired

private BeanAnnotation beanAnnotation;


public static void main(String[] args) {

//ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContent2.xml");

BeanAnnDemo demo=new BeanAnnDemo();

demo.test();

}


public  void test(){

beanAnnotation.myHashcode();

beanAnnotation.say("this a test");

}

}




package com.immoc.beanAutoWiring;


import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Component;


@Scope("prototype")

@Component

public class BeanAnnotation {


public void say(String string){


System.out.println("BeanAnnotation"+string);

}

public void myHashcode(){

System.out.println("BeanAnnotation"+this.hashCode());

}

}




<?xml version="1.0" encoding="UTF-8"?>

<!-- 指定Spring配置文件的DTD信息 -->

<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.immoc.beanAutoWiring"  />

</beans>


error:

Exception in thread "main" java.lang.NullPointerException

at com.immoc.beanAutoWiring.BeanAnnDemo.test(BeanAnnDemo.java:22)

at com.immoc.beanAutoWiring.BeanAnnDemo.main(BeanAnnDemo.java:18)



循着那道光前行
浏览 1483回答 3
3回答

楠宝宝_

你这种问题台笼统化,不贴代码错误信息啥的我们不可能帮的了你

carolcoral

                               <context:component-scan base-package="com.immoc"  />扫描所有同类型的包                             报错空指针是没找到

循着那道光前行

代码如上
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java