sping.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="JoAreaDao" class="com.joint.ddw.dao.impl.JoAreaDaoImpl">
<property name="template">
<ref bean="template"/>
</property>
</bean>
<bean id="proxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.joint.ddw.dao.JoAreaDao</value>
</property>
<property name="transactionManager">
<ref bean="TransactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="target">
<ref bean="JoAreaDao"/>
</property>
</bean>
<bean id="JoAreaServiceDao" class="com.joint.ddw.service.impl.JoAreaServiceDaoImpl">
<property name="dao">
<ref bean="proxyBean"/>
</property>
</bean>
</beans>被代理类 注入Template,
package com.joint.ddw.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.HibernateTemplate;import com.joint.ddw.dao.JoAreaDao;
import com.joint.ddw.orm.JoArea;public class JoAreaDaoImpl implements JoAreaDao {
private HibernateTemplate template;
public void delete(JoArea jo) throws Exception {
template.delete(jo); } public List findJoArea() throws Exception {
return template.find("from JoArea");
} public void insert(JoArea jo) throws Exception {
template.save(jo); } public void update(JoArea jo) throws Exception {
template.update(jo); } public HibernateTemplate getTemplate() {
return template;
} public void setTemplate(HibernateTemplate template) {
this.template = template;
}}把代理后的proxyBean注入到这类
package com.joint.ddw.service.impl;import java.util.List;import com.joint.ddw.dao.JoAreaDao;
import com.joint.ddw.orm.JoArea;
import com.joint.ddw.service.JoAreaServiceDao;public class JoAreaServiceDaoImpl implements JoAreaServiceDao {
private JoAreaDao dao;
public List find() throws Exception {
return dao.findJoArea(); 
} public void insert(JoArea jo) throws Exception {
if(jo==null){
throw new NullPointerException();

}
if(dao==null){
System.out.println("dao is null");

}
dao.insert(jo);
} public JoAreaDao getDao() {
return dao;
} public void setDao(JoAreaDao dao) {
this.dao = dao;
}}
Test测试类
package com.joint.ddw.service.impl;import com.joint.ddw.orm.JoArea;
import com.joint.ddw.service.JoAreaServiceDao;public class Test { /**
 * @param args
 */
public static void main(String[] args) {
JoAreaServiceDao dao=new JoAreaServiceDaoImpl();
JoArea jo=new JoArea();
jo.setAreaname("四川");
jo.setAreaid(5);
try {

dao.insert(jo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }}
错误页面
dao is null
java.lang.NullPointerException
at com.joint.ddw.service.impl.JoAreaServiceDaoImpl.insert(JoAreaServiceDaoImpl.java:24)
at com.joint.ddw.service.impl.Test.main(Test.java:18)

解决方案 »

  1.   

    你没有初始化容器并从容器里获取dao对象public class Test { 
    private static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
    public static void main(String[] args) { 
    JoAreaServiceDao dao= factory.getBean("JoAreaServiceDao");
    JoArea jo=new JoArea(); 
    jo.setAreaname("四川"); 
    jo.setAreaid(5); 
    try { dao.insert(jo); 
    } catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } } }