Inject ArrayList using RequiredArgsConstructor - dependency-injection

I tried to inject List into constructor through lombok RequiredArgsConstructor
#Slf4j
#Component
#RequiredArgsConstructor (onConstructor = #_(#Inject))
public class ClassA {
#NonNull private List<GoodSkill> skills;
......
}
However then have errors:
[tomcat:launchProperties]
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'ClassA' defined in URL
[jar:file:/XXXXX/ClassA.class]: Unsatisfied dependency expressed
through constructor parameter 0; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type java.util.List<\GoodSkill>: expected at least 1
bean which qualifies as autowire candidate. Dependency annotations: {}
It seems there is NO bean of for "List<\GoodSkill>"? I guess there's some special rule when injecting List or other Collections? And as for GoodSkill class, I guess I also should add annotation like #Component?
========
edit:
More weird thing is, I re-build package and now it's no longer complaining find no bean for List<\GoodSkill>, but no bean for GoodSkill. I'm very confused.

Injecting is possible only for managed beans. So, for a list you should have somewhere something like:
#Component
public class GoodSkillList extends ArrayList<GoodSkill>{}
for container to find.
Perhaps you have? If you have many of such beans container/Spring might not be able to decide which to use. In that case you can delimit the possible alternatives many ways for example you could set ClassA to accept only:
#NonNull private GoodSkillList skills;
or alternatively you can research howto inject/autowire by name.

Related

Spring boot #ConditionalOnBean does not detect bean missing, throws exception "No qualifying bean of type"

Have this #Configuration class that requires a bean implementing JmsProperties which is declared in the #ConditionalOnBean
#Configuration
#ConditionalOnBean(JmsProperties.class)
public class JmsConfiguration {
#Inject
private JmsProperties properties;
...
}
Getting exception:
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.....JmsProperties] found for dependency:
Isn't the #ConditionalOnBean supposed to detect this missing bean and not try to inject the dependency in the first place?
Thanks in advance for any pointers
You've cross-posted on the Spring Boot tracker and the developers responded:
#ConditionalOnBean is evaluated after all configuration classes have been processed, i.e you can't use it to make a whole configuration class conditional on the presence of another bean. You can, however, use it where you have to make all of the configuration's beans conditional on the presence of another bean.
You must use #ComponentScan(basePackages="".
Without using it your app wont discover & register/instantiate all defined beans and hence you would end up with this type of exception i.e. No bean def found.

Grails-The different dependence Injection ways in grails

What's the difference of different way to inject dependence in grails:
ABCService abcService
def abcService
#Autowired
ABCService ABCService
Expected type is specified, if service with name abcService (or other Spring bean with such name) will have different class, then you'll get ClassCastException here
Just any bean with name abcService
Spring annotation, it's optional. But if you've marked a field but Grails/Spring cannot find such bean it will throw NoSuchBeanDefinitionException (previous two will get null if it doesn't exists) #Autowired could be combined with def type also
Basically Grails services are standard Spring beans, Grails just follows convention over configuration that for every class in services dir it will create a bean with name abcService that could be autowired into other beans. All other job is done by Spring. See also docs for Spring and Grails

Inject singleton bean into session bean via remote interface, object always "null"

I need to inject a singleton bean into the session bean. Below are the corresponding classes. The problem is that the injected object is always null. I tried all of the JNDI lookup strings which my JBoss 7.0.1 server showed me during startup (i.e. JNDI bindings for session bean named GlobalBean in deployment unit subdeployment .. of deployment .. are as follows: ..). I also tried commenting out the #EJB annotation in GlobalBean.java and also tried to use the "ejb/GlobalBean" during injection. However, no luck. What could be the reason? Thx.
GlobalBean.java:
#Startup
#Singleton
#Remote(GlobalBeanRemote.class)
#EJB(name="ejb/GlobalBean", beanName="GlobalBean", beanInterface=GlobalBeanRemote.class)
#ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class GlobalBean implements GlobalBeanRemote
{
// CODE
}
SessionBean.java:
#Stateful
public class SessionBean extends ParentBean
{
#EJB(name="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
private GlobalBeanRemote globalBeanAPI3;
// CODE
}
In your SessionBean class try changing name attribute of #EJB to mappedName.
#EJB(mappedName="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
This will, of course, only work if your two beans are in the same module.
Update
Given that your beans are in separate modules, try using the java:app namespace:
#EJB(mappedName="java:app ...")
The java:app namespace is used to look up local enterprise beans packaged within the same application. That is, the enterprise bean is packaged within an EAR file containing multiple Java EE modules. JNDI addresses using the java:app namespace are of the following form:
java:app[/module name]/enterprise bean name[/interface name]
Also try removing GlobalBean's #EJB annotation. #EJB is used to define a dependency.

Cannot access inner class in bean

I'm using JSF 2.0. I have a managed bean which I can access through my xhtml page. Inside the bean I declared an inner class. I can access ArrayList<String> of managed bean but not ArrayList<InnerClass> and I get the error that the InnerClass does not have a readable property. Anyone know what's wrong?
That can happen if the inner class is not public. It will then be invisible to other classes outside the package (like as JSF/EL itself!). Make sure that the inner class is public whenever you need to access it by JSF/EL.
public class Bean {
public class InnerClass {
// ...
}
}
Otherwise it will be interpreted as String and you'll get confusing exceptions like
javax.el.ELException: /test.xhtml: Property 'someProperty' not readable on type java.lang.String
when you want to access #{innerClass.someProperty}.

Is there any Spring #Required annotation equivalent in EJB 3.0?

Is there any equivalent annotation in EJB for #Required (Spring)? I do dependency injection using setters and I want to be sure that resource was injected (almost no probability of NullPointerException ;)). In Spring it is easy:
#Required
public void setProperty(Property p) {
this.property = p;
}
Is there any way to do such a validation in EJB? (Maybe some other solution than annotatations). Thanks
In ejb injection is done via #EJB and #Resource (as stated above).
If the bean for the given (or auto-generated) name doesn't exist you get an error from the container (in many cases this happens at deployment time).
The only way to (maybe) get a nullpointer exception inside a ejb bean is if you try to access an injected object in the default constructor. By spec injection happens after the constructor and before the #PostConstruct lifecycle is called.

Resources