logo

JSF & PrimeFaces & Spring tutorial 6: ManagedBean vs Component vs Named

How to use @ManagedBean, @Component and @Named annotations with JSF

pom.xml:
<dependency>
	<groupId>javax.inject</groupId>
	<artifactId>javax.inject</artifactId>
	<version>1</version>
</dependency>
HelloController:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.jiripinkas.sitemonitoring.service.HelloSpringService;

// JSF annotation
@ManagedBean
// Request scope - needed when using Spring or standard annotations (unless you want singleton)
// @Scope("request")
// Spring-specific annotation
// @Component
// Standard annotation
// @Named
public class HelloController {
	
	@ManagedProperty("#{helloSpringService}")
	// Spring-specific annotation
	// @Autowired
	// Standard annotation
	// @Inject
	private HelloSpringService helloSpringService;

	public String showHello() {
		return helloSpringService.sayHello();
	}
	
	public void setHelloSpringService(HelloSpringService helloSpringService) {
		this.helloSpringService = helloSpringService;
	}
}

comments powered by Disqus

rss

Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:
github

Search