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)
楠宝宝_
carolcoral
循着那道光前行
相关分类