带有Spring Data的自动存储库

时间:2018-02-26 20:13:11

标签: spring jsf spring-boot

我在将spring与JSF集成时遇到了一些问题。

我知道Spring和JSF都有不同的容器。所以我不能混合他们的注释,因为注册的bean彼此不可见。

但是,我已经阅读了一篇文章:Spring DAO is not injected in JSF managed bean其中,人们说可以通过弹簧注释注释所有内容,使这些组件可以显示给JSF。

我真的对xml配置感到困惑,因为有时我读到我不需要它,但在大多数情况下,我看到人们通常会这样做。

我的问题是,如果我有这个源代码并且我想为JSF制作可见的spring bean,需要配置什么:

package pl.catarina.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import pl.catarina.domain.Patient;
import pl.catarina.repository.PatientRepository;
import javax.annotation.PostConstruct;
@Component
@Scope("request")
public class RegisterController {

    @Autowired
    private PatientRepository patientRepository;

    private Patient patient;


    @PostConstruct
    public void init(){
        patient = new Patient();
        patient.setName("jan");
        patient.setSurname("Way");
        patient.setPhoneNumber("21421131");
        patient.setEmail("asdpad@gmail.com");
        patient.setPassword("pdsad223Sdsd");
    }


    public void save(){
        patientRepository.save(patient);
    }


}

REPOSITORY

package pl.catarina.repository;

import org.springframework.stereotype.Repository;
import pl.catarina.domain.Patient;

@Repository
public interface PatientRepository extends UserBaseRepository<Patient> {
        Patient findByEmail(String email);
}

的web.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xmlns = "http://java.sun.com/xml/ns/javaee"
         xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id = "WebApp_ID" version="2.5">

    <welcome-file-list>
        <welcome-file>pages/home.xhtml</welcome-file>
    </welcome-file-list>

    <!--
       FacesServlet is main servlet responsible to handle all request.
       It acts as central controller.
       This servlet initializes the JSF components before the JSP is displayed.
    -->

    <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>/faces/*</url-pattern>
    </servlet-mapping>

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

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

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

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>

    <context-param>
        <param-name>com.ocpsoft.pretty.DEVELOPMENT</param-name>
        <param-value>false</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>bootstrap</param-value>
    </context-param>

</web-app>

0 个答案:

没有答案