Arquillian and CDI - java

I'm reading the Arquillian Reference Guide which is very well written, however in the chapter that talks about setting up dependency injection I can't find where you actually specify the beans/bindings.
Most of the Arquillian CDI code examples show the use of Java's #Inject annotation. So I'm just wondering, where I define these beans/DI mappings/bindings, and how do I configure Arquillian to use them?
In Spring DI, you specify a bean descriptor, like spring-config.xml. In Guice you implement a Module and define its configure(Binder) method. What does this look like in Arquillian-land when using javax.inject.Inject? Thanks in advance.

The short answer - there is no need to define bean mappings in CDI, because CDI works with annotations exclusively. You can add extra information in config-files, but this is usually not required.
The long answer is best taken from this excellent introduction into CDI.

I think you need to use "Alternatives" CDI mechanism
Alternatives are beans whose implementation is specific to a particular client module or deployment scenario.

Related

Callbacks in bean lifecycle

I was just going through spring documentation for Lifecycle callbacks for a bean. For initialization callback the documentation recommends using #PostConstruct annotation instead of using the InitializingBean interface implementation as it couples code with Spring. Could someone please explain what they mean when they say coupling code with spring. I will share the link to the section i was going through here.
The #PostContruct annotation is not Spring-specific, but from Jakarta Annotations. This way, the core part of your code is not tied to Spring specifically, but could also work in a non-Spring environment, including any Jakarta EE implementation (see "Certified referencing runtimes" in the article), for example, GlassFish or WildFly.
Here is the relevant Javadoc from the Jakarta documentation: PostConstruct.
InitializingBean is pure Spring and will only work in Spring. #PostConstruct is ordinary Java 1.8. Sadly, from 9 it's dependent on javax.annotation:javax.annotation-api:1.3.2

What entity deals with dependency injection? in CDI and EJB3

I have a basic know-how understanding about EJB3 and CDI but i want to dig deeper and understand how this 2 DI implementations actual work.
stackoverflow.com/questions/4684112/how-do-cdi-and-ejb-compare-interact
I have studied them but i fail to find what entity deals with the actual injection of the objects in the annotated variables and and by who is called.
Any help in this is welcomed. Thank you!
The "entity" that handles the injection is any injection framework, such as the reference implementation Weld for CDI or any particular EJB framework included in an application server.
The calling is done by the client code, which will reference the framework's proxies and enable the framework to perform actions.

Interceptor API : in a classic java project

I want to add a listener on java method and I saw the annotation #Interceptors and #AroundInvoke.
I want to use it in a classic java project but all tutorials show me Java EE projects.
Is it possible ?
Interceptor is a DI (Dependency injection) concept based on AOP (aspects oriented programming).
Since Java-ee 6 you have a lightweight DI container named CDI.
You can use CDI in a java-se context if you provide an implementation allowing a standalone deployment like jboss weld :
See Is it possible to use javax.interceptor in a Java SE environment?
Otherwise, you can use spring, which also provides a lightweight DI container with the same kind of features and may be better suited for a java-se context usage.
I don't think so, those are managed by the Java EE container.
An easy way is to create a proxy/wrapper over all instances of your class.
Or use Spring AOP (but you will have to configure Spring) or AspectJ.
Check this out: http://www.javacodegeeks.com/2012/06/simple-introduction-to-aop.html
Reading the doc here: https://docs.oracle.com/javaee/6/tutorial/doc/gkigq.html
Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events.
So I guess the answer to your question is no.

Dependency injection in OSGi vs testing

For a new project I'm looking at what OSGi has to offer in terms of dependency injection, and I kinda like iPOJO (pure annotation-based, not the xml backed).
However, from a testing perspective, Blueprint might be better, since then for different test cases (functionality testing) it might be enough to rewrite the blueprint configuration and there would be an other service injected right away.
What are your thoughts on this subject?
Can I abandon XML-based Blueprint (I loathe XML) in favor of iPOJO without sacrificing flexibility in terms of testing?
Yes. You can. I don't think you will sacrafice anything.
In fact, iPOJO is more powerful then blueprint, it supports things like "Field injection", "Service lifecycle control", and "Configuration admin" - which Blueprint does not (reference).
Blueprint, however, is part of the OSGi Enterprise Specification, if that matters.
I have not worked with Blueprint, but just looking at the specification for it - unless you are a java beans guy, I would stay away. Also, I prefer iPOJO over DS, as it seems to be a lot smarter in many cases, and just does the right thing.
Regarding iPOJO unit testing, you can use constructor (or constructor injection) to inject mock services and effectively test the behavior of your component:
So two choices:
either you have an additional constructor to inject the dependencies
you need using mocks
or your component is using constructor
injection, and then you just call the constructor with mock objects
If you trying to do integration testing, you can just use pax exam and deploy your bundle (in additional to iPOJO).

Spring Meta-Annotations

Does anybody know what Spring JAR (and where to find it!) contains the functionality for Spring's so-called "meta-annotations". As this article shows, these cool "new" (well, sorta) constructs allow code like this:
#Service
#Scope("request")
#Transactional(rollbackFor=Exception.class)
#Retention(RetentionPolicy.RUNTIME)
public #interface MyService {
}
#MyService
public class RewardsService {
…
}
Basically, I have a Swing app that does not currently use Spring. I have a sudden requirement change that would be made supremely-easier if I could use these annotations.
So I'm just looking for the smallest, minimally-invasive Spring JAR where this functionality can be found. If absolute-need-be, I can use the entire Spring JAR, but it would be a very heavy-handed solution. It would be nice if Spring released a smaller JAR with smaller-scope functionality.
I'm browswing Maven Repo and see spring-core which contains a package called:
org.springframework.core.annotation
But upon inspection of the API docs it doesn't seem to be what I need...
Ancillary question: is it even possible to use these meta-annotations as standalone constructs, or do I have to use them along with, say, Spring DI or AOP or other major parts of the Spring framework?
Thanks in advance!
The annotations you want will be with the jar it is related to. For instance, the annotation org.springframework.transaction.annotation.Transactional will be in the spring-transactions artifact.
Meta-annotations are not really different from regular annotations other than the fact that Spring now detects Annotations on annotations whereas it didn't before. Whether annotations "work" or not depends on what is looking for them (in this case something in the Spring context).
For further reading see Annotation-based container configuration in the Spring framework reference.
There is project on github which is trying to provide meta-annotation functionality.
https://github.com/dblevins/metatypes/

Categories