Spring笔记-set对象注入与注解

一、set对象注入
一个对象的成员变量中常常含另一个类,这个时候就需要对象注入。

一个set注入的例子:
AccountServiceImpl类中成员变量含有AccountDao

AccountServiceImpl:

package zk;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class AccountServiceImpl implements AccountService {

    /*
     * @Autowired
     * 
     * @Qualifier("accountDao")
     */
    **private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }**

    public void transfer(String from, String to, Double money) {

        accountDao.from(from, money);
        accountDao.in(to, money);

    }

}

AccountDao类:
这个类不用太关心大概看一下就是

package zk;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{

    public void from(String from, Double money) {
        // TODO Auto-generated method stub
        String sql = "update account set money=money-? where name = ?";

        this.getJdbcTemplate().update(sql,money,from);
//      System.out.println("adsasd");
    }

    public void in(String in, Double money) {
        // TODO Auto-generated method stub
        String sql = "update account set money=money+? where name = ?";
        this.getJdbcTemplate().update(sql, money,in);

    }

}

配置applicationContext.xml

<bean id="accountDao" class="zk.AccountDaoImpl">
        <property name="dataSource" ref="dataSource" />
</bean>

<bean id="accountService" class="zk.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>

setter注入对象有两点需要注意:
1.需要在含有对象成员变量的那个类中提供被注入对象的set方法,就像这里的AccountServiceImpl类 上面代码出现加粗的地方 就是对象成员变量与set方法
2.就是需要在applicationContext.xml中配置AccountServiceImpl类和AccountDaoImpl类,并且在AccountServiceImpl类中 配置成员变量 ref而不是value 这个不要搞错了。

二、注解方式注入
跟上面的set方式注入不同的是,这里不需要set方法了 AccountServiceImpl换成了如下:

package zk;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class AccountServiceImpl implements AccountService {

    @Autowired
    @Qualifier("accountDao")
    private AccountDao accountDao;

    public void transfer(String from, String to, Double money) {

        accountDao.from(from, money);
        accountDao.in(to, money);

    }

}

配置文件如下:

<bean id="accountDao" class="zk.AccountDaoImpl">
        <property name="dataSource" ref="dataSource" />
</bean>
<bean id="accountService" class="zk.AccountServiceImpl">
        <!-- <property name="accountDao" ref="accountDao"></property> -->
</bean>

注意到 这里不再需要,因此注释掉了。

@Qualifier(“accountDao”) 这里的”accountDao” 对应配置文件中的AccountDao类的id。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值