简体   繁体   中英

Implementing Transactional annotation

I need to implement transactional values in my newly built application but i am not able to understand how i should go ahead i know has to be added to my Application context. xml. But still its not working. Can any one guide me please ? I am a beginner in Spring. Below is my code and implementation for just saving registration info :

ApplicationContext.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.xsd">

    <bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost/springmvc"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>        
    </bean> 
     <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceBean"></property>
       <!--<property name="mappingResources">
            <value>com.lnt.Pojo/Login.hbm.xml</value>                   
        </property>-->
        <property name="hibernateProperties">
              <props>
                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                  <prop key="hibernate.show_sql">true</prop>
              </props>
         </property>
    </bean>
    <bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactoryBean"></property>
    </bean>
    <!--  <bean id="AuthenticateServiceBean" class="com.lnt.services.AuthenticateServices">
        <property name="hibernateTemplate" ref="hibernateTemplateBean"></property>
    </bean> -->

</beans>

mvc- dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


    <context:component-scan base-package="com.karan.TestFactory.Controller"></context:component-scan>

    <bean id = "viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
    <value>/</value>
    </property>
    <property name="suffix">
    <value>.jsp</value>
    </property>

    </bean>
    </beans>

My RegisterController

package com.karan.TestFactory.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.karan.TestFactory.Services.BaseServices;
import com.karan.TestFactory.entity.Register_info;

@Controller
public class RegisterController {

    BaseServices baseservices ;

    @RequestMapping(value="submitRegistration", method = RequestMethod.POST)
    public String adduser(@ModelAttribute Register_info register_info, HttpServletRequest request)  
    {
        baseservices.saveorupdate(register_info);

        return null;

    }


}

My Base Service Interface

package com.karan.TestFactory.Services;

import com.karan.TestFactory.entity.Register_info;

public interface BaseServices {

    void saveorupdate(Register_info register_info);

}

My BaseImpl class

package com.karan.TestFactory.Services;

import org.springframework.beans.factory.annotation.Autowired;

import com.karan.TestFactory.Dao.BaseDao;
import com.karan.TestFactory.entity.Register_info;

public class BaseImpl implements BaseServices{

@Autowired
BaseDao basedao ;

@Transactional
public void saveorupdate(Register_info register_info) {
    // TODO Auto-generated method stub

}



}

Add these lines to your ApplicationContext.xml

<bean id="transactionManager" 
          class="org.springframework.orm.hibernate3.HibernateTransactionManager >
   <property name="sessionFactory" ref="sessionFactoryBean" />
</bean>

<tx:annotation-driven/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM