错误为:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
我被初始化了
Exception in thread "main" java.lang.NullPointerException
at service.imp1.PersonServiceBean.add(PersonServiceBean.java:40)
at SpringText.main(SpringText.java:20)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
"> 
<context:component-scan base-package="service"/>
</beans>
personDaoBean.java
package service.imp1;
import org.springframework.stereotype.Repository;
import service.PersonDao;
@Repository
public  class PersonDaoBean implements PersonDao {public void add(){
 System.out.println("我被注入了");
 }
}personServiceBean.java
package service.imp1;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Service;
import service.PersonDao;
import service.PersonService;
@Service("personService") 
public   class PersonServiceBean implements PersonService {
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@PostConstruct
 public void init()
 {
 System.out.println("我被初始化了");
 }
@PreDestroy
public void destroy()
{
System.out.println("关闭");
}public void add()
{
personDao.add();
}
}SpringText.javaimport org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.PersonService;
public class SpringText {
public static void setUpBeforeClass() throws Exception {
}
 public static void main(String[] args)
   {
  AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
 PersonService personService= (PersonService) ctx.getBean("personService");
 
 personService.add();  
  }
}

解决方案 »

  1.   

    PersonService personService= (PersonService) ctx.getBean("personService");
    没有配置bean,所以取不到值吧
      

  2.   

    你的personServiceBean中用到了 personDao,所以要set  PersonDao,你应当是没有把personDaoImpl注入到 personServiceImpl 所以在set的时候找不到 personDao,报了nullPionter异常。
      

  3.   

    你的Service层用到了dao层的东西,你没有把它注入进去,所以包了个空指针异常。
     @Resource(name="personDaoBean ")
     public void setPersonDao(PersonDao personDao) {
            this.personDao = personDao;
        }
      

  4.   

    5楼兄 这个
    package service.imp1;
    import org.springframework.stereotype.Repository;
    import service.PersonDao;
    @Repositorypublic  class PersonDaoBean implements PersonDao {public void add(){
         System.out.println("我被注入了");
     }
    }
    @Repository不是让Spring建立么?
      

  5.   

    确实没有在set方法上注入   好想在XML中还少了个配置 <context:annotation-config />PS:4楼三国杀陆逊头像没次看到都很帅~
      

  6.   


    @Service("personService") 
    public class PersonServiceBean implements PersonService {
        
        @Resource() //得用这样才能注入啊   
        private PersonDao personDao;    public PersonDao getPersonDao() {
            return personDao;
        }
        public void setPersonDao(PersonDao personDao) {
            this.personDao = personDao;
        }
      

  7.   

    正解@Repositorypublic class PersonDaoBean implements PersonDao {public void add(){
      System.out.println("我被注入了");
     }
    }
    @Repository不是让Spring建立么?@Repository 让Spring初始化这个bean,
    但PersonServiceBean 要用到PersonDaoBean ,需要在PersonServiceBean 中注入PersonDaoBean  @Resource() 把PersonDaoBean 注入PersonServiceBean