As it says if I take out the @Lazy annotation my class won't get injected.
SomeClassTest
package com.somePackage;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.somePackage.SomeClass;
@Configuration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath*:junit-spring-context.xml")
public class SomeClassTestTest {
@Autowired
@Lazy // If i take this out, the test fails
private SomeClass someClass;
@Test
public void someTest() {
assertNotNull(someClass);
}
}
SomeClass
package com.differentPackage;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.DependsOn;
@Service("someClass")
@DependsOn("someOtherClass")
public class SomeClass {
// Bunch of code here
}
SomeOtherClass
package com.someOtherPackage.config
import org.springframework.context.annotation.Configuration;
@Configuration
public class SomeOtherClass {
// Just more code
}
junit-spring-context
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Detect classes annotated with @Repository, @Component and @Service and register them as beans -->
<context:component-scan base-package="com.differentPackage" />
</beans>
Why does it only work with @Lazy being present? Btw SomeClassTest and SomeClass are in different packages if that helps!