Using JSF only
When using JSF, without Spring, mostly dependency-injection will be performed by JSF itself. This one is declaring a managed bean in session scope.
@ManagedBean
@SessionScope
public class LoginBean {
When you want inject other bean in this bean. You will use @ManagedProperty annotation given by JSF.
@ManagedBean
@SessionScope
public class LoginBean {
@ManagedProperty(value="#{otherBean}")
OtherBean otherBean;
//make sure create the setter for injection
public void setOtherBean(OtherBean otherBean){
this.otherBean = otherBean;
}
}@ManagedBean
@SessionScope
public class OtherBean {
}
Something when I'm using JSF + Spring, i added configuration in faces-config.xml
<faces-config>
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config> adding listener to web.xml inside element <web-app>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
In the first time I didn't notice when creating managed bean, I'm using annotation from JSF (@ManagedBean, and for scope @ApplicationScoped, @SessionScoped, @ViewScoped, @RequestScoped, etc. )<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
@ManagedBean //spring don't read this
@SessionScope //spring don't read this
@Controller //spring check this
public class LoginBean {
It won't work, and spring don't read two annotation introduced in JSF 2.0.
Spring will create the bean as singletion (similar to application scoped in JSF). Only one instance in one application, and created when the application start.
I should use this one instead.
@Scope("session")
//you can use singleton (same as application), request, or //session mostly
@Component("loginBean")
public class LoginBean {
}
See scope type in Spring for more details, click here
The injection is performed by Spring, not JSF.
But there's no built on view scope in Spring, but Spring give you capability to define your own scope. To know how to implement view scope, click here
Hope this blog help other peoples. :)
2 comments:
thanks
buena informacion
Post a Comment