Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧&x2B;Hibernate控制台应用程序_Java_Spring_Hibernate_Spring Mvc_Console Application - Fatal编程技术网

Java 弹簧&x2B;Hibernate控制台应用程序

Java 弹簧&x2B;Hibernate控制台应用程序,java,spring,hibernate,spring-mvc,console-application,Java,Spring,Hibernate,Spring Mvc,Console Application,一些史前史。。 我有一个用Spring和Hibernate编写的基于web的企业CRM系统。有许多任务需要系统地完成,例如提醒或电子邮件通知。。现在它被实现为一个单独的控制器,从cron调用它。除了一些任务非常“繁重”并且占用了大量Tomcat的资源之外,所有的工作都很好。所以我决定将它们分成不同的java控制台应用程序。为了使用相同的对象和服务,我将主项目拆分为单独的项目(库): 物体 刀 在主项目中,我刚刚将这些项目添加到构建路径中,这样我就可以毫无问题地使用所有对象和服务 现在我开始实现第

一些史前史。。 我有一个用Spring和Hibernate编写的基于web的企业CRM系统。有许多任务需要系统地完成,例如提醒或电子邮件通知。。现在它被实现为一个单独的控制器,从cron调用它。除了一些任务非常“繁重”并且占用了大量Tomcat的资源之外,所有的工作都很好。所以我决定将它们分成不同的java控制台应用程序。为了使用相同的对象和服务,我将主项目拆分为单独的项目(库):

  • 物体
  • 在主项目中,我刚刚将这些项目添加到构建路径中,这样我就可以毫无问题地使用所有对象和服务

    现在我开始实现第一个控制台实用程序,并面临一些问题。。看一看

    public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-service.xml", "spring-hibernate.xml");
    
            try {
                MessageSourceEx messageSource = new MessageSourceEx((ResourceBundleMessageSource) ctx.getBean("messageSource"));
                ITasksService tasksService = (ITasksService) ctx.getBean("tasksService");
                NotificationsService notificationsService = (NotificationsService) ctx.getBean("notificationsService");
    
                List<Task> tasks = tasksService.systemGetList();
                for (Task t: tasks) {
                    Locale userLocale = t.getCreator().getCommunicationLanguageLocale();
                    EmailNotification reminder = new EmailNotification(t.getCreator().getEmail(), 
                            messageSource.getMessage(userLocale, "notifications.internal.emails.task.subject"), 
                            messageSource.getMessage(userLocale, "notifications.internal.emails.task.text", 
                                    t.getCreator().getNickname(),
                                    t.getName(),
                                    t.getDescription(),
                                    AppConfig.getInstance().getUrl(),
                                    t.getId()), 
                            userLocale, t.getCreator());
                    notificationsService.email.send(reminder);
                    if (reminder.getState() == EmailNotificationSendState.Sent) {
                        t.setReminderSent(true);
                        tasksService.save(t);
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                ((ConfigurableApplicationContext)ctx).close();
            }
    
            System.exit(0);
        }
    
    publicstaticvoidmain(字符串[]args){
    ApplicationContext ctx=新的ClassPathXmlApplicationContext(“spring service.xml”、“spring hibernate.xml”);
    试一试{
    MessageSourceEx messageSource=newMessageSourceEx((ResourceBundleMessageSource)ctx.getBean(“messageSource”);
    ITasksService tasksService=(ITasksService)ctx.getBean(“tasksService”);
    NotificationsService NotificationsService=(NotificationsService)ctx.getBean(“NotificationsService”);
    List tasks=tasksService.systemGetList();
    for(任务t:任务){
    Locale userLocale=t.getCreator().getCommunicationLanguageLocale();
    EmailNotification提醒=新的EmailNotification(t.getCreator().getEmail(),
    messageSource.getMessage(userLocale,“notifications.internal.emails.task.subject”),
    messageSource.getMessage(userLocale,“通知.内部.电子邮件.任务.文本”,
    t、 getCreator().getNickname(),
    t、 getName(),
    t、 getDescription(),
    AppConfig.getInstance().getUrl(),
    t、 getId()),
    userLocale,t.getCreator());
    通知服务。电子邮件。发送(提醒);
    if(remention.getState()==EmailNotificationSendState.Sent){
    t、 设置提醒(真);
    tasksService.save(t);
    }
    }
    }
    捕获(例外e){
    e、 printStackTrace();
    }
    最后{
    ((ConfigurableApplicationContext)ctx).close();
    }
    系统出口(0);
    }
    
    spring-service.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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        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">
        <context:annotation-config />
        <context:component-scan base-package="com.dao,com.service,com.notifications,com.interfaces" />
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="com.Resources" />
        </bean>
    </beans>
    
    
    
    spring-hibernate.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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        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">
        <tx:annotation-driven />
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:/hibernate.properties" />
        </bean>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${hibernate.connection.driver_class}" />
            <property name="url" value="${hibernate.connection.url}" />
            <property name="username" value="${hibernate.connection.username}" />
            <property name="password" value="${hibernate.connection.password}" />
        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses">
                <list>
                    <value>com.data.Task</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                    <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
               </props>
            </property>
        </bean>
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    </beans>
    
    
    com.data.Task
    ${hibernate.dial}
    ${hibernate.show_sql}
    ${hibernate.format_sql}
    ${hibernate.cache.use\u query\u cache}
    ${hibernate.cache.region.factory\u class}
    

    @组件
    公共类任务{
    /**
    *检索任务列表
    * 
    *@返回任务列表
    */
    @SuppressWarnings({“unchecked”})
    公共列表systemGetList(){
    Session Session=SessionFactoryUtils.getSession(sessionFactory,false);
    列表结果=空;
    现在日期=新日期();
    条件查询=session.createCriteria(Task.class)
    .add(Restrictions.le(“提醒时间”,DateUtilsEx.addMinutes(现在,3)))
    .add(Restrictions.eq(“提醒”,false))
    .addOrder(Order.asc(“提醒时间”));
    结果=query.list();
    如果(结果==null)
    结果=新的ArrayList();
    返回结果;
    }
    }
    
    服务

    @Service
    public class TasksService implements ITasksService {
    
        /**
         * 
         */
        @Override
        public List<Task> systemGetList() {
            return tasksDAO.systemGetList();
        }
    }
    
    @服务
    公共类TasksService实现ITasksService{
    /**
    * 
    */
    @凌驾
    公共列表systemGetList(){
    返回任务dao.systemGetList();
    }
    }
    
    它失败,没有绑定到线程的Hibernate会话,并且配置不允许在此创建非事务会话 在org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:356)exception。。有趣的是,如果我将
    @Transactional
    添加到
    systemGetList()
    ,效果很好。但我不想为所有select语句添加事务。。。 同样的代码(没有事务)在网站上也能正常工作


    有什么帮助吗?提前感谢。

    您已将服务方法指定为事务性的

    <tx:annotation-driven />
    
    
    

    在选择/只读方法上添加
    @Transactional(readOnly=true)

    是的,我理解。但是为什么它在web项目(我以前写过的CRM)上工作得很好,而在这里它却失败了。。对象和服务相同..配置文件相同吗?那一定有什么不同。在Spring上下文中,可能没有在该CRM上创建事务管理器,或者使用
    Hmm应用事务语义。。您是对的,我没有在spring上下文中指定此注释,但在hibernate中。。。但交易仍然可以正常进行,我现在再次检查了它。。如果一种方法失败,第二种方法将回滚。。因此,我想我将使用
    建议
    功能。最后,我决定用另一个包含
    @事务性
    的服务重写库的DAO服务。
    <tx:annotation-driven />