Spring zu JSF hinzufügen

Hi,

ich muss Spring als IoC für meine JSF WebApplikation verwenden und versucht Spring über xml zu konfigurieren.

Ich habe ein IQuestionDao interface und die dazugehörige Implementierung QuestionDao. Das IQuestionDao und QuestionDao befinden sich in einem anderen Projekt wie die WebApplikation.

Ich orientiere mich an https://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/ 3.1. XML Schema Example.

Ich habe wie beschrieben eine applicationContext mit folgendem Wert hinterlegt:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.1.RELEASE.xsd">

    <bean id="QuestionDao" class="code.elephant.dao.QuestionDao"></bean>
    
</beans>

In der faces-config habe ich dann zum QuestionBean folgenden Eintrag:

<managed-bean>
	<managed-bean-name>Question</managed-bean-name>
	<managed-bean-class>QuestionBean</managed-bean-class>
	<managed-bean-scope>session</managed-bean-scope>
	<managed-property>
		<property-name>QuestionDao</property-name>
		<value>#{QuestionDao}</value>
	</managed-property>
</managed-bean>

Der Bean sieht so aus:

@ManagedBean(name = "QuestionBean")

@ViewScoped
public class QuestionBean implements Serializable {

IQuestionService _IQuestionService;
/**
 * Creates a new instance of QuestionBean
 */
public QuestionBean() {
    
}

@PostConstruct
public void init() {

    Object bla = getQuestionDao();
    //bla ist NULL
    System.out.print("yoyoyo");
}

private IQuestionDao QuestionDao;

/**
 * Get the value of QuestionDao
 *
 * @return the value of QuestionDao
 */
public IQuestionDao getQuestionDao() {
    return QuestionDao;
} ...

Jedoch ist QuestionDao immer NULL. Was habe ich falsch gemacht? Wie macht man dann in weitere Folge eine Konstruktor Injection? Eigentlich möchte ich ja das Interface IQuestionService in den Bean injezierbar machen.

Das Komplette Projekt sieht man hier https://github.com/mfe-/SEPM

Ich weiß nur, dass Spring anfangs sehr schwierig in Verbindung mit JSF war. Zudem sind spätestens mit JSF 2.3 die alten ManagedBean-Annotation deprecated.

Wenn Du Richtung SpringBoot gehen kannst, hilft Dir sehr stark das Framework https://github.com/joinfaces/joinfaces.

Hilft Dir das weiter?

Danke Sym für den Tipp. An der Spring Vorgabe komme ich aber leider nicht drum herum. Die Annotation brauch ich nicht, wenn ich es über die XML Konfiguration mache (wenn ich es richtig verstanden habe).

Ich habe noch zwei Fehler gefunden, die ich mit dem letzten Commit ausgebessert habe. Nun startet der GF ohne Fehlermeldung. Das IQuestionDao bleibt aber weiterhin leer.

Ich glaube den managed-bean im faces-config.xml

<!--	<managed-bean>
		<managed-bean-name>Question</managed-bean-name>
		<managed-bean-class>QuestionBean</managed-bean-class>
		<managed-bean-scope>View</managed-bean-scope>
		<managed-property>
			<property-name>QuestionDao</property-name>
			<value>#{questionDao}</value>
		</managed-property>
	</managed-bean>-->

brauch ich gar nicht, da ich in der BeanKlasse die Annotation @ManagedBean(name = “QuestionBean”) und @ViewScoped verwende.

Dafür habe ich im ApplicationContext jetzt folgendes stehen:

    <bean id="QuestionDao" class="code.elephant.dao.QuestionDao"></bean>

<bean id="QuestionBean" class="QuestionBean">
    <property name="QuestionDao">
        <idref bean="QuestionDao" />
    </property>
</bean>

Ich möchte, dass er in den QuestionBean bei der Property vom Interface IQuestionDao eine QuestionDao instanz injeziert.

Jedoch sagt GF:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘QuestionBean’ defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type ‘java.lang.String’ to required type ‘code.elephant.contract.dao.IQuestionDao’ for property ‘QuestionDao’; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [code.elephant.contract.dao.IQuestionDao] for property ‘QuestionDao’: no matching editors or conversion strategy found

Mit der Property injection hats nicht funktioniert, mit der Konstruktor injection schon.

Habe das ganze dann gleich auf den QuestionService ausgeweitet:

<bean id="QuestionDao" class="code.elephant.dao.QuestionDao"></bean>

<bean id="QuestionService" class="code.elephant.service.QuestionService">
    <constructor-arg ref="QuestionDao"/>
</bean>

<bean id="QuestionBean" class="QuestionBean">
    <constructor-arg ref="QuestionService"/>
</bean>