Spring @Autowired注解用在集合上面,可以保持接口的所有实现类
CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl
public interface CourseService {
String getCourseName();
void attendCourse();
}
package com.junge.spring.demo.service.course.impl; import com.junge.spring.demo.service.course.CourseService;
import org.springframework.stereotype.Service; import java.util.logging.Logger; /**
* 历史课
*
* @author liuxj
* @create 2018-01-06 12:46
*/
@Service
public class HistroyCourseServiceImpl implements CourseService { private static final Logger logger = Logger.getLogger(HistroyCourseServiceImpl.class.getName()); @Override
public String getCourseName() {
return "历史";
} @Override
public void attendCourse() {
System.out.println("上历史课");
logger.info("上历史课");
}
}
package com.junge.spring.demo.service.course.impl; import com.junge.spring.demo.service.course.CourseService;
import org.springframework.stereotype.Service; import java.util.logging.Logger; /**
* 数学课
*
* @author liuxj
* @create 2018-01-06 12:49
*/
@Service
public class MathsCourseServiceImpl implements CourseService {
private static final Logger logger = Logger.getLogger(MathsCourseServiceImpl.class.getName()); @Override
public String getCourseName() {
return "数学课";
} @Override
public void attendCourse() {
System.out.println("上数学课");
logger.info("上数学课");
}
}
学生类有接口attendAllCourse,参加所有课程
public interface StudentService {
void addStudent();
Student getStudent(int id);
void updateStudent(Student student);
void attendAllCourse(Student student);
}
package com.junge.spring.demo.service.impl;
import com.junge.spring.demo.model.Student;
import com.junge.spring.demo.service.StudentService;
import com.junge.spring.demo.service.course.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Set;
import java.util.logging.Logger;
/**
* 业务实现类
*
* @author liuxj
* @create 2018-01-06 11:24
*/
@Service
public class StudentServiceImpl implements StudentService {
private static final Logger logger = Logger.getLogger(StudentServiceImpl.class.getName());
/**
* @Autowired注解用在集合上面,可以保持接口的所有实现类
*/
@Autowired
private Set<CourseService> courseSet;
@Override
public void addStudent() {
System.out.println("addStudent");
logger.info("addStudent");
}
@Override
public Student getStudent(int id) {
System.out.println("getStudent id=" + id);
logger.info("getStudent id=" + id);
Student student = new Student();
student.setId(id);
student.setAge(22);
student.setName("张胜男");
return student;
}
@Override
public void updateStudent(Student student) {
System.out.println("updateStudent info:" + student);
logger.info("updateStudent info:" + student);
}
@Override
public void attendAllCourse(Student student) {
if (!CollectionUtils.isEmpty(courseSet)) {
for (CourseService service : courseSet) {
service.attendCourse();
}
}
}
}
调用学生上课接口,可以打印所有课程,说明CourseService所有的实现类都注册到集合courseSet
Spring @Autowired注解用在集合上面,可以保持接口的所有实现类的更多相关文章
- Spring课程 Spring入门篇 4-3 Spring bean装配(下)之Autowired注解说明2 集合运用
课程链接: 本节主要讲了以下几块内容 1 注解相关解析 2 代码演练 集合for循环的使用 2.1 list集合应用 2.2 map集合应用 2.3 集合排序(只对list有效,对map无效(list ...
- Spring @Autowired 注解 学习资料
Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...
- Spring @Autowired 注解自动注入流程是怎么样?
面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...
- Spring@Autowired注解
@Autowired注解可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作. 注意:@Autowired默认是按照类型来注入的. 看下面的例子:例子是以对成员变量(field)为例进行的 p ...
- Java集合框架之四大接口、常用实现类
Java集合框架 <Java集合框架的四大接口> Collection:存储无序的.不唯一的数据:其下有List和Set两大接口. List:存储有序的.不唯一的数据: Set:存储无序的 ...
- Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- 【转】Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- Spring @Autowired注解在非Controller中注入为null
问题描述 今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, ...
- Spring@Autowired注解与自动装配(转发)
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
随机推荐
- 离线在MVC4中的使用
最近在项目中用到了离线功能,自己感觉挺高端的,但是遇到了一些问题. 现在工作告一小段落,就把这次离线工作中遇到的关键技术点和难题记录下来. 什么是离线,什么时候需要离线 离线:Offline,不联网也 ...
- VS2010配置OpenGL开发环境
OpenGL(Open Graphics Library)是一个跨编程语言.跨平台的专业图形程序接口.OpenGL是SGI公司开发的一套计算机图形处理系统,是图形硬件的软件接口,任何一个OpenGL应 ...
- linux学习第四天 (Linux就该这么学)2018年11月16日
今天主要讲了 管道符,重写向与环境变量 输入输出重写向 标准输出重写向 (标准,覆盖,错误) > 将标准输出重写向到一个文件中 >> 追加到文件 2>错误输出重定向 2> ...
- Statement、PreparedStatement、CallableStatement的区别
此三个接口的声明如下: public interface Statement extends Wrapper, AutoCloseable public interface PreparedState ...
- c sharp multithreading
1. 静态方法 using System; using System.Threading; namespace PlusThread { class Program { static void Ma ...
- 爬虫初窥day4:requests
Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 ...
- PHP数组对象互转
//数组转对象 function array2object($array) { if (is_array($array)) { $obj = new StdClass(); foreach ($arr ...
- Java类加载机制及自定义加载器
转载:https://www.cnblogs.com/gdpuzxs/p/7044963.html Java类加载机制及自定义加载器 一:ClassLoader类加载器,主要的作用是将class文件加 ...
- ServiceDesk Plus更有序地组织IT项目
- 前端html的简单认识
一.html 超文本标记语言 hypertext markup language 二.html的结构 三.html标签格式 1.标签由<>把关键字括起来 2.标签通常是成对出现的 , eg ...