Spring之IOC案例分析以及注解案例

day2..........

一、spring中IOC的常用注解

1.注解创建对象

@Component

2.自动按照类型注入

如果有唯一一个匹配时,直接注入;若有多个匹配,则首先圈出匹配的对象,接着使用变量名称

作为bean的id在圈定的对象中继续查找,若圈中的对象一样则匹配成功,否则匹配失败;

3.该边作用范围和生命周期的注解


二、基于XML的IOC案例

案例分析:用基于XML的IOC实现对一个标的CURD操作

 

1)创建数据库,添加数据

CREATE TABLE `t_account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `aname` varchar(30) DEFAULT NULL,
  `money` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

2)创建西maven项目,导入必要的;依赖编写实体类、数据持久层、以及业务层的接口类和实现类,项目结构如图所示

pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dzg</groupId>
    <artifactId>spring-day2-xml-ioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.6</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

    </dependencies>
</project>

实体类:

public class Account implements Serializable {

    private Integer id;
    private String aname;
    private float money;

    生成对应的set和get方法
}

业务层:

/**
 * 账户的业务接口层
 */
public interface IAccountService{

    /**
     * 查询所有账户信息
     * @return
     */
    List<Account> findAllAccount();

    /**
     * 根据id查询一个账户
     * @param accountId
     * @return
     */
    Account findAccountById(Integer accountId);

    /**
     * 增加账户信息
     * @param account
     */
    void insertAccount(Account account);

    /**
     * 修改账户信息
     * @param account
     */
    void updateAccount(Account account);

    /**
     * 根据id删除账户信息
     * @param accountId
     */
    void deleteAccount(Integer accountId);
}

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;

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

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

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

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

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

    public void deleteAccount(Integer accountId) {
        accountDao.deleteAccount(accountId);
    }
}

持久层:持久层接口和业务层接口方法相同,只需要实现持久层实现类,实现和数据库的交互

/**
 * 账户持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    private QueryRunner qr;

    public void setQr(QueryRunner qr) {
        this.qr = qr;
    }

    public List<Account> findAllAccount() {
        try {
            return qr.query("select * from t_account",new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Account findAccountById(Integer accountId) {
        try {
            return qr.query("select * from t_account where id=?",new BeanHandler<Account>(Account.class),accountId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void insertAccount(Account account) {

        try {
            qr.update("insert into t_account(aname,money) values(?,?)",account.getAname(),account.getMoney());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void updateAccount(Account account) {

        try {
            qr.update("update t_account set aname = ?,money=? where id = ?",account.getAname(),account.getMoney(),account.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void deleteAccount(Integer accountId) {
        try {
            qr.update("delete from t_account where id = ?",accountId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

上述步骤完成之后,配置spring环境:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置service-->
    <bean id="accountService" class="com.dzg.service.impl.AccountServiceImpl">
        <!--注入dao-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!--配置Dao对象-->
    <bean id="accountDao" class="com.dzg.dao.impl.AccountDaoImpl">
        <!--注入QueryRunner-->
        <property name="qr" ref="runner"></property>
    </bean>

    <!--配置QueryRunner-->
    <bean id="runner" 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/db_spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

编写junit单元测试,对上述代码进行测试:

/**
 * 使用juint单元测试,测试配置
 */
public class AccountServiceTest {

    @Test
    public void testFindAll(){
        //1.获取数据信息
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        List<Account> accountList = as.findAllAccount();
        for (Account account:
             accountList) {
            System.out.println(account);
        }
    }

    @Test
    public void testFindOne(){
        //1.获取数据信息
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        Account account = as.findAccountById(3);
        System.out.println(account);
    }

    @Test
    public void testAdd(){
        Account account = new Account();
        account.setAname("ddd");
        account.setMoney(1234f);
        //1.获取数据信息
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);

        as.insertAccount(account);
    }

    @Test
    public void testUpdate(){

        Account account = new Account();
        account.setId(4);
        account.setAname("dddupdate");
        account.setMoney(4434f);
        //1.获取数据信息
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        as.updateAccount(account);
    }

    @Test
    public void testDelete(){
        //1.获取数据信息
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        as.deleteAccount(4);
    }

}

 

三、注解开发ioc案例

使用注解开发,更改上述案例:

      对于JavaBean,持久层和业务层的实现类,使用注解的方式进行更改,则对应的beans.xml文件中删除已经注解的bean;通知spring

在创建容器时需要扫描的包;

    在对相应的实现类进行注解后,则类中的成员变量不再需要相应的set方法,删除即可;同时将变量注解为Autowired

 

注解开发,更改之后的beans.xml文件

<!--告知spring在创建容器时需要扫描的包-->
        <context:component-scan base-package="com.dzg"></context:component-scan>
    <!--配置QueryRunner-->
    <bean id="runner" 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/db_spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

四、去掉beans.xml文件的注解开发

1.spring的新注解Configuation和ComponentScan

但是并不绝对!

2.spring的新注解bean

在config包下

 

删除xml文件

进行测试:

 @Test
    public void testFindAll(){
        //1.获取数据信息
//        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        ApplicationContext ac = new AnnotationConfigApplicationContext("com.dzg");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService", IAccountService.class);
        //3.执行方法
        List<Account> accountList = as.findAllAccount();
        for (Account account:
             accountList) {
            System.out.println(account);
        }

3.AnnotationConfigApplicationContext的使用

注意,曾经的QueryRunner是一个多例的,现在是一个单例子的;

test测试下:

在createQueryRunner上添加注解 @Scope("prototype")

4.spring的新注解Import

将内容拷贝到JdbcConfig

即@Configuration注解不写

        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);


此时使用另一个方法来创建ApplicationContext对象,传递的参数为注解的类

5.spring的新注解PropertySource

将数据库连接信息提取出,写入一个properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_spring
jdbc.username=root
jdbc.password=root

自己写的类使用注解开发,jar包中的类使用xml配置

五、spring和junit整合

1.分析:

 

2.整合

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值