码迷,mamicode.com
首页 > 编程语言 > 详细

Spring @Autowired注解用在集合上面,可以保持接口的所有实现类

时间:2018-01-06 15:52:10      阅读:460      评论:0      收藏:0      [点我收藏+]

标签:ret   service   over   inf   ide   static   dal   调用   system   

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注解用在集合上面,可以保持接口的所有实现类

标签:ret   service   over   inf   ide   static   dal   调用   system   

原文地址:https://www.cnblogs.com/junge8618/p/8213953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!