Spring黑马:SpringIOC案例(JDBC数据库):注解配置


SpringIOC案例(JDBC数据库):XML配置

1. bean.xml中配置context标签

主要是2点变化(其余如配置QueryRunner对象,配置数据源均沿用XML配置):
第一点:文件头变化成注解配置
在这里插入图片描述
第二点:将(1)配置Service层(2)配置Dao层更换成context标签
在这里插入图片描述
添加内容

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

<!--注解配置:告知Spring在创建容器时要扫描的包-->
    <context:component-scan base-package="com.jh"></context:component-scan>

2. 各层相对于XML配置的变化

1.dao层

  • AccountDaoImpl实现类变化:
    (1)删除set和get方法,为避免空指针异常,注解@Autowired;
    (2)加了创建bean对象的注解@Repository(“accountDao”)
    (3)其余代码沿用
    在这里插入图片描述
    变化代码
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    private QueryRunner runner;
  • IAccountDao接口无变化

2. domain层无变化

3. service层

  • AccountServiceImpl 业务层变化:
    同dao层变化一样,变化代码
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
   @Autowired
   private IAccountDao accountDao;
  • IAccountService 无变化

4. AccountServiceTest无变化

发布了101 篇原创文章 · 获赞 15 · 访问量 6767

猜你喜欢

转载自blog.csdn.net/JH39456194/article/details/104399523