【问题标题】:Is it possible to access service class method from functional interface in Spring Java 8?是否可以从 Spring Java 8 中的功能接口访问服务类方法?
【发布时间】:2021-01-13 11:36:54
【问题描述】:

我想从 Spring Java 8 中的功能接口访问服务类方法。可以吗?

public interface UserProfile extends Supplier<UserProfileDto> {

//  This is working
    static UserProfile getMyId() {
        return () ->
                new UserProfileDto(
                        SecurityContextHolder.getContext().getAuthentication().getName(),
                        "", "", "");
    }

    static UserProfile getMyProfile() {
        return () -> 
    }
}

@Service("userProfileService")
public class UserProfileServiceImpl implements UserProfileService {

    private final UserProfileRepo userProfileRepo;

    @Autowired
    public UserProfileServiceImpl(@Qualifier("userProfileSql") UserProfileRepo userProfileRepo) {
        this.userProfileRepo = userProfileRepo;
    }

    @Override
    public Optional<UserProfileDto> getUserProfileById(String userId) {
        return userProfileRepo.selectUserProfileById(userId);
    }
}

我想在 UserProfile 的 getMyProfile() 中调用服务类方法“getUserProfileById(SecurityContextHolder.getContext().getAuthentication().getName())”。有可能吗?

【问题讨论】:

  • 不,这是不可能的。
  • 没有。它是一个接口方法,所以没有。
  • 您可以在初始化后从服务中设置的UserProfile 中定义Function&lt;String, UserProfile&gt; 类型的静态成员。但这太不干净了,我不会把它作为答案发布。
  • 谢谢.. 如果我用 SPEL 来解决它。是否可以 ?提前致谢。

标签: java spring functional-interface


【解决方案1】:

不,你不能从功能接口访问服务类方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多