基于注解和配置类的方式实现IOC注入
工程结构
常规三层架构实现
dao 层
package com.example.dao;
import com.example.pojo.Student;
import java.util.List;
public interface StudentDao {
public List<Student> queryAll();
}
package com.example.dao.impl;
import com.example.pojo.Student;
import com.example.dao.StudentDao;
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 // ioc 容器注入的必须是一个 实现类,不能是接口,因此 ioc注解 必须标注在实现类上面
public class StudentDaoImpl implements StudentDao {
// jdbcTemplate 也从 ioc 容器中获取,在配置类中配置该bean,在整体都加入ioc容器中,他们一起都是透明可见的
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Student> queryAll() {
String sql = "select id, name, gender, age, class as classes from students;";
List<Student> studentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
return studentList;
}
}
service 层
package com.example.service;
import com.example.pojo.Student;
import java.util.List;
public interface StudentService {
public List<Student> findAll();
}
package com.example.service.impl;
import com.example.pojo.Student;
import com.example.dao.StudentDao;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> findAll() {
List<Student> studentList = studentDao.queryAll();
return studentList;
}
}
controller 层
package com.example.controller;
import com.example.pojo.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
// 注入ioc
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
public void getList() {
List<Student> studentList = studentService.findAll();
System.out.println(studentList);
}
}
配置类
package com.example.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
// 开启包扫描注解,value 是一个 String[],那么 String[] 的实例化是怎么写的呢?String[] strs = new String(){};
@ComponentScan(value = {"com.example"})
// 加载外部资源注解
@PropertySource(value = {"classpath:jdbc.properties"})
public class JavaConfig {
// DruidDataSource bean 组件声明在ioc容器中
@Bean
public DataSource createDataSource(@Value("${atguigu.url}") String url, @Value("${atguigu.driver}") String driver,
@Value("${atguigu.username}") String username, @Value("${atguigu.password}") String password) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(url);
druidDataSource.setDriverClassName(driver);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
// 注入 ioc 中,同时需要引用到 createDataSource
@Bean
public JdbcTemplate createJdbcTemplate(DataSource createDataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(createDataSource);
return jdbcTemplate;
}
}
测试
package com.example;
import com.example.config.JavaConfig;
import com.example.controller.StudentController;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestIoC {
/**
* 注解+配置类实现 ioc 容器注入的方式的测试
*/
@Test
public void test() {
// 1. 构造 ioc 容器
ApplicationContext applicationContext
= new AnnotationConfigApplicationContext(JavaConfig.class);
// 2. 获取 bean
StudentController controller = applicationContext.getBean(StudentController.class);
controller.getList();
}
}
本文来自博客园,作者:LoremMoon,转载请注明原文链接:https://www.cnblogs.com/hello-cnblogs/p/17786686.html