Injetar Bean no Managed Bean (jsf)

1 resposta
rogeriosantos77

Srs, tenho uma classe anotada num projeto jsf como @Bean.
Como é possivel injeta-la no managed bean.

veja bem fiz o seguinte teste

Criei essa interface
package br.com.c5a.interfaces;

public interface IHello {
	
	public String getHello();
	
}

Depois essa implementação

package br.com.c5a.implementacao;

import net.sourceforge.sannotations.annotation.Bean;
import br.com.c5a.interfaces.IHello;

@Bean(name="helloImpl")
public class HelloImpl implements IHello {
	public String getHello() {
		return "Hello World";
	}
}

AI no managed bean eu anotei com @Bean e criei um set para ~fazer a injeção por set

package br.com.c5a.mb;

import java.util.ArrayList;
import java.util.Collection;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import br.com.c5a.interfaces.IHello;

import net.sourceforge.sannotations.annotation.Bean;

@Bean(name="indexMB")
public class IndexMB {

	private String usuario;
	private String senha;
	
	private IHello helloImpl;
	public void setHelloImpl(IHello helloImpl) {		
		this.helloImpl = helloImpl;
	}
	
	public String getUsuario() {
		return usuario;
	}
	public void setUsuario(String usuario) {
		this.usuario = helloImpl.getHello();
	}
	public String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}
}

Entretanto não ocorre a injeção pois o valor helloImpl é nulo.
Neste caso usando o @Bean não deveria ocorrer um autowire por nome ?

Este é o web.xml da aplicação

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
		 version="2.4">
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:applicationContext.xml</param-value>
  </context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>    
  </servlet>

  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  
  </servlet-mapping>

  <context-param>
  	<param-name>org.richfaces.SKIN</param-name>
  	<param-value>blueSky</param-value>
  </context-param>
  <filter>
  	<display-name>RichFaces Filter</display-name>
  	<filter-name>richfaces</filter-name>
  	<filter-class>org.ajax4jsf.Filter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>richfaces</filter-name>
  	<servlet-name>Faces Servlet</servlet-name>
  	<dispatcher>REQUEST</dispatcher>
  	<dispatcher>FORWARD</dispatcher>
  	<dispatcher>INCLUDE</dispatcher>
  </filter-mapping>
  
</web-app>

Estou usando os .jars
spring-annotation-base-1.1.1.GA.jar e spring-2.0.5.jar.
O caminho que estou seguindo está errado ? não é assim que faz esse tipo de injeção ?

1 Resposta

pauloalexandre3d

Não estou certo, mas acho q faltou algo no seu faces-config como por exemplo.
Não sei como funcionaria isso com annotations.

indexMB br.com.c5a.mb.IndexMB request helloImpl br.com.c5a.implementacao.HelloImpl #{helloImpl}

Espero ter ajudado.
Paulo

Criado 3 de março de 2009
Ultima resposta 2 de abr. de 2009
Respostas 1
Participantes 2