I am trying to autowire Optional in a component. Object is in custom ThreadScope and proxy-mode = INTERFACES.
I think @Autowired Optional<ObjectType> objectTypeOptional
just ensures that Autowiring is not requried.
Now objectTypeOptional.isPresnet()
always returns true. Because there is a proxy for ObjectType. Is there a way to say that objectTypeOptional is not present?
Post Edit (Adding more context to the question):
My bean is something like this -
@Component
public User {
private final userName;
private final userEmail;
// constructors and getters
}
Now I want to inject Optional of User in any service or controller. Something like -
@Autowired Optional<User> userOptional;
So whenever a user is logged in, a filter/interceptor will create an appropriate User
object. But when no user is logged in I should be able to check it via optional -
userOptional.isPresent()