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

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.

Related

NoSuchBeanDefinitionException for bean defined in JavaConfig

I was using XML configuration on my project earlier, and everything was working.
We're moving gradually to java config, so right now I'm at a stage where I'm using a mix of Java and XML configs.
Here's the problem, there is a bean defined in my XML config : beanA.
<bean id="beanA" class="BeanA" />
The class BeanA has an autowired dependency on BeanB.
class BeanA {
#Autowired
BeanB beanB;
}
Earlier, this beanB was also in XML config, and it ran correctly.
Now, I have made this change, and it is no longer working :
#Configuration
class MyConfig {
#Bean
public BeanB beanB() {
return new BeanB();
}
}
Apart from adding #Configuration and #Bean annotations, is there something else required to do that I am missing?
I'm getting the following error :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beanB': Unsatisfied dependency expressed through field 'beanA';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xxxxxx.yyy.zzzzzzzzzzzz.abc.beanA' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Please note,
I have only moved beanB to be created via java config.
BeanA actually has more dependencies, so I cannot move it to java config at this stage (will be doing proper migration later).
you need to add configuration for BeanB as well in the xml configuration. as when the program is run it loads all the bean definition from xml configuration file. So it looks like BeanB definition is missing in configuraion file.
Spring does not load the beans from the classes annotated with #Configuration unless it has been told to look for classes with this annotation.
To make spring look for these classes, the following must be added in your xml configuration :
<context:component-scan base-package="com.xxxxx.yyy.zzzz">
It will then recursively find and initialize all the beans in the package com.xxxxx.yyy.zzzz.

Unable to Autowire brave.Tracer inside Spring boot Application

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.

Application Context is failing to load, as it is not finding any qualifying bean of type EntityManager

While running JUnits, ApplicationContext is not loaded, as it could not find a qualifying bean javax.persistence.EntityManager. However, the application for which the Junit is being written is working. Both the application and JUnits are using the same spring configuration.
My Configuration class looks like below
#Configuration
#EnableTransactionManagement
public class AppConfiguration {
#Autowired private EntityManager entityManager;
#Override #Bean
public CustomRepository<Person> customRepository(){
return new CustomRepository<>(Person.class, entityManager);
}
As you see, I'm able to autowire EntityManager, even though I have not explicitly defined the bean and the application is working without any issues.
Now in my JUnit
#RunWith(SpringRunner.class)
#ContextConfiguration(classes=AppConfiguration.class)
#TestPropertySource(locations = "classpath:application.properties")
public class ControllerTest {
#Autowired
private CController controller;
As you see both application and my Junit are using same configuration. How ever when running JUnit, I'm seeing
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I could not understand what in the JUnit is causing the isssue, while the aplication is still working.
Thanks
it clearly means that EntityManager bean is not defined in your application context.
So either you define it by yourself or let Spring-boot do the lifting(Spring-boot auto config your context based on dependencies in your POM file). If you choose the latter option then one option to get rid of the error by adding H2 DB.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
UPDATE:
In your test, instead of initializing your context using #ContextConfiguration do that with #SpringBootTest it will bootstrap your entire container(including other beans that are not defined in AppConfiguration.java). Read more about in spring-docs

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);

Spring boot (1.3.6) + Hibernate(5.2.1) No qualifying bean of type [org.hibernate.SessionFactory] found for dependency:

While moving into micro-services from monolithic application, upon creating the micro-services using Spring boot (1.3.6) + Hibernate(5.2.1) we got an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}.
But the same source code is running on hibernate(4.3.11). Please find the source code on
https://github.com/pandiaraj2/Spring-boot-1.3.6-Hibernate-5.2
What do I need to do to resolve this error?
Spring Boot 1.3.6 uses Spring 4.2.x, which doesn't support Hibernate 5.2. You also have to upgrade Spring to 4.3, e.g. by adding the following property in your pom.xml:
<properties>
<spring.version>4.3.1.RELEASE</spring.version>
</properties>
But it still might be, that some of the autoconfigurations are not working, because full Hibernate support in Spring Boot will only be introduced in Spring Boot 1.4, which is still in release candidate.
i guess you need to add #EnableJpaRepositories annotation on your main class DtcmwsApplication and also need to remove exclusion for hibernate-entitymanager

Resources