GionJh
GionJh

Reputation: 2894

@Autowire using singleton vs prototype

Take a look at the following code:

@Controller
public class HomeController
{   
    @Autowired
    private FacadeRemote fr;

// code omitted   
}

|

@Component("user-details")
public class CustomUserServiceDetails implements UserDetailsService
{

    @Autowired
    private FacadeRemote fr;

    @Override
    public UserDetails loadUserByUsername(String email)
            throws UsernameNotFoundException
    {
        // omitted
    }

}

configuration:

<!-- JNDI Properties -->
    <util:properties id="jndiProps">
        <beans:prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory</beans:prop>
        <beans:prop key="java.naming.factory.url.pkgs">org.jboss.ejb.client.naming</beans:prop>
        <beans:prop key="java.naming.provider.url">remote://127.0.0.1:4447</beans:prop>
        <beans:prop key="java.naming.client.ejb.context">true</beans:prop>
        <beans:prop key="boss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT">false</beans:prop>
    </util:properties>


    <!-- EJB look up -->
    <jee:remote-slsb id="ns" jndi-name="Layer//myejb!com.uni.ag.FacadeRemote"
        environment-ref="jndiProps" resource-ref="false"
        business-interface="com.uni.ag.FacadeRemote"
        lookup-home-on-startup="true">
    </jee:remote-slsb>

In the following example how does @Autowire works:
Are the two classes using the same instance of FacadeRemote ?
In general how can I learn @Autowire behaviour in relation to thread-safety ?

Upvotes: 0

Views: 699

Answers (1)

Arek
Arek

Reputation: 3176

There is almost no relation between @Autowired and thread-safety. The only responsibility of Spring IoC is to inject declared dependencies, taking into account their scope etc. It's your responsibility to make sure, that all beans will work correctly in multi-threaded applications. It means for example that for singleton beans all state that this bean holds should be synchronized.

Regarding your first question: there will be injected a proxy to FacadeRemote (to allow for example transations of checked exception RemoteException to an unchecked exception). According to the Spring Spec inside this proxy will be a cached instance of remote interface to FacadeRemote.

Upvotes: 2

Related Questions