package com.model;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
}
package com.dao;
import com.model.User;
public interface UserDAO {
public void save(User user);
}
package com.imp;
import com.dao.UserDAO;
import com.model.User;
public class UserDAOimp implements UserDAO{
public void save ( User user) {
System.out.println("user is saved");
}
}
package com.service;
import com.dao.UserDAO;
import com.model.User;
public class UserService {
private UserDAO userDAO;
public void add(User user){
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
package com.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.User;
public class UserServiceTest {
public static void main(String[] args) {
ApplicationContext acx = new ClassPathXmlApplicationContext("bean.xml");
UserService u = (UserService) acx.getBean("userService");
u.add(new User());
}
}
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:annotation-config/>
<context:component-scan base-package="com"/>
<aop:aspectj-autoproxy/>
<bean id="log" class="com.aop.Log"/>
<bean id="userService" class="com.service.UserService"/>
</beans>
jar 包:commons-logging-1.1.1.jar aopalliance-1.0.jar aspectjrt-1.5.0.jar aspectjweaver-1.5.0.jar
报错:
七月 09, 2012 10:01:58 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f32ca2b: startup date [Mon Jul 09 22:01:58 CST 2012]; root of context hierarchy
七月 09, 2012 10:01:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
七月 09, 2012 10:01:58 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'log': replacing [Generic bean: class [com.aop.Log]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\myeclipse10\MyEclipse 10 workspace\Spring\bin\com\aop\Log.class]] with [Generic bean: class [com.aop.Log]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [bean.xml]]
七月 09, 2012 10:01:58 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@57a40a9f: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,log,org.springframework.aop.config.internalAutoProxyCreator,userService,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
Exception in thread "main" java.lang.NullPointerException
at com.service.UserService.add(UserService.java:11)
at com.service.UserServiceTest.main(UserServiceTest.java:14)
5 个解决方案
#1
你的UserDAO 没注入到UserService里面
所在UserService里面UserDAO 对象还是没初始化,为null的,那么后面这样子调用userDAO.save(user);就会出错了
所在UserService里面UserDAO 对象还是没初始化,为null的,那么后面这样子调用userDAO.save(user);就会出错了
#2
有了
<context:component-scan base-package="com"/>
就不需要
<context:annotation-config/>
不知道是不是这个原因。
<context:component-scan base-package="com"/>
就不需要
<context:annotation-config/>
不知道是不是这个原因。
#3
没依赖注入
#4
那应该怎么修改呢?
#5
遇到同样问题了,请问怎么解决呀????弄了一个晚上了,毫无头绪,急求解决方案
#1
你的UserDAO 没注入到UserService里面
所在UserService里面UserDAO 对象还是没初始化,为null的,那么后面这样子调用userDAO.save(user);就会出错了
所在UserService里面UserDAO 对象还是没初始化,为null的,那么后面这样子调用userDAO.save(user);就会出错了
#2
有了
<context:component-scan base-package="com"/>
就不需要
<context:annotation-config/>
不知道是不是这个原因。
<context:component-scan base-package="com"/>
就不需要
<context:annotation-config/>
不知道是不是这个原因。
#3
没依赖注入
#4
那应该怎么修改呢?
#5
遇到同样问题了,请问怎么解决呀????弄了一个晚上了,毫无头绪,急求解决方案