java spring第七次学习 用于注入数据的注解

用于注入数据的注解
*      他们的作用就和在xml配置文件中的bean标签中写一个<property></property>标签的作用是一样  的
*      Autowired:
*          作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的类型匹配,就可以注入成功
*              如果ioc容器中没有任何bean的类型和要注入的类型匹配,则报错。
*              如果ioc容器中有多个匹配的bean类型
*          出现位置:
*              可以使变量上,也可以是方法上
*          细节:
*              在使用注解注入时,set方法就不是必须的了。
*      Qualifier:(和Autowired一起使用)
*          作用:在按照类中注入的基础之上再按照名称注入。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以
*          属性:
*              value:用于指定注入bean的id。
*      Resource:
*          作用:直接按照bean的id注入。可以独立使用
*          属性:
*              name:用于指定bean的id。
*      以上三个注入都只能注入其他bean类型的数据。而基本类型和String类型无法使用上述注解实现。
*      另外,集合类型的注入只能通过XML来实现。
*
*      Value:
*          作用:用于注入基本类型和String类型的数据
*          属性:
*              value:用于指定数据的值。它可以使用spring中的SpEl(也就是spring的el表达式)
*                      SpEl的写法:${表达式}
package com.dao.impl;
import com.dao.*;
import org.springframework.stereotype.Repository;

/**
 * 账户持久类实现类
 */
@Repository("accountDao1")
public class AccountDaoImpl1 implements IAccountDao{
    @Override
    public void save() {
        System.out.println("success1");
    }
}
package com.dao.impl;
import com.dao.*;
import org.springframework.stereotype.Repository;

/**
 * 账户持久类实现类
 */
@Repository("accountDao2")
public class AccountDaoImpl1 implements IAccountDao{
    @Override
    public void save() {
        System.out.println("success2");
    }
}
package com.ui;
import com.dao.IAccountDao;
import com.service.*;
import com.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 模拟表现层
 */
public class Client {
    /**

     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//此时创建对象
        //2.根据id获取Bean对象
        IAccountService as = (IAccountService)ac.getBean("accountService");
        as.saveAccount();
        //System.out.println(as);
        //IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
        //System.out.println(adao);
    }
}

 有改动的代码部分:

package com.service.impl;

import com.dao.IAccountDao;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService {
    //@Autowired
    //@Qualifier(value = "accountDao1")
    @Resource(name = "accountDao2")
    private IAccountDao accountDao = null;
    @Override
    public void saveAccount() {
        accountDao.save();
    }
}

运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值