jyp2613 2010-04-19 00:55
浏览 295
已采纳

spring 注解方面的疑惑

小弟最近刚刚接触ssh2整合 ,有2个问题没弄明白,希望各位高手 指教一下,小弟不胜感激
下面把我的代码先贴出来

[color=red]user.java[/color]
public class User {
private int id;
private String name;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

[color=red]user.hbm.xml[/color]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >







[color=red]UserDao[/color]

public interface UserDao {

/**
 * 添加
 */
public void save(User user);

[color=red]UserDaoImpl[/color]

@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

@Resource(name = "hibernateTemplate")
public void setHibernateTemplate1(HibernateTemplate hibernateTemplate) {
    super.setHibernateTemplate(hibernateTemplate);
}

/*
 * 添加
 */
public void save(User user) {
    getHibernateTemplate().save(user);
}

[color=red]UserService[/color]

public interface UserService {
/**
* 注册用户
*/
public void save(User user);

[color=red]UserServiceImpl[/color]

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;

/*
 * 注册用户
 */
public void save(User user) {
    if (user != null && user instanceof Object)
        userDao.save(user);
}

[color=red]struts.xml[/color]

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<package name="User" extends="struts-default">
    <action name="userAdd" class="userAddAction">
        <result name="success" >/success.jsp</result>
        <result name="input">/userAdd.jsp</result>
    </action>
</package>

[color=red]applicationContext.xml[/color]

<?xml version="1.0" encoding="UTF-8"?>
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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- spring自动扫描和管理bean -->
    <context:component-scan base-package="cn.com.jyp"/>

    <!-- 配置数据源 -->
    <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/ssh2" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>cn/com/jyp/model/User.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 配置hibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- 配置spring提供的事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 采用@Transactional注解方式使用事务  -->
    <tx:annotation-driven transaction-manager="txManager"/>

我的疑问是,第一个:在使用Spring提供的注解方式@Resource 注入依赖对象,标注在属性上,是不是可以不用提供这个属性的setter方法,如果可以不提供的话,为什么UserDaoImpl 里 这么写
@Resource private HibernateTemplate hibernateTemplate;
确出错,这个注解的实现原理是不是根据相应的属性自动提供其setter方法为其注入的啊 ?
第二个:关于事务的注解是按照上面的代码这么标在service上吧?
第三个:就是请各位帮我看看我的代码不足之处 谢谢了!

  • 写回答

5条回答 默认 最新

  • iteye_13917 2010-04-21 10:23
    关注

    1、默认情况下@Resource是按照setXXX格式来注入的,也就是按照属性名注入的。必须要有set方法

    2、事务的处理一般不做成你那样的,而是做一个统一的切面进行事务处理,即直接在spring配置文件里面指定某个层为事务层,如:service层:

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*"      propagation="REQUIRED"/>
            <tx:method name="save"      propagation="REQUIRED"/>
            <tx:method name="delete*"   propagation="REQUIRED"/>
            <tx:method name="update*"   propagation="REQUIRED"/>
            <tx:method name="*"         read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 那些类的哪些方法参与事务 -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution(* service.*.*(..))"/>
        <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
    </aop:config>
    

    3、大家都很忙,没时间看你的代码哦~还是直接问问题好点

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算