基于注解的IOC案例

本节内容

我们对上一节中实现的项目进行改写,将其修改称为注解的方式进行配置。
但是我们要注意的是由于数据库的相关类都是封装在别人jar包中的类,因此无法改写别人的代码,对这种类型的配置只能通过xml的方式进行配置,或者是我们后续学习的使用config class类进行配置。

bean.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在bean约束中,
       而是在一个名称为context的名称空间和约束中-->

    <context:component-scan base-package="com.lujuan"/>

    <!--没有办法改-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <constructor-arg name="ds" ref = "dataSource"> </constructor-arg>
    </bean>

    <!--没有办法改-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <property name="driverClass" value="com.mysql.jdbc.Driver"> </property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?characterEncoding=utf8"> </property>
        <property name="user" value="root"> </property>
        <property name="password" value="Admin@123"> </property>

    </bean>

</beans>

dao-impl 文件

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {


    @Resource(name = "queryRunner")
    private QueryRunner queryRunner;//这是commons.dbutils提供的

    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }




    public List<Account> findAllAccount() {


        List<Account> reV = null;
        try{
            //查询之后顺便映射为实体类
            reV = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));

        }catch (Exception e){
            throw new RuntimeException(e);
        }

        return reV;
    }

    public Account findAccountById(Integer id) {

        Account reV = null;
        try{
            reV = queryRunner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class), id);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return reV;
    }

    public void addAccount(Account account) {

        try{
            queryRunner.update("insert into account(name, money) values (?, ?)", account.getName(), account.getMoney());
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    public void deleteAccountById(Integer id) {
        try{
            queryRunner.update("delete  from account where id = ?", id);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public void updateAccount(Account account) {

        try{
            queryRunner.update("update account set name= ?, money = ? where id = ?", account.getName(), account.getMoney(), account.getId());
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }
}

主要是增加了上面两行内容,主要是bean定义了依赖注入

service-impl修改

@Service(value="accountService")
public class AccountServiceImpl implements IAccountService {



    @Resource(name = "accountDao")
    private IAccountDao accountDao;


    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }


    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    public Account findAccountById(Integer id) {
        return accountDao.findAccountById(id);
    }

    public void addAccount(Account account) {
        accountDao.addAccount(account);
    }

    public void deleteAccountById(Integer id) {

        accountDao.deleteAccountById(id);
    }

    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    public void saveAccount() {

        System.out.println("AccountServiceImpl: saveAccount");
    }

    @PostConstruct
    public void init(){

        System.out.println("init......");
    }

    @PreDestroy
    public void destroy(){

        System.out.println("destroy......");
    }
}

主要bean定义和依赖注入。
至此更改还是很简单的。
本节完结。

小白学习java Spring 文章被收录于专栏

寻觅了很多学习Spring的学习资料,终于让我找到讲解的简单清晰的老师了~ 分享给同样从零开始学习的人

全部评论

相关推荐

刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
03-23 15:00
已编辑
厦门大学 Java
xiaowl:你这个简历的问题是对于技术点、项目的描述,都是描述action的,对于面试官而言,仅能知道你干了什么,无法判断你为什么这么干,干的好不好。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务