SpringIoC/DI完全注解配置流程笔记

一、配置说明

1.完全注解方式指的是去掉xml文件,使用配置类 + 注解实现

2.xml文件替换成使用@Configuration注解标记的类

3.标记IoC注解:@Component,@Service,@Controller,@Repository

4.标记DI注解:@Autowired @Qualifier @Resource @Value

5.<context:component-scan标签指定注解范围使用@ComponentScan(basePackages = {"com.suchuanlin.components"})替代

6.<context:property-placeholder引入外部配置文件使用@PropertySource({"classpath:application.properties","classpath:jdbc.properties"})替代

7.<bean 标签使用@Bean注解和方法实现

8.IoC具体容器实现选择AnnotationConfigApplicationContext对象

二、配置流程

1.准备数据库测试数据

/*创建表*/
create table user(
    id int primary key auto_increment,
    phone varchar(20),
    username varchar(50),
    password varchar(50),
    gender char,
    email varchar(50)
);

/*插入数据*/
insert into user (id, phone, username, password, gender, email)
values (null, '18800001111', '张三', '123456', '男', '18800001111@163.com'),
       (null, '18800001112', '李四', '123456', '女', '18800001112@163.com'),
       (null, '18800001113', '王五', '123456', '男', '18800001113@163.com'),
       (null, '18800001114', '赵六', '123456', '女', '18800001114@163.com'),
       (null, '18800001115', '田七', '123456', '男', '18800001115@163.com'),
       (null, '18800001116', '陈八', '123456', '女', '18800001116@163.com'),
       (null, '18800001117', '朱九', '123456', '男', '18800001117@163.com'),
       (null, '18800001118', '郑十', '123456', '女', '18800001118@163.com');

/*查询表*/
select * from user;

2.准备jdbc.properties配置文件

suchuanlin.url=jdbc:mysql://localhost:3306/db_02
suchuanlin.driver=com.mysql.cj.jdbc.Driver
suchuanlin.username=root
suchuanlin.password=1234

3.搭建项目框架结构

Spring-ioc-Annotation
|---src
|---|---main
|---|---|---java
|---|---|---|---com.suchuanlin
|---|---|---|---|---config
|---|---|---|---|---controller
|---|---|---|---|---dao
|---|---|---|---|---pojo
|---|---|---|---|---service

|---|---|---resources
|---|---|---|---jdbc.properties

|---|---test
|---|---|---java
|---|---|---|---com.suchuanlin.test
|---|---|---|---|---SpringIcoCTest

4.编写配置类

package com.suchuanlin.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;

@Configuration  //声明当前为配置类
@ComponentScan(basePackages = "com.suchuanlin") //声明包扫描范围
@PropertySource(value = "classpath:jdbc.properties")    //引入外部配置文件
public class JavaConfig {

    //配置数据库连接池参数
    @Value("${suchuanlin.driver}")
    private String driver;
    @Value("${suchuanlin.url}")
    private String url;
    @Value("${suchuanlin.username}")
    private String username;
    @Value("${suchuanlin.password}")
    private String password;

    //配置DruidDataSource
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    //配置JdbcTemplate
    @Bean
    public JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }
}

5.编写pojo.User类

package com.suchuanlin.pojo;

public class User {
    private Integer id;
    private String phone;
    private String username;
    private String password;
    private String gender;
    private String email;

    public User() {
    }

    public User(Integer id, String phone, String username, String password, String gender, String email) {
        this.id = id;
        this.phone = phone;
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", phone='" + phone + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

6.1.编写UserDao接口

package com.suchuanlin.dao;

import com.suchuanlin.pojo.User;
import java.util.List;

public interface UserDao {
    List<User> selectAll();
}

6.2.编写UserDaoImpl类实现UserDao接口

package com.suchuanlin.dao.impl;

import com.suchuanlin.dao.UserDao;
import com.suchuanlin.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<User> selectAll() {
        String sql = "select * from user";
        List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        return userList;
    }
}

7.1.编写UserService接口

package com.suchuanlin.service;

import com.suchuanlin.pojo.User;
import java.util.List;

public interface UserService {

    List<User> selectAll();

}

7.2.编写UserServiceImpl类实现UserService接口

package com.suchuanlin.service.impl;

import com.suchuanlin.dao.UserDao;
import com.suchuanlin.pojo.User;
import com.suchuanlin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public List<User> selectAll() {
        List<User> userList = userDao.selectAll();
        return userList;
    }
}

8.编写UserController

package com.suchuanlin.controller;

import com.suchuanlin.pojo.User;
import com.suchuanlin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    public void SelectAll(){
        List<User> userList = userService.selectAll();
        for (User user : userList) {
            System.out.println(user);
        }
    }
}

9.测试

package com.suchuanlin.test;

import com.suchuanlin.config.JavaConfig;
import com.suchuanlin.controller.UserController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(value = {JavaConfig.class})  //声明配置文件
public class SpringIcoCTest {

    @Autowired
    private UserController userController;

    @Test
    public void testSpringIoCJavaConfig(){
        userController.SelectAll();
    }
}

10.输出结果

D:\Programs\Java\jdk-17\bin\java.exe...
10月 12, 2023 1:59:57 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
User{id=1, phone='18800001111', username='张三', password='123456', gender='男', email='18800001111@163.com'}
User{id=2, phone='18800001112', username='李四', password='123456', gender='女', email='18800001112@163.com'}
User{id=3, phone='18800001113', username='王五', password='123456', gender='男', email='18800001113@163.com'}
User{id=4, phone='18800001114', username='赵六', password='123456', gender='女', email='18800001114@163.com'}
User{id=5, phone='18800001115', username='田七', password='123456', gender='男', email='18800001115@163.com'}
User{id=6, phone='18800001116', username='陈八', password='123456', gender='女', email='18800001116@163.com'}
User{id=7, phone='18800001117', username='朱九', password='123456', gender='男', email='18800001117@163.com'}
User{id=8, phone='18800001118', username='郑十', password='123456', gender='女', email='18800001118@163.com'}

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值