0

I have a project which uses an old spring.jar (1.2.6),from this project, I am expected to call a newer version (spring version 5.0.7) spring boot project's method. Below is the way I am creating my bean in old version project.

I am getting NullPointer exception while creating the Autowired bean.

Create bean from XML:spring test-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "qvc-spring-beans.dtd">
<beans>
<bean name="testPci" class="com.test.client.TestPci">
</bean>
</beans>

sampleParent-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "spring-beans.dtd">

<beans> 
        
    <import resource="classpath:/com/test/test-context.xml" />
    <bean id="classA" class="com.test.A" >      
        <property name="testPci">
            <ref bean="testPci"/>
        </property>
    </bean>
</beans>

Java code old spring project:

package com.test;
public class A{

private TestPci testPci;

private ApplicationContext ctx;

public TestPci getTestService() {
        if (!StringUtils.isValid(ctx)) {
            ctx = new ClassPathXmlApplicationContext("./com/test/test-context.xml");
            
        }

        if (!StringUtils.isValid(this.testPci)) {
            if (StringUtils.isValid(ctx)) {
                testPci = (TestPci) ctx.getBean("testPci");
                TestPci testPci = (TestPci) ctx
                        .getBean("testPci");
                this.setSecureTestService(testPci);
            }
        }
        return this.getSecureTestService();
    }
    
     public TestPci getSecureTestService() {
        return testPci;
    }

    public void setSecureTestService(TestPci testPci) {
        this.testPci = testPci;
    }
    
public void methodA(){
//Calling newer code form old spring code:
testPci.testing("1", "2", "3");
}
    
    }

Calling "TestPci" class as above, but when trying to call using the above, it actually calls the "TestPci"."testing" method. But the object autowired as "testWebClientService" is returning as null. I would like to get the object created instead it returns null.

New spring version class:

@Service
@EnableConfigurationProperties(TestWebClientProperties.class)
@Configurable
public class TestPci{
    
    @Autowired
    private TestWebClientService testWebClientService;
        
    
    public Map<String, String> testing(String a, String b, String c) throws Exception {
        Map<String, String> map =  testWebClientService.find(a, b, c);
        System.out.println("**=="+map.get(0));
        return map;
    }
    }

Adding junit which is used to call the TestPci class from newer version of spring:

@RunWith(SpringJUnit4ClassRunner.class)
@EnableConfigurationProperties(TestWebClientProperties.class)
@SpringBootTest(classes = { TestWebClientService.class, TestPci.class }, webEnvironment = WebEnvironment.NONE)
public class TestJunit {
    
    
    @MockBean(name="restTemplate")
     public RestTemplate              restTemplate;
     
     @Autowired
     private TestPci testPci;
    
    @Test
    public void ff() throws Exception {
        testPci.testing("1","1","1");
    }

}
user2000189
  • 479
  • 4
  • 6
  • 22
  • The reason for autowiring to not happen is that the bean was not registered to application context . There could be multiple reasons why that happened and the code shared does not provide enough information . Please share an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) – R.G Sep 30 '21 at 03:48
  • @R.G Added more information. – user2000189 Sep 30 '21 at 04:52
  • You are using XML and an XML based `ApplicationContext`, which doesn't proces annotations by default. You need to include `` to have it pickup your `TestPci` class. Another things, you are using Spring Boot features in a non Spring Boot application, that isn't going to work either. – M. Deinum Sep 30 '21 at 05:47
  • With the updated spring versoin , how is the `TestPci` bean obtained ? . Could you please provide the complete code for the new version ? A reproducible testcase , may be ? – R.G Sep 30 '21 at 06:22
  • @R.G updated the junit test. – user2000189 Sep 30 '21 at 06:59
  • Do read documentation on [SpringBootTest.classes](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html#classes--) . Try to create a configuration class , say `Config` , annotate the same with `@Confiugration` and define the beans / component scan . Refer the same in `classes` . The problem here appears to be that the application context for test is not getting created. – R.G Sep 30 '21 at 07:38

0 Answers0