我刚刚使用spring + hibernate编写了一个简单的测试,但我在此测试中得到了一个空指针异常,因为sudo /etc/init.d/celerybeat stop
+ DEFAULT_PID_FILE=/var/run/celerybeat.pid
+ DEFAULT_LOG_FILE=/var/log/celerybeat.log
+ DEFAULT_LOG_LEVEL=INFO
+ DEFAULT_CELERYBEAT=celerybeat
+ test -f /etc/default/celeryd
+ . /etc/default/celeryd
++ CELERYD_NODES=w1
++ CELERYD_OPTS='--time-limit=7200 -E'
++ CELERYD_CHDIR=/home/ubuntu/myexamplewebsite/mywebsite
++ CELERYD_MULTI='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celeryd_multi'
++ CELERYD_LOG_FILE=/var/log/celery/%n.log
++ CELERYD_PID_FILE=/var/run/celery/%n.pid
++ CELERYD_USER=www-data
++ CELERYD_GROUP=www-data
++ CELERYBEAT_CHDIR=/home/ubuntu/myexamplewebsite/mywebsite
++ CELERYBEAT='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celerybeat'
++ CELERYBEAT_OPTS='-S djcelery.schedulers.DatabaseScheduler'
++ CELERYBEAT_LOG_LEVEL=INFO
++ CELERYBEAT_USER=www-data
++ CELERYBEAT_GROUP=www-data
++ CELERYBEAT_LOG_FILE=/var/log/celery/beat.log
++ CELERYBEAT_PID_FILE=/var/run/celery/beat.pid
++ CELERYEV='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celeryev'
++ CELERYEV_USER=www-data
++ CELERYEV_GROUP=www-data
++ CELERYEV_CAM=djcelery.snapshot.Camera
++ CELERYEV_LOG_FILE=/var/log/celery/evcam.log
++ CELERYEV_PID_FILE=/var/run/celery/evcam.pid
+ test -f /etc/default/celerybeat
+ CELERYBEAT='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celerybeat'
+ CELERYBEAT_PID_FILE=/var/run/celery/beat.pid
+ CELERYBEAT_LOG_FILE=/var/log/celery/beat.log
+ CELERYBEAT_LOG_LEVEL=INFO
+ export CELERY_LOADER
+ CELERYBEAT_OPTS='-S djcelery.schedulers.DatabaseScheduler -f /var/log/celery/beat.log -l INFO'
+ '[' -n '' ']'
++ dirname /var/log/celery/beat.log
+ CELERYBEAT_LOG_DIR=/var/log/celery
++ dirname /var/run/celery/beat.pid
+ CELERYBEAT_PID_DIR=/var/run/celery
+ '[' '!' -d /var/log/celery ']'
+ '[' '!' -d /var/run/celery ']'
+ '[' -n www-data ']'
+ DAEMON_OPTS=' --uid www-data'
+ chown www-data /var/log/celery /var/run/celery
+ '[' -n www-data ']'
+ DAEMON_OPTS=' --uid www-data --gid www-data'
+ chgrp www-data /var/log/celery /var/run/celery
+ CELERYBEAT_CHDIR=/home/ubuntu/myexamplewebsite/mywebsite
+ '[' -n /home/ubuntu/myexamplewebsite/mywebsite ']'
+ DAEMON_OPTS=' --uid www-data --gid www-data --workdir /home/ubuntu/myexamplewebsite/mywebsite'
+ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/sbin
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/sbin
+ case "$1" in
+ check_paths
++ dirname /var/run/celery/beat.pid
+ ensure_dir /var/run/celery
+ '[' -d /var/run/celery ']'
+ mkdir -p /var/run/celery
+ chown www-data:www-data /var/run/celery
+ chmod 02755 /var/run/celery
++ dirname /var/log/celery/beat.log
+ ensure_dir /var/log/celery
+ '[' -d /var/log/celery ']'
+ mkdir -p /var/log/celery
+ chown www-data:www-data /var/log/celery
+ chmod 02755 /var/log/celery
+ stop_beat
+ echo -n 'Stopping celerybeat... '
Stopping celerybeat... + '[' -f /var/run/celery/beat.pid ']'
++ cat /var/run/celery/beat.pid
+ wait_pid 3691
+ pid=3691
+ forever=1
+ i=0
+ '[' 1 -gt 0 ']'
+ kill -0 3691
+ '[' 0 -eq 1 ']'
+ kill -TERM 3691
+ i=1
+ '[' 1 -gt 60 ']'
+ sleep 0.5
+ '[' 1 -gt 0 ']'
+ kill -0 3691
+ '[' 1 -eq 1 ']'
+ echo OK
OK
+ forever=0
+ '[' 0 -gt 0 ']'
+ set +x
sudo /etc/init.d/celerybeat start
+ DEFAULT_PID_FILE=/var/run/celerybeat.pid
+ DEFAULT_LOG_FILE=/var/log/celerybeat.log
+ DEFAULT_LOG_LEVEL=INFO
+ DEFAULT_CELERYBEAT=celerybeat
+ test -f /etc/default/celeryd
+ . /etc/default/celeryd
++ CELERYD_NODES=w1
++ CELERYD_OPTS='--time-limit=7200 -E'
++ CELERYD_CHDIR=/home/ubuntu/myexamplewebsite/mywebsite
++ CELERYD_MULTI='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celeryd_multi'
++ CELERYD_LOG_FILE=/var/log/celery/%n.log
++ CELERYD_PID_FILE=/var/run/celery/%n.pid
++ CELERYD_USER=www-data
++ CELERYD_GROUP=www-data
++ CELERYBEAT_CHDIR=/home/ubuntu/myexamplewebsite/mywebsite
++ CELERYBEAT='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celerybeat'
++ CELERYBEAT_OPTS='-S djcelery.schedulers.DatabaseScheduler'
++ CELERYBEAT_LOG_LEVEL=INFO
++ CELERYBEAT_USER=www-data
++ CELERYBEAT_GROUP=www-data
++ CELERYBEAT_LOG_FILE=/var/log/celery/beat.log
++ CELERYBEAT_PID_FILE=/var/run/celery/beat.pid
++ CELERYEV='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celeryev'
++ CELERYEV_USER=www-data
++ CELERYEV_GROUP=www-data
++ CELERYEV_CAM=djcelery.snapshot.Camera
++ CELERYEV_LOG_FILE=/var/log/celery/evcam.log
++ CELERYEV_PID_FILE=/var/run/celery/evcam.pid
+ test -f /etc/default/celerybeat
+ CELERYBEAT='/home/ubuntu/myexamplewebsite/mywebsite/manage.py celerybeat'
+ CELERYBEAT_PID_FILE=/var/run/celery/beat.pid
+ CELERYBEAT_LOG_FILE=/var/log/celery/beat.log
+ CELERYBEAT_LOG_LEVEL=INFO
+ export CELERY_LOADER
+ CELERYBEAT_OPTS='-S djcelery.schedulers.DatabaseScheduler -f /var/log/celery/beat.log -l INFO'
+ '[' -n '' ']'
++ dirname /var/log/celery/beat.log
+ CELERYBEAT_LOG_DIR=/var/log/celery
++ dirname /var/run/celery/beat.pid
+ CELERYBEAT_PID_DIR=/var/run/celery
+ '[' '!' -d /var/log/celery ']'
+ '[' '!' -d /var/run/celery ']'
+ '[' -n www-data ']'
+ DAEMON_OPTS=' --uid www-data'
+ chown www-data /var/log/celery /var/run/celery
+ '[' -n www-data ']'
+ DAEMON_OPTS=' --uid www-data --gid www-data'
+ chgrp www-data /var/log/celery /var/run/celery
+ CELERYBEAT_CHDIR=/home/ubuntu/myexamplewebsite/mywebsite
+ '[' -n /home/ubuntu/myexamplewebsite/mywebsite ']'
+ DAEMON_OPTS=' --uid www-data --gid www-data --workdir /home/ubuntu/myexamplewebsite/mywebsite'
+ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/sbin
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/sbin
+ case "$1" in
+ check_dev_null
+ '[' '!' -c /dev/null ']'
+ check_paths
++ dirname /var/run/celery/beat.pid
+ ensure_dir /var/run/celery
+ '[' -d /var/run/celery ']'
+ mkdir -p /var/run/celery
+ chown www-data:www-data /var/run/celery
+ chmod 02755 /var/run/celery
++ dirname /var/log/celery/beat.log
+ ensure_dir /var/log/celery
+ '[' -d /var/log/celery ']'
+ mkdir -p /var/log/celery
+ chown www-data:www-data /var/log/celery
+ chmod 02755 /var/log/celery
+ start_beat
+ echo 'Starting celerybeat...'
Starting celerybeat...
+ '[' -n '' ']'
+ /home/ubuntu/myexamplewebsite/mywebsite/manage.py celerybeat -S djcelery.schedulers.DatabaseScheduler -f /var/log/celery/beat.log -l INFO --uid www-data --gid www-data -- --pidfile=/var/run/celery/beat.pid
Overriding with Production settings...
+ set +x
始终为null。然后我尝试调用方法sessionFactory
来获取getBean()
。我发现它有效,但sessionFactory
没有。为什么呢?
@autowired
感谢任何帮助;感谢。
以下是相关文件的内容:
这是我的private ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
protected SessionFactory sessionFactory=(SessionFactory)applicationContext.getBean("sessionFactory");
applicationContext.xml
这是我的测试<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
<!-- <bean id="..." class="..."> collaborators and configuration for this
bean go here </bean> <bean id="..." class="..."> collaborators and configuration
for this bean go here </bean> more bean definitions go here -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/desserthouse?autoReconnect=true" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="edu.nju.dessertHouse.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="autowiredAnnotationBeanPostProcessor"
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="edu.nju.dessertHouse" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*User" propagation="REQUIRED" />
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution( * edu.nju.dessertHouse.service..*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
:
MemberDaoImpl
}
为什么@Repository
public class MemberDaoImpl implements MemberDao {
@Autowired
protected SessionFactory sessionFactory;
protected Session session;
protected static MemberDaoImpl memberDao=new MemberDaoImpl();
public Session getSession(){
return sessionFactory.openSession();
}
@Override
public void resigter(Member member) {
try {
session=getSession();
Transaction tx=session.beginTransaction();
session.save(member);
tx.commit();
session.close();
sessionFactory.close();
System.out.println("ok");
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
Member member=new Member();
member.setMemberid(1);
member.setCreditCard("1");
member.setMemberCard("1");
member.setLocation("1");
member.setMname("1");
member.setPassword("1");
member.setSex("1");
member.setPoint(1);
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.YEAR,1995);
calendar.set(Calendar.MONTH,7-1);
calendar.set(Calendar.DAY_OF_MONTH,6);
Date date=new Date(calendar.getTimeInMillis());
member.setBirthday(date);
memberDao.resigter(member);
}
为空?我尝试了很多方法,但它仍然无法正常工作。
答案 0 :(得分:0)
您正在通过以下方式创建MemberDAOImpl
的新实例:
protected static MemberDaoImpl memberDao=new MemberDaoImpl();
然后,您通过执行register()
在此新实例上调用memberDao.resigter(member);
。
您认为Spring Framework如何运作?
您不应该创建MemberDAOImpl
的新实例而只是register(member);
而不是memberDao.resigter(member);
,因为您已经通过{{1}注释它来创建单个MemberDAOImpl
}}。这注入了@Repository
Bean。