Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection without using spring framework

I have a rather naive question. Can we inject dependencies using core java just like how we inject using Spring framework?

For now, I do something like this:

In web.xml:

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

spring applicationcontext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="mybean" class="com.test.app.MyService" />

</beans>

Class where I will use the injected bean:

public class MyResource {

    @Autowired
    private MyService mybean;

     public MyResponse doService(MyRequest req) {
           mybean.doBusiness(req);
     }
}

}

So, is there a way we can do this dependency injection using core java? I have been reading a bit on CDI but didn't understand it well. Plus, it also felt like it was not a direct substitution of what Spring does.

Please help and correct me if I am wrong.

like image 354
Aravind Datta Avatar asked Mar 19 '14 15:03

Aravind Datta


People also ask

Do I need a framework for dependency injection?

Dependency Injection is a pattern, not any particular framework. You could even implement it manually, but that would be too much trouble.

Is Di possible without Spring?

Dependency Injection is a concept first and then a framework. When the application under question is small, we can always meet the needs by injecting dependencies manually, without using any framework like Spring.

Which dependency injection is not possible in Spring?

What About Optional Dependencies? With setter injection, Spring allows us to specify optional dependencies by adding @Autowired(required = false) to a setter method. This is not possible with constructor injection since the required=false would be applied to all constructor arguments.

What is the alternative to dependency injection?

An alternative to dependency injection is using a service locator. The service locator design pattern also improves decoupling of classes from concrete dependencies. You create a class known as the service locator that creates and stores dependencies and then provides those dependencies on demand.


2 Answers

javax.inject (JSR-330) and CDI (JSR-299, JSR-346) are just APIs, not implementations. There are implementations of these APIs that do work in Java SE (as opposed to Java EE, which I suppose is what you mean by "core Java"), e.g. Weld SE.

CDI builds upon JSR-330.

While javax.inject is only about basic injection and does not address scopes and lifecycles, it is fair to say that CDI and Spring Core are more or less equally powerful. Both can be used in Java SE.

like image 66
Harald Wellmann Avatar answered Nov 07 '22 10:11

Harald Wellmann


@Autowired is a Spring annotation that is not standardized. The corresponding standard annotation is @Inject from javax.inject

Aside from Spring, Google Guice supports also javax.inject

Another extremely lightweight framework for dependency injection (that does not support javax.inject) is PicoContainer

like image 31
geoand Avatar answered Nov 07 '22 11:11

geoand