【发布时间】: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<String, UserProfile>类型的静态成员。但这太不干净了,我不会把它作为答案发布。 -
谢谢.. 如果我用 SPEL 来解决它。是否可以 ?提前致谢。
标签: java spring functional-interface