Bamboo ProcessService bean does not exist? - java

Following https://developer.atlassian.com/bamboodev/bamboo-tasks-api/executing-external-processes-using-processservice I would like to invoke some command using ProcessService bean. The injection as described in the link, does not work.
I checked the source of several other plugins at Bitbucket, but each is using the concept as described in the link.
My class:
import com.atlassian.bamboo.process.ProcessService;
public class CheckTask implements TaskType {
private final ProcessService processService;
public CheckTask(#NotNull final ProcessService processService) {
this.processService = processService;
}
However Bamboo does not find the ProcessService bean and fail with following:
(org.springframework.beans.factory.UnsatisfiedDependencyException :
Error creating bean with name 'bamboo.tasks.CheckTask': Unsatisfied
dependency expressed through constructor argument with index 0 of type
[com.atlassian.bamboo.process.ProcessService]: : No qualifying bean of
type [com.atlassian.bamboo.process.ProcessService] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {}; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [com.atlassian.bamboo.process.ProcessService]
found for dependency: expected at least 1 bean which qualifies as
autowire candidate for this dependency. Dependency annotations: {})
Am I missing something ?
Bamboo version: 5.13.0
AMPS version: 6.2.6

The solution in the end was quite simple, no oficial docs discuss the solution though. Hope this helps you a bit.
Finally thanks to this post I made it work: https://answers.atlassian.com/questions/33141765/testcollationservice-not-injected-into-tasktype-constructor-on-sdk-bamboo
import com.atlassian.bamboo.process.ProcessService;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
#Scanned
public class CheckTask implements TaskType {
#ComponentImport
private final ProcessService processService;
public CheckTask(#NotNull final ProcessService processService) {
this.processService = processService;
}
The rest of the project was basicaly default, as generated by atlas-create-bamboo-plugin.

Try to add in your atlassian-plugin.xml next line
<component-import key="processService"
interface="com.atlassian.bamboo.process.ProcessService"/>
That should help you

Related

Spring test integration not run how I deal with this error?

I have a test class that make me crazy with this error :
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name .imprimirRelatorio: Unsatisfied
dependency expressed through field repository; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type v1.teste.Service available: expected at least
1 bean which qualifies as autowire candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
After search in the web by a resolution, I couldn't run the test. The test class was like that:
#RunWith(SpringJUnit4ClassRunner.class)
public class imprimirRelatorio {
#Autowired
PautaService pautaRepository;
#Test
public void imprimirCabecalho(){
PautaReportBuilder pautaReportBuilder = new PautaReportBuilder();
//Reuniao reuniao = reuniaoService.findOne(UUID.fromString("4c7d3fc8-f78d-4ed3-a1ad-83c5da822ea7"));
try {
pautaReportBuilder.cabecalhoRelatorios(reuniao);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/*
Update
*/
So I forgot to say that I already tried the annotation #service in the pautaService interface. And the same on the test class I've tried to use the #SpringBootTest annotation. So the error messages was different.
The App.class
#SpringBootApplication
#ComponentScan({"br.com.empresa123.sistema123.*"})
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
The interface PautaService:
#Service
public interface PautaService {
Page<Pauta> findAll(Pageable page);
Pauta findOne(UUID uuid);
Pauta save(Pauta pauta);
}
This is the first approach of my test , the nested exception were :
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'subjectController': Unsatisfied
dependency expressed through field 'subjectService'; nested exception
is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'subjectService': Unsatisfied dependency
expressed through field 'domainSvc'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'dominio': Unsatisfied dependency
expressed through field 'domainRepo'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'domainReposImpl': Unsatisfied
dependency expressed through field 'sqlGenericPager'; 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:
{#org.springframework.beans.factory.annotation.Autowired(required=true),
#org.springframework.beans.factory.annotation.Qualifier(value=sql-Generic-Pager)}
For integration test, add #SpringBootTest over your class.
In case where your file is not in the same packaging of your app, #ComponentScan() your app packaging.
I think you have forgot to annotate the service with the #Service annotation. If so, please tag the Service CLASS with #Service.
I solve this problem when I search by a annotation lost in my code that I never used it :
public abstract class BaseCustomRepository {
// the class code
#Autowired
#Qualifier("sql-Generic-Pager")
protected String sqlGenericPager;
}
I never used a Custom repository implementation so I will rethink what I will do with this class. Thanks a lot of the coders that lost their time to help me and the stack over flow to share my concerns.
I was laughing about this question, all the trouble was caused by me, like you do a git blame command in a class and see your user that made the wrong code.

How to autowire service with implicit ExecutionContext argument

In a Scala project I work on we have an API that early on imports a global execution context like this
import scala.concurrent.ExecutionContext.Implicits.global
I later create a UserStorageService that requires said execution context. I'm attempting to introduce Spring Annotations into my application, but I'm getting stuck on how to handle the execution context. How do I autowire an implicit variable? I've tried this
class UserStorageService(
#Qualifier("userdb") val databaseConnector: DatabaseConnector
)(implicit executionContext: ExecutionContext) extends UserStorageTable {
that I'm trying to get as
private val userStorageService = appContext.getBean(classOf[UserStorageService])
resulting in
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'scala.concurrent.ExecutionContext' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
import this:
import scala.concurrent.ExecutionContext;
import scala.concurrent.ExecutionContext$;
and in your configuration(#Configuration) add this
#Bean
public ExecutionContext getExecutionContext() {
return ExecutionContext$.MODULE$.global();
}
And ExecutionContext bean will be added to the Spring context.

Spring autowiring list of components

I have an interface:
public interface IExternalCommandExecutor {
String[] getCommandNames();
void process(String command) throws FailedOperationException;
}
And two implementations(second is similar):
#Service
public class ExecutorImpl implements IExternalCommandExecutor {
//some code...
}
And service, in which I'm using #Autowired annotation on the list of IExternalCommandExecutor:
#Service
public class ExternalCommandsService {
#Autowired
List<IExternalCommandExecutor> commandExecutors;
// other code
}
When I'm trying to run this, I'm getting this error:
NoSuchBeanDefinitionException: No qualifying bean of type
'java.util.List<com.mpcomp.entity.system.IExternalCommandExecutor>' available:
expected at least 1 bean which qualifies as autowire candidate
Which is weird behavior for Spring. As it was mentioned in this article and in the javadoc, Spring should try to autowire all elements of IExternalCommandExecutor instead of trying to find bean of type java.util.List.
If I would change List<IExternalCommandExecutor> to IExternalCommandExecutor[], error is quite similar:
NoSuchBeanDefinitionException: No qualifying bean of type
'com.mpcomp.entity.system.IExternalCommandExecutor[]' available:
expected at least 1 bean which qualifies as autowire candidate
I'm using Spring of version 4.3.8 RELEASE. Any help will be appreciated!
Edit
Interface and service are in the core module, and implementations of interface are in 'children' module which is depends on core module. When I moved service to this 'children' module, everything works fine!

SpringBootTest for DAO JUnit test

I am attempting the following unit test of a DAO.
I am unable to get the DataSource recognized.
Can I get a tip on how to resolve this?
Details below
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
public class EntityDaoTest {
#Autowired
protected EntityDao entityDao;
#Before
public void setup()
{
}
#Test
public void test() throws InternalServerException
{
List<Entity> entities = entityDao.list();
assert(entities.size()==0);
}
}
The relevant aspects of the DAO class are as follows
#Repository
public class EntityDao extends GenericDao<Entity>{
public EntityDao(DataSource dataSource) {/.../}
}
My src/test/resources/application.properties file is as follows
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=dbuser
spring.datasource.password=dbpass
Trace from running as JUnit test in Eclipse
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityController': Unsatisfied dependency expressed through field 'entityDao': Error creating bean with name 'entityDao' defined in file .../target/classes/hitstpa/dao/EntityDao.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {};
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityDao' defined in file [/home/fmason/workspace/hitstpa/target/classes/hitstpa/dao/EntityDao.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. ...`
Application structure
-src
--main
---java
----Application.java
----com
----hitstpa
-----controller
-----dao
------EntityDao.java
-----model
---resources
----application.properties
--test
---java
----hitstpa
-----dao
------EntityDaoTestDOTjava
---resources
----applicationDOTproperties
First of all for integration tests you need an integration Db with some fixed data.
Now you need to create a configuration class which will create the
integration test specific dependencies(I have named it as DbConfig
.java)
Next is add #ContextConfiguration annotation to the integration test
class and provide DbConfig.java, so that when test runs it will
create the datasource dependency and inject it to the container
Sample Code
#Configuration
public class DbConfig {
#Bean
public DataSource dataSource() {
//Create the DataSource with integration-DB properties
return dataSource;
}
}
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
#ContextConfiguration(classes=DbConfig.class)
public class EntityDaoTest {
}
For the record, I believe this is not a good unit test. This test requires that a mysql databases exists on localhost.
Anyhow, the errors suggest that the Spring Context isn't loaded correctly. When using SpringBootTest, Spring looks for the configuration using the test's package as root. So, if it's lower than your Configuration classes, it won't them.
Take a look at Spring's documentation:
The search algorithm works up from the package that contains the test
until it finds a #SpringBootApplication or #SpringBootConfiguration
annotated class. As long as you’ve structure your code in a sensible
way your main configuration is usually found.
Solution:
You can either move your tests to the same level as your SpringBoot Main class or change it to: #SpringBootTest(classes = YourSpringBootMainClass.class)

Unable to inject array list as dependency

I am creating a bean with annotations.
#Component
public class MyClass
{
#Autowired
private ArrayList<String> myFriends= new ArrayList<String>();
//Getters and setters
}
I am getting the following exception
Could not autowire field: private java.util.ArrayList com.mypackage.MyClass.myFriends; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.util.ArrayList] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I also tried with this
#Resource
private ArrayList<String> myFriends= new ArrayList<String>();
I am getting the following exception
No matching bean of type [java.util.ArrayList] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
Please let me know how to fix this.
In the XML file you would need to define a list.
In the XML file include the util namespace and add the following bean definition.
<util:list id="myFriends">
<value>string1</value>
<value>string2</value>
<value>string3</value>
</util:list>
You need to change the type of the variable to List<String> instead of ArrayList<String>. That will make it easier to inject and is also a better coding practice to follow. You need to add a Qualifier annotation to specify the id of the bean that needs to be injected, qualifier might not be required if you have only one such list.
#Component
public class MyClass {
#Autowired
#Qualifier("myFriends")
private List<String> myFriends= new ArrayList<String>();
//Getters and setters
}
Link to spring reference documentation for util:list
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util-list

Categories