Ioc之全注解开发

  • @Configuration是@Component的别名,所以两个使用哪个都可以
  • @ComponentScan注解中value的别名是basePackages,所以两个用哪个都可以
  • 配置类
@Component
//@Configuration
//@ComponentScan(value = {"cn.powernode.dao","cn.powernode.service"})
@ComponentScan(basePackages = {"cn.powernode.dao","cn.powernode.service"})
public class Spring6Config {
}

该配置类相当于*.xml配置文件中的<context:component-scan base-package="org.powernode"/>

  • 持久层接口
public interface StudentDao {
void deleteById();
}
  • 持久层实现类
//@Repository中默认的value也是类名首字母变小写
@Repository("studentDaoImplForMySql")
public class StudentDaoImplForMySql implements StudentDao {
@Override
public void deleteById() {
System.out.println("Mysql正在删除学生信息");
}
}
  • 业务层
@Service(value = "studentService")
public class StudentService {
// @Resource(name = "studentDaoImplForMySql")
@Resource(name = "studentDaoImplForMySql")
private StudentDao studentDao;
// @Resource(name = "studentDaoImplForMySql")
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void deleteStudent(){
studentDao.deleteById();
}
}
  • 测试
@org.junit.Test
public void testNoXml(){
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Spring6Config.class);
StudentService studentService = annotationConfigApplicationContext.getBean("studentService",StudentService.class);
studentService.deleteStudent();
}
posted @   文采杰出  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示