I have defined a class with annotation #Configuration and some bean methods in it. This class is inside the test folder. So when I run the application will these beans be loaded by SpringBoot?
Related
I am writing a Spring Boot integration test. Here I want to exclude several beans. So I am trying to load only as many beans as are required for my tests. For that I am using #COntextConfiguration and specifying only those classes which I want.
Despite of doing this I can see Spring Boot is still loading all other beans. Like for e.g it is loading RetryTemplateConfig which is present in a different package altogether. I used #ComponentScan to scan only current package and not the one in which RetryTemplateConfig lies. Still I get bean configuration errors. Below is my top section of my test class
#SpringBootTest
#RunWith(SpringJUnit4ClassRunner.class)
#Import(BulkExceptionClosureTestConfig.class)
#ActiveProfiles("test")
#TestPropertySource(locations = "classpath:application-test.yml", properties = {"INSTANCE_ID=1","spring.profiles.active=test","ENV=test"})
#ContextConfiguration(classes = {BulkExceptionClosureApplication.class, ClosureRequestCsvFileProcessor.class,
MetadataFetcher.class, XmlProcessingService.class})
#ComponentScan(basePackages = "com.barclays.exceptionclosure")
public class BulkExceptionClosureTest {
And I get below error when starting this test.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'retryTemplate' defined in class path resource [com/xyz/validation/config/RetryTemplateConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException
I have not included this class in my ContextConfiguration, still it gets loaded. Also #ComponentScan doesn't seem to work, as RetryTemplateConfig is in com.xyz.validation package. And my test class is in com.xyz.exceptionclosure package
What can be done to definitely load only the required beans for my test? Please not I cannot add #Profile("!test") on classes which I don;t want in Spring Boot integration test. Those are from library I cannot modify.
I have a project structure that looks like this:
In My Groovy folder contains a class marked with #Component that I Want to inject in another class inside the java folder. However spring can't find the bean to inject, when I move the class marked with #Component to the java folder it works fine. How can I make spring aware of the groovy folder?
You can use #ComponentScan at your Spring application (annotated with #SpringBootApplication) to scan components from other packages.
#ComponentScan(basePackages = "com.stackoverflow.other.package")
See https://www.baeldung.com/spring-component-scanning.
I have one test class with #TestConfiguation and inside that a bean is returned as "headerProcessor".
Now that same bean I am importing and #autowiring in the class. Now if I run this class it is working fine. I am running the test class as Spring runner
But if I run another test class getting error like "Failed to load ApplicationContext" caused by UnsatisfiedDependencyException for bean "headerProcessor".
Sorry I cannot paste the codes its restricted from the company.
I'm trying to use Spring annotations in Domino XPages Java classes. Spring works when I'm defining beans in the configuration file. However I fail to use the annotations.
To illustrate the problem, I have created two simple empty classes annotated with #Component annotation - com.geo168.a.B (#Component public class B {}) and com.geo168.a.C (#Component public class C {})
The first one I have created in Eclipse, packed and added to the application in a jar. The second one I add directly in Code/Java section.
I add a component-scan tag in the configuration file: <context:component-scan base-package="com.geo168.a"/> and try to instantiate the classes:
ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG);
// class defined in the jar, works ok
com.geo168.a.B b = ctx.getBean(com.geo168.a.B.class);
// class defined in Code/Java, throws exception
com.geo168.a.C c = ctx.getBean(com.geo168.a.C.class);
I get an error: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.geo168.a.C] is defined
Spring has found the annotated class only in the JAR file. It works if I add the bean explicitly to the configuration file:
<bean class="com.geo168.a.C"/>
A similar post: Is possible to add annotation #ManagedBean in XPages? seems to address only the particular JSF annotations (that do not work, because they are not implemented).
In the Spring documentation: http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-scanning-autodetection I find a note: The scanning of classpath packages requires the presence of corresponding directory entries in the classpath. When you build JARs with Ant, make sure that you do not activate the files-only switch of the JAR task.
Could this be somehow related? I do not know in what form Domino deploys the classes internally.
In my project in Application context "context:component-scan" is mentioned and there is a class (e.g A) which is not annotated with #Service, but declared in Application Context. And other classes are annotated with (#Service, #Component,..).
I this A class is Autowired in another class, When I execute this project, It runs fine, But it gives NoSuchBeanDefinitionException for Merger class when I execute it with jUNIT.
When i annotate A class with #Service, it is working in JUNIT also.
In Summary
Not Working
no autowire
class A{} Declared in Application context.
#Service
Class B{#Autowire A a;}
Normal run works fine, but JUNIT gives NoSuchBeanDefinitionException.
Working
#Service
Class A{} Declared in Application context.
#Service
Class B{#Autowire A a;}
JUNIT and Normal run both works fine.
Note: I can not annotate Class A with #Service, because there are many classes of this type, I just want to write JUNIT for them, Kindly suggest.
Thanks.