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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