Unable to Autowire brave.Tracer inside Spring boot Application - spring-boot

I am working on Spring boot application and I tried to Autowire Tracer object to get the traceId, but its raised the following exception. why??
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'brave.Tracer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I used the Tracer in a lot of projects and its always working with no issues!!

Spring boot container is not able to resolve the implementation of your autowired interface in this case. Please annotate your implementation class with spring stereotype annotations.
For e.g We provide #Reposiory for dao classes, #Service for service classes & #Component as a generic one. This will solve your problem. If you still face any issues, Just share your code snippet.

Related

How to autowire a spring DAO repository in its unit test?

I am experimenting Spring's reactive support for DB operations. So I have created a repository as:
public interface FeatureRepository extends ReactiveCrudRepository<Feature, UUID> {}
Now I want to test it through a unit test.
So my test is:
#ExtendWith(SpringExtension.class)
#SpringBootTest
public class FeatureRepositoryTest {
#Autowired
FeatureRepository featureRepository;
.....
}
But I get error:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of
type 'x.y.z.FeatureRepository' available: expected at least 1 bean which
qualifies as autowire candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
How can I solve this issue?
Your DAO is managed as any other tested class, so without defining a specific ApplicationContext in your test runtime, Spring do nothing and the FeatureRepository bean is not known.
2 approaches :
Your test is more about integration test, so your test runtime need to be specify.
Your test is more about unit test, I suggest you to mock your repository (pragrammatically or using framework like Mockito)
Regards.

spring statemachine data jpa sample issues

I am trying to get the spring statemachine data jpa sample working and I am unable to. I have created a sample github project here. So far, I have only added the necessary dependencies and getting this error:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.statemachine.data.StateRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound (DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency (DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency (DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject (AutowiredAnnotationBeanPostProcessor.java:585)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject (InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues (AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean (AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:483)
Maybe you already figured this out but you're missing jpa libs. Those can be added i.e. using spring-boot-starter-data-jpa

Spring initialization done outside the project

I have a java spring project . I see that one way of initialing the spring project is using this code in the main method.
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(Config.class);
ctx.scan("com.example.db.app");
ctx.refresh();
Is it possible to keep this outside a main method and then make a jar of this project. Add it as a dependency in pom.xml in other project and call the method which initializes the spring artifacts from there.
I tried doing it. I am getting an error.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemInformationRepositoryService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.example.db.app.service.ItemInformationRepositoryService.setItemInformationRepositoryService(com.example.db.app.repository.ItemInformationRepository); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.db.app.repository.ItemInformationRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
The exception message states:
No qualifying bean of type [com.example.db.app.repository.ItemInformationRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
This means that the class com.example.db.app.repository.ItemInformationRepository is not in the Spring context. Perhaps you were expecting Spring to discover this class as a result of your instruction to Spring to scan com.example.db.app? According to the Javadocs for AnnotationConfigApplicationContext.scan() ...
Perform a scan within the specified base packages.
#param basePackages the packages to check for annotated classes
So, in order for Spring to discover com.example.db.app.repository.ItemInformationRepository you must either:
Annotate it with org.springframework.stereotype.Component so that it is discovered by scan()
Register it in the same way as you are registering Config.class e.g. ctx.register(ItemInformationRepository.class);

Use CDI ConversationScoped beans in Spring Controllers

I'm trying to make webapp which should use thymeleaf with spring controllers. But I'd like to have some CDI ConversationScoped beans injected into my Spring controller. For now I managed to configure CDI with my Spring application I when I tried to incject CDI bean into my controller it seems to work fine, but when I tried to inject Conversation bean it fails with error:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.enterprise.context.Conversation] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#javax.inject.Inject()}
In CDI 1.0 the conversation scope is tied to JSF. If you're not using JSF you won't be able to access the conversation scope. You could create another scope which mimics the conversation scope though.

JBoss - Autowiring list - #Resource, #Inject throws NoSuchBeanDefinitionException

Env - JBoss 5.1, Spring 3.1.2, jdk 1.5
I am trying to autowire a list. As per the discussion here Auto-wiring a List using util schema gives NoSuchBeanDefinitionException, #Resource doesn't work with JBoss.
<util:list id="reportTypes">
<value>PDF</value>
<value>CSV</value>
<value>XML</value>
</util:list>
#Inject
private List<String> reportTypes;
I also tried using #Inject, but I get the same exception. What am I missing ?
I am using the JSR 330, com.springsource.javax.inject-0.9.0.PFD.jar file for #Inject as mentioned here http://forum.springsource.org/showthread.php?78737-JSR-330-Inject-Spring-3-0
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [java.lang.String] found for dependency
[collection of java.lang.String]: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {#javax.inject.Inject()}
EDIT using
#Resource(name = "reportTypes")
private List<String> reportTypes;
throws
java.lang.RuntimeException: mapped-name is required for reportTypes of deployment app.war
at org.jboss.web.tomcat.service.injection.WebResourceHandler.loadXmlResourceEnvRefs(WebResourceHandler.java:287)
at org.jboss.web.tomcat.service.injection.WebResourceHandler.loadXml(WebResourceHandler.java:325)
at org.jboss.web.tomcat.service.TomcatInjectionContainer.processMetadata(TomcatInjectionContainer.java:550)
at org.jboss.web.tomcat.service.WebCtxLoader.start(WebCtxLoader.java:158)
I've battled with this problem a few times myself. I don't have an exact answer to your question, other than the fact that I've moved to using #Inject #Named pairs. That seems to work most of the time.
The reason #Resource doesn't work, to my knowledge, is not the fault of jboss itself, but the fault of the version of tomcat bundled by jboss.

Resources