代码如下:<!-- ======================分割线 ====================== -->使用@Resource注解的PersonServiceBean
package com.spring_core.imp;import javax.annotation.Resource;import com.spring_core.dao.PersonDao;
import com.spring_core.inf.PersonService;public class PersonServiceBean implements PersonService
{
@Resource private PersonDao personDao;
public PersonServiceBean(){}
public void say()
{
personDao.add();
System.out.println("执行PersonServiceBean的say()方法!");
}
}<!-- ======================分割线 ====================== -->接口 PersonDao
package com.spring_core.dao;public interface PersonDao
{
public abstract void add();
}<!-- ======================分割线 ====================== -->接口 PersonDao的实现类: PersonDaoBean
package com.spring_core.imp;import com.spring_core.dao.PersonDao;public class PersonDaoBean implements PersonDao
{
public void add()
{
System.out.println("执行PersonDaoBean的add()方法!");
}
}<!-- ======================分割线 ====================== -->Sprint2.5配置文件beans.xml内容
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="personDao" class="com.spring_core.imp.PersonDaoBean" />
<bean id="personService" class="com.spring_core.imp.PersonServiceBean" />
</beans><!-- ======================分割线 ====================== -->测试代码
package com.spring_core.view;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lw.spring_core.inf.PersonService;public class Test
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
PersonService ps=(PersonService)ctx.getBean("personService");
ps.say();
}
}运行后抛出空指针异常
Exception in thread "main" java.lang.NullPointerException
at com.spring_core.imp.PersonServiceBean.say(PersonServiceBean.java:14)
at com.spring_core.view.Test.main(Test.java:15)这个例子很简单,可是我真的找不到问题出在哪里(我是照传智播客Spring教学录像写的,录像里面运行正常,我这里却抛异常)
求高人指点~!!
package com.spring_core.imp;import javax.annotation.Resource;import com.spring_core.dao.PersonDao;
import com.spring_core.inf.PersonService;public class PersonServiceBean implements PersonService
{
@Resource private PersonDao personDao;
public PersonServiceBean(){}
public void say()
{
personDao.add();
System.out.println("执行PersonServiceBean的say()方法!");
}
}<!-- ======================分割线 ====================== -->接口 PersonDao
package com.spring_core.dao;public interface PersonDao
{
public abstract void add();
}<!-- ======================分割线 ====================== -->接口 PersonDao的实现类: PersonDaoBean
package com.spring_core.imp;import com.spring_core.dao.PersonDao;public class PersonDaoBean implements PersonDao
{
public void add()
{
System.out.println("执行PersonDaoBean的add()方法!");
}
}<!-- ======================分割线 ====================== -->Sprint2.5配置文件beans.xml内容
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="personDao" class="com.spring_core.imp.PersonDaoBean" />
<bean id="personService" class="com.spring_core.imp.PersonServiceBean" />
</beans><!-- ======================分割线 ====================== -->测试代码
package com.spring_core.view;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lw.spring_core.inf.PersonService;public class Test
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
PersonService ps=(PersonService)ctx.getBean("personService");
ps.say();
}
}运行后抛出空指针异常
Exception in thread "main" java.lang.NullPointerException
at com.spring_core.imp.PersonServiceBean.say(PersonServiceBean.java:14)
at com.spring_core.view.Test.main(Test.java:15)这个例子很简单,可是我真的找不到问题出在哪里(我是照传智播客Spring教学录像写的,录像里面运行正常,我这里却抛异常)
求高人指点~!!
<bean id="personService" class="com.spring_core.imp.PersonServiceBean" />
改成
<bean id="personService" class="com.spring_core.imp.PersonServiceBean" >
<property name="personDao" ref="personDao"></property>
</bean>
这样试试,我没用过注解,建议你别从注解开始学spring
public PersonServiceBean(){}
public void say()
{
这里要写成:@Resource(name="personServiceBean") private PersonDao personDao;
既然配了注解,又没有配:
<context:annotation-config />
<context:component-scan base-package="包名" />
sping如何知道你到底是要通过xml,还是注解来注入呢?要么楼主就全部用注解:在personService/personDao类定义上写@component
然后在要注入的属性的set方法上写@resource即可!
假如要是要用xml的配置的话,就要协商ref。。等等东西!!
我又重新看了遍录像
确实是少了
<context:annotation-config />
添加之后,运行就正常了
有了<context:component-scan base-package="包名" />
就不用<context:annotation-config />了