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

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


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

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">
        
bean>

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

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

你可能感兴趣的:(javaee,Spring)