No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator] - spring

I started to develop a Jira External System Importer Plugin and I should upload a JSON File using its Setup Page but when I add "TemporaryAttachmentsMonitorLocator" to my SetupPage class constructor and I click on my importer's button, it fails with the error: "No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator]"
[INFO] [talledLocalContainer] org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dev.addax.jira.plugins.trello2jira.web.TrelloSetupPage': Unsatisfied dependency expressed through constructor argument with index 4 of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator]: No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport(value=)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: **No qualifying bean of type [com.atlassian.jira.issue.TemporaryAttachmentsMonitorLocator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency**. Dependency annotations: {#com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport(value=)}
[INFO] [talledLocalContainer] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
public class TrelloSetupPage extends AbstractSetupPage {
private final TemporaryAttachmentsMonitorLocator locator;
public TrelloSetupPage(#ComponentImport UsageTrackingService usageTrackingService, #ComponentImport WebInterfaceManager webInterfaceManager,
#ComponentImport PluginAccessor pluginAccessor, #ComponentImport EventPublisher eventPublisher,
#ComponentImport TemporaryAttachmentsMonitorLocator locator) {
super(usageTrackingService, webInterfaceManager, pluginAccessor, eventPublisher);
this.locator = locator;
}
// ... other content omitted.
}
The other params in my constructor are resolved right. Only this one is what it fails. If I remove that class I can navigate to my SetupPage as is expected.
Finally, the class is exported by the OSGi system bundle and I checked it is even still used as a service by the Jira native JIM Plugin.
Any idea what might be the reason why it is not finding this class?

I removed #ComponentImport TemporaryAttachmentsMonitorLocator locator and I included
ComponentAccessor.getOSGiComponentInstanceOfType(TemporaryAttachmentsMonitorLocator.class) in the constructor body.
I don't know why #ComponentImport does not work for this case but the workaround works like a charm.

Related

No qualifying bean of type 'org.springframework.data.redis.support.atomic.RedisAtomicLong' available: expected at least 1 bean as autowire candidate

I have a common library and common library is using redis jar:
package com.service.dao.impl;
RedisImpl.class
#Profile
#Log
#Named
public class RedisImpl implements RedisDao {
#Autowired
#Qualifier("ECID")
private RedisAtomicLong ECID;
// this field will be errored out
}
after I create a common jar and importing this common library in another project:
I get an error as:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type'org.springframework.data.redis.support.atomic.RedisAtomicLong'
available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=ECID)}
I tried several options with
#EnableRedisRepositories(basePackageClasses = { SequenceNumberRedisDaoImpl.class })
#ComponentScan
no luck.... How do I resolve this issue . How can I autowired the third party library field through commons library?

Spring Data JDBC UnsatisfiedDependencyException

I wanted to move from JdbcTemplate to Spring Data JDBC. However I seem to have some misconfiguration but I cannot figure out where. The errors are "expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}" and "Parameter 0 of constructor ... required a bean ... that could not be found."
I put #Repository on the public repository interfaces extending PagingAndSortingRepository (as I did with the DAO classes extending from JdbcDaoSupport) without success. Then I added #EnableJdbcRepositories with and without package name to the database config class, also no success. I also tried the database config to inherit from AbstractJdbcConfiguration, still the same errors ...
Unfortunately I couldn't find a working example and I now gave up after some trial and error. I still would love to get this working, the version I used is spring-boot-starter-data-jdbc:2.4.0
Code fragments:
DatabaseConfiguration.java
#Configuration
#EnableJdbcRepositories("<basepackage>.repository.jdbc")
#EnableJdbcAuditing(auditorAwareRef = "springSecurityAuditorAware")
#EnableJpaRepositories("<basepackage>.repository")
#EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
#EnableTransactionManagement
#EnableElasticsearchRepositories("<basepackage>.repository.search")
public class DatabaseConfiguration extends AbstractJdbcConfiguration {
}
UserRepository.java
#Repository
public interface UserRepository extends PagingAndSortingRepository<User, String> {
}
QualityResource.java (REST Controller)
public QualityResource(UserRepository userRepository) {
this.userRepository = userRepository;
}
Error messages:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qualityResource' defined in file [.../backend/build/classes/java/main/.../web/rest/QualityResource.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '....repository.jdbc.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Application failed to start: Description: Parameter 0 of constructor in <basepackage>.web.rest.QualityResource required a bean of type '<basepackage>.repository.jdbc.UserRepository' that could not be found.

Kotlin application.yml data class looking for beans

