Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

我利用Spring 类优化了一下 #2

Copy link
Copy link
@lhsaq2009

Description

@lhsaq2009
Issue body actions

不太信任:ClassScaner.java 和 BeanTool.java 这两个类

public interface AbstractOrderHandler {
    String handle(OrderDTO dto);
    String type();
}
@Component
public class LitbHandler implements AbstractHandler {

    @Override
    public String handle(OrderDTO dto) {
        return OrderType.litb.toString();
    }

    @Override
    public String type() {
        return OrderType.litb.toString();
    }
}
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    public ApplicationContext applicationContext;

    @Override
    public String handle(OrderDTO dto) {
        String orderType = String.valueOf(dto.getType());

        // Spring 有个 Map Cache 根据 class 来缓存 bean Name, 此处无需担忧性能
        Map<String, AbstractOrderHandler> beans = applicationContext.getBeansOfType(AbstractOrderHandler.class);
        beans.values().stream()
                .filter(x -> x.type().equals(orderType))
                .findFirst()
                .orElseThrow(() -> new RuntimeException("not found handler for type: " + orderType))
                .handle(dto);

        return null;
    }
}

后来发现用 注解 有点脱了裤子放屁的感觉。
绕来绕去的,除了增加阅读复杂度

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.