【Spring】基于注解的IOC案例

Spring学习笔记(五)基于注解的IOC案例

1.创建账户实体类

  • Account
@Component
public class Account {
    @Value("1")
    private Integer id;
    @Value("Tom")
    private String name;
    @Value("34567")
    private Float money;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

2.创建接口

  • IAccountDao
public interface IAccountDao {

    /**
     * 保存
     */
    void saveAccount();
}
  • IAccountService
public interface IAccountService {
    /**
     * 保存
     */
    void saveAccount();
}

3.创建实现类

  • AccountDaoImpl
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    private Account account;
    /**
     * 保存
     *
     */
    @Override
    public void saveAccount() {
        System.out.println("基于注解的spring");
        System.out.println("保存的账户为:"+"Account{" +
                "id=" + account.getId() +
                ", name='" + account.getName() + '\'' +
                ", money=" + account.getMoney() +
                '}');
    }
}
  • AccountServiceImpl
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    @Qualifier("accountDao")
    private IAccountDao accountDao=null;

    /**
     * 保存
     */
    @Override
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

4.创建测试类

  • AccountServiceTest
public class AccountServiceTest {
    ApplicationContext applicationContext;
    @Before
    public void setUp(){
        // 获取配置文件
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void testAccountService(){
        //获取bean
        IAccountService accountService = applicationContext.getBean("accountService", AccountServiceImpl.class);

        accountService.saveAccount();
    }
}

5.配置 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="cn.edu.wtu"></context:component-scan>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攻城狮·建哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值