I have a Kotlin data class that I want to read properties from application.yml file. Here is my data class:
#ConstructorBinding
#ConfigurationProperties("meanwhile.in.hell.myapp")
data class MyAppProperties(val serverId: String, val locationId: String)
I then added it to my configuration class:
#Configuration
#EnableConfigurationProperties(MyAppProperties::class)
open class MyAppConfiguration(private val properties: MyAppProperties) {
where I access the values using just properties.serverId and pass the object into the constructor of other beans being created, such as this one:
open class MyAppClient(
private val webClient: WebClient,
private val properties: MyAppProperties
) : IMyAppClient {
However, when I start up my application I get an error that instead of trying to load the properties from application.yml, it is trying to find beans for the constructor params:
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'meanwhile.in.hell.myapp-meanwhile.in.hell.myapp.MyAppProperties': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Parameter 0 of constructor in meanwhile.in.hell.myapp.MyAppProperties required a bean of type 'java.lang.String' that could not be found.
How do I stop my app thinking that these params are Autowired? I know that in Kotlin, this is how a constructor Autowired bean looks like (ie, not requiring the annotation), but all example I have seen online on how to read application.yml properties looks the same as my data class.
Spring-Boot v2.3.0.RELEASE
Kotlin v1.3.72
Turns out I was missing the dependency
org.jetbrains.kotlin:kotlin-reflect
Docs:
https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#boot-features-kotlin-requirements
Issue:
https://github.com/spring-projects/spring-boot/issues/19582

Junit in Spring Boot keeps failing

I have very simple SpringBoot Junit but it keeps failing.
#RunWith(SpringRunner.class)
//#SpringBootTest
#WebMvcTest(TokenServiceImpl.class)
public class TokenTest {
#Test
public void getOauthToken()
{
System.out.println( " done test");
}
my TokenServiceImpl class has
public class TokenServiceImpl implements TokenService{
public String getToken() throws RuntimeException{
return " Token returned" ;
}
I get the below error : -Snippet
2019-11-27 19:12:45.884 WARN 15864 --- [ main]
o.s.w.c.s.GenericWebApplicationContext : Exception encountered
during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'runApplication': Unsatisfied dependency
expressed through field 'job'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'job' defined in class path resource
[com/mycompany/project1/batch/configurations/BatchBDREntityConfig.class]:
Unsatisfied dependency expressed through method 'job' parameter 4;
nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'batchDBWriter': Unsatisfied dependency
expressed through field 'BDREntityRepository'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'com.mycompany.project1.batch.repositories.BDREntityRepository'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Field BDREntityRepository in
com.mycompany.project1.batch.services.BatchDBWriter required a bean of
type 'com.mycompany.project1.batch.repositories.BDREntityRepository'
that could not be found.
Consider defining a bean of type
'com.mycompany.project1.batch.repositories.BDREntityRepository' in
your configuration.
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'runApplication': Unsatisfied dependency
expressed through field 'job'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'job' defined in class path resource
[com/mycompany/project1/batch/configurations/BatchBDREntityConfig.class]:
Unsatisfied dependency expressed through method 'job' parameter 4;
nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'batchDBWriter': Unsatisfied dependency
expressed through field 'BDREntityRepository'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'com.mycompany.project1.batch.repositories.BDREntityRepository'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'batchDBWriter': Unsatisfied dependency
expressed through field 'BDREntityRepository'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'com.mycompany.project1.batch.repositories.BDREntityRepository'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'com.mycompany.project1.batch.repositories.BDREntityRepository'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
My main method has following
#SpringBootApplication
#ComponentScan(basePackages= {"com.mycompany.project1"})
public class RunApplication{
private static final Logger logger = Logger.getLogger(BatchController.class);
#Autowired
JobLauncher jobLauncher;
#Autowired
Job job;
/*
* jmxBean will get loaded when this managed bean is called from RMI, and it will fire batch execution
*/
public static void main(String[] args){
ApplicationContext app = SpringApplication.run(RunApplication.class, args);
logger.debug("Batch Application has been started...");
BatchController controller = app.getBean(BatchController.class);
//Start batch process when application starts or restart, its optional call and can be commented out
//as we have JMX to expose load method on demand
//BatchController batch = new BatchController();
controller.startBatchProessFromMain();
}
}
#Component
public class BatchDBWriter implements ItemWriter<BDREntity> {
private static final Logger logger = Logger.getLogger(BatchDBWriter.class);
#Autowired
private BDREntityRepository bDREntityRepository;
/*
* this method call the JPArepository's saveAll
* function and saves all the list of bdrEntitiesat a time.
*/
#Override
public void write(List<? extends BDREntity> bdrEntities) throws Exception {
bDREntityRepository.saveAll(bdrEntities);
}
}
How can i fix my Junit ?
You are using #WebMvcTest that is a test-slice annotation. It's specifically intended for testing the WebMvc-related components in your application and is a form of integration test.
You've said in the comments that you're not trying to integration test your application. In that case, you should not be using #SpringBootTest or any of the other #…Test annotations that are provided by Spring Boot.
Assuming that your goal is to unit test TokenServiceImpl, I'd expect your test class to look something like this:
public class TokenTest {
private final TokenServiceImpl tokenService = new TokenServiceImpl();
#Test
public void getOauthToken() {
String token = this.tokenService.getToken();
// Assertions to check the token go here
}
}

Spring: how to ignore #Autowired property if bean is not defined

Situation: I have I class with property annotated with #Autowired:
public class MyClass {
#Autowired
protected MyAutoWiredBean myAutowiredBean;
}
Is there any possibility to made wiring this bean optional, i.e. if such bean is defined in some configuration file - to wire it, but if such bean is not defined - just continue working without throwing:
org.springframework.beans.factory.BeanCreationException:
Could not autowire field: protected MyAutoWiredBean...;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.mypackage.MyAutoWiredBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Have you tried:
#Autowired(required=false)
Javadoc:
Declares whether the annotated dependency is required. Defaults to true
you can set required attribute like :
#Autowired(required=false)
http://static.springsource.org/spring/docs/2.5.5/api/org/springframework/beans/factory/annotation/Autowired.html

Resources