Why won't my variable auto-connect?

For some reason, the pointer of the object I'm trying to create using @Autowired is never created. I tried to consider a few examples, but nothing works! Here is my code:

Testing.java

package com.example.core.service.integration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(locations={"/app-context.xml"}) public class Testing { @Autowired private IntegrationRestService integrationRestService; public static void main(String args[]) { Testing t = new Testing(); t.checkNull(); } private void checkNull() { if(integrationRestService == null) System.err.println("FAIL..."); else System.out.println("SUCCESS!"); } } 

IntegrationTestService.java

 public interface IntegrationRestService { public FindSomething getFindSomethingResponse(String a, int b, int c); public FindSomethingElse getFindSomethingElseResponse(String urlToRead); } 

IntegrationRestServiceImpl.java

 @Service @Path("/test") public class IntegrationRestServiceImpl implements IntegrationRestService { public IntegrationRestServiceImpl() { super(); } ... } 

application-context.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" default-autowire="constructor" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- JSR-303 support will be detected on classpath and enabled automatically --> <context:annotation-config/> <context:component-scan base-package="com.example.core"/> <bean id="IntegrationRestService" class="com.example.core.service.integration.IntegrationRestServiceImpl" /> <bean id="Testing" class="com.example.core.service.integration.Testing" /> </beans> 

Any ideas what I'm doing wrong?

Answer:

Testing.java

 @Service public class Testing { @Autowired private IntegrationRestService integrationRestService; public static void main(String args[]) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml"); Testing testing = (Testing) context.getBean(Testing.class); testing.checkNull(); } private void checkNull() { if(integrationRestService == null) System.err.println("FAIL..."); else System.out.println("SUCCESS!"); } } 

application-context.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" default-autowire="constructor" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- JSR-303 support will be detected on classpath and enabled automatically --> <context:annotation-config/> <context:component-scan base-package="com.example.core"/> <bean id="testing" class="com.example.core.service.integration.Testing"/> <bean id="integrationRestService" class="com.example.core.service.integration.IntegrationRestServiceImpl" /> </beans> 
+4
source share
4 answers

Try the following:

 @Service public class Testing { @Autowired private IntegrationRestService integrationRestService; public static void main(String args[]) { final AbstractApplicationContext context = new ClassPathXmlApplicationContext("/app-context.xml"); Testing t = context.getBean(Testing.class); t.checkNull(); } private void checkNull() { if(integrationRestService == null) System.err.println("FAIL..."); else System.out.println("SUCCESS!"); } } 

@ Only works with spring beans.

+3
source

Your object is not a Spring bean, since you create it yourself.

You do not have code to initialize your application. @ContextConfiguration , AFAIK, is used only for unit testing.

Spring is not magical, you need to call it before it works.

If you use the main method, you need to create your applicationContext yourself, and then get a bean from it.

 ApplicationContext ctx = new ClasspathXmlApplicationContext("/app-context.xml"); Testing testing = (Testing) ctx.getBean("testing"); 
+2
source

beans will only be introduced in spring-installed instances. Since you are creating an instance of Testing using the new operator, it will not embed integrationRestService .

what you can do is

  • Get the Testing instance from the application context.

     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); Testing testing = (Testing) context.getBean(Testing.class); 
  • Use the @Configurable annotation in the Testing class so that instances created using the new operator are also spring managed. But this is a rather cumbersome process.
0
source
 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); Testing testing = (Testing) context.getBean(Testing.class); 

well done!

but use

 @Autowired private Testing testing; 

fails.

-1
source

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


All Articles