错误为:
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();
}
}
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();
}
}
没有配置bean,所以取不到值吧
@Resource(name="personDaoBean ")
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
package service.imp1;
import org.springframework.stereotype.Repository;
import service.PersonDao;
@Repositorypublic class PersonDaoBean implements PersonDao {public void add(){
System.out.println("我被注入了");
}
}
@Repository不是让Spring建立么?
@Service("personService")
public class PersonServiceBean implements PersonService {
@Resource() //得用这样才能注入啊
private PersonDao personDao; public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
System.out.println("我被注入了");
}
}
@Repository不是让Spring建立么?@Repository 让Spring初始化这个bean,
但PersonServiceBean 要用到PersonDaoBean ,需要在PersonServiceBean 中注入PersonDaoBean @Resource() 把PersonDaoBean 注入PersonServiceBean