I've been facing the problem of Circular dependency in spring.
Public class UserServiceImpl implements UserService{
#Autowired
private RoleService roleService;
}
Public class RoleServiceImpl implements RoleService{
#Autowired
private UserService userService;
}
Is there any solution to deal with this problem, But I still want to use #Autowired. Other solutions might be to wire them manually. Or by using bean awares or by using bean post processor.
The working solution was to add default-lazy-init="true" to the
application config xml file
Details are here.
Related
We are planning to change Managed Beans to CDI beans. We used below code to invoke Service class in managed Bean.
#ManagedProperty("#{userService}")
private UserService userService; and setter method
For CDI bean, I replaced #ManagedProperty with #inject as shown below, and it is throwing following exception.
#SessionScoped
#Named
public class LoginController implements Serializable {
#Inject
private UserService userService;
}
org.apache.webbeans.exception.WebBeansDeploymentException: Passivation capable beans must satisfy passivation capable dependencies.
UserService is a plain interface with unimplemented methods and UserServiceImpl implements UserService interface. Please see below:
public interface UserService {
public List<User> getUserList();
}
public class UserServiceImpl implements UserService {
private UserDao userDao;
public List<User> getUserList() {
return userDao.getUserList();
}
}
Please let me know me how to invoke service interface in CDI beans?
Reading BalusC's answer on Spring JSF integration: how to inject a Spring component/service in JSF managed bean? tells me it is supposed to be supported that your Spring bean userService should be injected into your CDI bean LoginController.
But your UserServiceImpl is not Serializable which in CDI context means it is not passivation capable.
This is also what your Exception tells.
So either make your LoginController #RequestScoped instead of #SessionScoped so itself and #Injected children do not require to be passivation capable (aka Serializable).
Or make your UserServiceImpl and DAO Implementations Serializable (which imho is somewhat odd?).
Spring #Autowired
I have a doubt on Spring #Autowired annotation.Please Help...
In Spring mvc ,when I tried #Autowired in this order
Controller--->Service--->Dao
ie,In Controller I autowired Service Class Object , In Service Class Autowire Dao Object.
This Injection chain works perfectly.
Similliarly In strutrs2+Spring ,I applied #Autowired Annotation in this way
Action--->Service-->Dao
This Injection chain also works fine.
If I call a funtion from outside this chain (eg:Custom Taglib class (from jsp)) to funtion in Service class Then in this Service class the Autowired dao object is null(ie,this call braks the chain).
My questions is
Is this #Autowired works in a Injection chain Only?
Beans that have #Autowired fields only have them set if they are sent through the Spring Bean Postprocessor -- that is, like you said, if you autowire them yourself. That is a big reason that constructor injection is much more preferred than field injection. Instead of doing
#Service
public class MyService {
#Autowired
private MyDao dao;
...
}
you should do
#Service
public class MyService {
private final MyDao dao;
#Autowired
public MyService(MyDao dao) {
this.dao = dao;
}
}
That way, when you're in a situation where you can't rely on a service to be post-processed (as in your case of using the jsp tag library), you can simply instantiate a new instance with a MyDao object and be on your merry way.
I am using Spring in my Java Application, all the #Autowired annotations working until now.
Simplified example would be:
#Component
public class MyBean implements MyInterface {
...
}
#Component
public class MyOtherBean {
#Autowired
private MyBean myBean;
...
}
Once I try to start the Application, I get:
java.lang.IllegalArgumentException: Can not set MyBean field MyOtherBean.myBean to $ProxyXX
The interface contains just two public simple methods and the class implements them.
Both classes are public and have public default constructor. (I even tried to instantiate them in tests.
Once I remove the implements section, everything works correctly.
What can be wrong with the implementation of the interface? What is $ProxyXX?
I suspect the issue is that Spring is injecting an AOP proxy which implements MyInterface - possibly for the purposes of transaction management or caching. Are any of MyBean's methods annotated #Transactional or annotated with any other annotation?
Ideally you'd probably want to reference MyBean by it's interface type - which should resolve the issue.
#Component
public class MyOtherBean {
#Autowired
private MyInterface myBean;
...
}
If you have more than one bean implementing MyInterface then you an always qualify your bean by name.
#Component
public class MyOtherBean {
#Autowired
#Qualifier("myBean")
private MyInterface myBean;
...
}
By default, Spring uses Java dynamic proxies to implement AOP when the bean implements an interface. The easiest and cleanest way to solve your problem is to make program on interfaces, and inject theinterface insted of the concrete class:
#Component
public class MyOtherBean {
#Autowired
private MyInterface myBean;
...
}
See http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/htmlsingle/#aop-proxying for how to force Spring to always use CGLib.
I'm using GWT with Spring. I encountered the problem of using an #Autowired bean in a RemoteServiceServlet. For some reason this doesn't work automatically and I need to use #Configurable to get this working. I followed this approach but I still get a NullPointerException for the #Autowired bean:
#Configurable
#Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class AServiceImpl extends RemoteServiceServlet implements AService {
#Autowired
private IABean aBean;
#Override
public void aMethodFromAService(Args arg[]) {
aBean.aMethodOfABean(); // this gives a NullPointerException
}
}
#Component
public class ABean implements IABean {
...
}
Any guidance in what is going on? Any extra information I need to provide?
http://mitosome.blogspot.be/2011/01/injecting-spring-beans-into-gwt.html
Thanks Alexander for putting me in the right direction
You found a workable solution, but just for the record and we have it working as follows:
public class MyServiceImpl extends RemoteServiceServlet
implements MyService, ServletContextAware
{
#Autowired private transient SomeService someService;
....
}
and
<context:annotation-config/>
<context:component-scan base-package="..."/>
The SomeService is a completely vanilla XML-defined bean. Perhaps that or ...implements ServletContextAware makes a difference.
Cheers,
I am using Spring for my DI.
Is there an equivalent of #ManagedProperty? I want to inject the value from one view scoped bean into another one on the next page.
e.g
#Component
#Scope("view")
public class Page1Bean(){
private String value;
}
#Component
#Scope("view")
public class Page2Bean(){
#ManagedProperty(value = #{page1Bean}") //doesnt work in Spring
private Page1Bean bean;
}
#Resource or #Autowired should work. #Resource is the Java EE implementation, #Autowired is the spring specific annotation. I can't find the reference now, but it seems like I read once to prefer #Resource over #Autowired.
here's a blog post I found that talks about #Inject vs. #Resource vs. #Autowired
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/#more-2350