Spring-free injection

I have a rather naive question. Can we embed dependencies using the java core in the same way as we implement using the Spring framework?

I am currently doing 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>

The class where I will use the entered bean:

public class MyResource {

    @Autowired
    private MyService mybean;

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

}

So, is there a way to do this dependency injection using the java kernel? I read a little CDI, but did not understand it well. In addition, he also felt that it was not a direct replacement for what Spring is doing.

Please help and correct me if I am wrong.

+4
source share
6 answers

- . , , Spring , Inversion of Control.

, Java SE Java EE Inversion of Control.

Java EE API CDI . , Weld, .

IoC.

+1

HK2, JSR-330 JDK. JDK, . . :

HK2

+1

javax.inject (JSR-330) CDI (JSR-299, JSR-346) - API, . API, Java SE ( Java EE, , , , " Java" ), . Weld SE.

CDI JSR-330.

javax.inject , , CDI Spring Core . Java SE.

+1

, javax.inject. API , , .

Guava Dagger.

@Autowired @Inject javax.inject ; Java (, , , ).

, ...

0

@Autowired - Spring, . - @Inject javax.inject

Spring, Google Guice javax.inject

( javax.inject) PicoContainer

0

, ! JSR 330 .

You can also see Spring documentation .

0
source

Source: https://habr.com/ru/post/1532501/


All Articles