06.使用Spring注解实现替换xml配置

221 阅读1分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第16天,点击查看活动详情

  • 📝 个人主页:程序员阿红🔥
  • 🎉 支持我:点赞👍收藏⭐️留言📝
  • 🍓欢迎大家关注哦,互相学习🍓
  • 🍋欢迎大家访问哦,互相学习🍋
  • 🍑欢迎大家收藏哦,互相学习🍑 需求:在之前我们依然需要依靠xml配置文件,来进行依赖注入。现在我们需要利用注解来替换掉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="com.lagou"></context:component-scan>
​
    <!--引入jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
​
​
    <!--dataSource-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
​
​
    <!--queryRunner-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"/>
    </bean>
​
    <!--AccountDao-->
    <bean id="accountDao" class="com.lagou.dao.impl.AccountDaoImpl">
        <property name="queryRunner" ref="queryRunner"/>
    </bean>
​
    <!--AccountDao2-->
    <bean id="accountDao2" class="com.lagou.dao.impl.AccountDaoImpl2">
    </bean></beans>

1.Spring新注解

使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:

image-20220303100423255

2.Spring纯注解整合DbUtils

步骤:

  1. 编写Spring核心配置类
  2. 编写数据库配置信息类
  3. 编写测试代码

(1)编写Spring核心配置类

@Configuration
@ComponentScan("com.lagou")
@Import(DataSourceConfig.class)
public class SpringConfig {
  @Bean("queryRunner")
  public QueryRunner getQueryRunner(@Autowired DataSource dataSource) {
    return new QueryRunner(dataSource);
 }
}

(2)编写数据库配置信息类

@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
  @Value("${jdbc.driver}")
  private String driver;
  @Value("${jdbc.url}")
  private String url;
  @Value("${jdbc.username}")
  private String username;
  @Value("${jdbc.password}")
  private String password;
  @Bean("dataSource")
  public DataSource getDataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName(driver);
      dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
 }
}

(3)编写测试代码

public class AccountServiceTest {
ApplicationContext applicationContext =
 new
AnnotationConfigApplicationContext(SpringConfig.class);
  AccountService accountService =
applicationContext.getBean(AccountService.class);
  //测试查询
  @Test
  public void testFindById() {
    Account account = accountService.findById(3);
    System.out.println(account);
 }
}

💖💖💖 完结撒花

💖💖💖 路漫漫其修远兮,吾将上下而求索

💖💖💖 写作不易,如果您觉得写的不错,欢迎给博主点赞、收藏、评论、收藏来一波~让博主更有动力吧

✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨

最后,还不收藏进你的收藏夹吃灰😎😎😎