I have an NotificationsAtmosphereHandler as an Atmosphere framework handler class in my Grails application. I would like to use springSecurityService service inside of it. How I can inject service object into my handler class?
def springSecurityService
The usual service injection does not work.
resources.xml:
<bean id="notificationsAtmosphereHandler" class="your.package.NotificationsAtmosphereHandler"/>
in class:
class NotificationsAtmosphereHandler {
.....
#Autowired
SpringSecurityService springSecurityService
.....
}
or rather do it the Groovy way in your resources.groovy:
import package.NotificationsAtmosphereHandler
//...
notificationsAtmosphereHandler(NotificationsAtmosphereHandler) { bean ->
bean.autowire = 'byName'
}
this way, you don't need the #Autowired notation also.
Related
I am trying to injecting my own service elasticsearchService from the following domain class:
class DocumentESO extends ElasticsearchObject{
ElasticsearchService elasticsearchService
def afterInsert() {
elasticsearchService.save(this) // <-- Cannot invoke method save() on null object
}
}
However, it tells me that it Cannot invoke method save() on null object. Here is my service:
#Transactional
class ElasticsearchService {
#Transactional
def save(ElasticsearchObject esObject) {...}
}
Did I misspell something? If I would use ElasticsearchService elasticsearchService = new ElasticsearchService() then it would work, but I don't have the transactional support anymore.
In this answer, robert mentions it needs to initialized, while using meta programming save() for example. Does it mean that I cannot go with dependency injection in this case?
Thus it would be:
def afterInsert() {
ElasticsearchService elasticsearchService = new ElasticsearchService()
elasticsearchService.save(this)
}
??
Service injection in GORM entities is disabled by default since Grails 3.2.8.
You can turn on autowiring in this one particular domain class by adding to DocumentESO:
static mapping = {
autowire true
}
however it's not recommended: https://grails.org/blog/2017-05-09.html
I have a class like this,
#Component
class GenericRepositoryModel<T>
{
//.,..
}
In my code, I have to use for a specific type,
#Controller
class WebController
{
#Autowired
private GenericRepositoryModel<Type1> customerRepository;
}
But when I try to do the above, I am getting the below exception.
Field customerRepository in com.sbexample.springbootdemo.WebController
required a bean of type
'com.sbexample.springbootdemo.GenericRepositoryModel' that could not
be found. Consider defining a bean of type
'com.sbexample.springbootdemo.GenericRepositoryModel' in your
configuration.
What's the right way of doing it?
BTW, I am using Spring 4.3.16 via Spring boot 1.5.10.
In my current setup i want to unit test a Grails service that has an #autowired dependency and inject a mock for the dependency.
class AcmeService {
#Autowired
FooService fooService // not a Grails service!
}
The FooService is not a Grails service but it is a dynamic implementation from a FeignClient. I am looking for a way to inject a Mock for the FooService service in a UnitTest. What would be the best solution to do this?
I tried setting the dependency in the setup, but then i get a 'Unsatisfied dependency expressed through field fooService'
class AcmeService extends Specification {
FooService mockedFooService = Mock(FooService)
def setup() {
service.fooService = mockedFooService
}
}
you can add the following to your unit test:
def doWithSpring = {
fooService( InstanceFactoryBean, Mock(FooService) )
}
I am trying to write an integration test for a service that has a spring bean injected into it. The spring bean is defined in resources.groovy. The bean that my service is using does not appear to be getting injected in my integration test, but it gets injected fine when i run grails run-app.
Here is a minimal failing example:
grails-app/conf/spring/resources.groovy
beans = {
myBean(Object){}
}
grails-app/services/MyService.groovy
class MyService {
def myBean
def serviceMethod(){
myBean.class.simpleName
}
}
grails-app/src/integration-test/groovy/MyServiceSpec.groovy
#Integration
class MyServiceSpec extends Specification {
def myService
when:
def myBean = myService.myBean
then:
myBean != null
}
Grails version info:
$ grails -v
| Grails Version: 3.1.9
| Groovy Version: 2.4.7
| JVM Version: 1.8.0_92
Update:
Spring seems to inject other services just fine. If I declare another service inside of MyService, it gets injected.
class MyService {
def myBean
def myOtherService
def serviceMethod(){
myBean.class.simpleName
}
}
I may be late to the party on this one... But, for future readers, try setting grails.gorm.autowire to true in your application.yml
There are a number of peculiar things about your example, e.g.
beans = {
myBean(Object){}
}
I can't imagine why you'd want to create a bean of type Object, but maybe this is a simplification you made for the purposes of the example and the real bean has a different type.
#Integration
class MyServiceSpec extends Specification {
def myService
when:
def myBean = myService.myBean
then:
myBean != null
}
This test boils down to testing whether dependency injection works, which means your testing Spring/Grails rather than your own code. This is not something you should be testing IMO.
Anyhow to fix your problem I think you just need to add an #Autowired annotation
import org.springframework.beans.factory.annotation.*
#Integration
class MyServiceSpec extends Specification {
#Autowired
def myService
when:
def myBean = myService.myBean
then:
myBean != null
}
Currently I am trying to inject a stateless EJB into a CDI managed controller on Jboss 6 AS Final. The controller is managed in the context an accessible from the JSF pages. If I inject the stateless bean with #EJB it is working. If I inject the stateless EJB with #Inject I get the following Exception:
My controller:
#Named("TestController")
public class TestController {
#Inject
private TestManagerLocal myTestManager;
...
}
}
My stateless bean:
#SuppressWarnings("unchecked")
#Stateless
public class TestManagerBean implements TestManagerLocal {
#PersistenceContext
private EntityManager em;
...
}
The Interface of the Bean is annotated with #Local.
If I try to call myTestManager I get the following exception:
WELD-000079 Could not find the EJB in JNDI: class
de.crud.org$jboss$weld$bean-jboss$classloader:id="vfs:$$$usr$local$jboss$server$default$deploy$test$ear"-SessionBean-TestManagerBean_$$_WeldProxy
THX a lot.
For those not having the luxury to change an ear to a war, I've found the following workaround:
Create an EJB in the war
Inject that EJB with the EJBs from the EJB module
Add CDI producer methods
Qualify #Inject with the qualifier for those producer methods:
Code:
// This bean is defined in the WEB module
#Stateless
public class EJBFactory {
#EJB
protected UserDAO userDAO;
// ~X other EJBs injected here
#Produces #EJBBean
public UserDAO getUserDAO() {
return userDAO;
}
// ~X other producer methods here
}
Now EJBs from anywhere in the EAR can be injected with:
// This bean is also defined in the web module
#RequestScoped
public class MyBean {
#Inject #EJBBean
private UserDAO userDAO; // injection works
public void test() {
userDao.getByID(...); // works
}
}
EJBBean is a simple standard qualifier annotation. For completeness, here it is:
#Qualifier
#Retention(RUNTIME)
#Target({TYPE, METHOD, FIELD, PARAMETER})
public #interface EJBBean {
}
The problem was, that I built and deployed my application as an ear. Weld is working when I deploy my application as an war including all EJBs.
Currently there are various problems arising from the fact that WARs in EAR-Deployments don't share the same classloader. See https://issues.jboss.org/browse/JBAS-8683 for the ongoing discussion in the JBoss-AS JIRA (and vote it up :-) )
UPDATE I found this information on how to disable separate classloaders, option 1 worked for me, but be extremely careful with this. The separation of classloaders hasn't been introduced for no reason, so apparently there are new problems on the road ahead...