Autowiring Optional<object> in Spring 4.1 -
i trying autowire optional in component. object in custom threadscope , proxy-mode = interfaces.
i think @autowired optional<objecttype> objecttypeoptional
ensures autowiring not requried. objecttypeoptional.ispresnet()
returns true. because there proxy objecttype. there way objecttypeoptional not present?
post edit (adding more context question):
my bean -
@component public user { private final username; private final useremail; // constructors , getters }
now want inject optional of user in service or controller. - @autowired optional<user> useroptional;
so whenever user logged in, filter/interceptor create appropriate user
object. when no user logged in should able check via optional - useroptional.ispresent()
i believe provider more fit you.
you autowire
@resource provider<usersconnectionrepository> usersconnectionrepositoryprovider;
and have method or call get()
on provider
private usersconnectionrepository getuserconnectionrepository() { return usersconnectionrepositoryprovider.get(); }
also can @autowired(required = false)
.
Comments
Post a Comment