jsf-spring:没有调用managedbean postconstruct

时间:2013-01-03 18:05:30

标签: spring jsf primefaces postconstruct

我宁愿通过这样做来学习它可能是一个愚蠢的问题,但我找不到任何答案。

我有一个JSF应用程序,与简单的JDBC一起使用效果很好。

举一个“domain.xhtml”的例子,它有一个列出“DomainController”bean元素的表。这一切都很好,然后我们切换到JPA。该控制器必须使用服务,因此它被声明为@Component并包含(@Autowired)服务。除了我的所有JSF注入(@ManagedProperty)都不再注入,我的@PostConstruct不再被调用之外,它也运行良好。

有没有我遗漏的东西,或者这种方式出错了?

@ManagedBean
@Component
public class DomainController implements Serializable {
    private static Logger log = Logger.getLogger(DomainController.class);

    private static final long serialVersionUID = -2862060884914941992L;
    private List<Domain> allItems;
    private Domain[] selectedItem;
    private SelectItem[] yesNoNull;
    private DomainFilter filter = new DomainFilter();

    @Autowired
    private DomainService domainService;

    @Autowired
    private ValidationLookUpService validationLookUpService;

    @Autowired
    private ValidationService validationService;

    @ManagedProperty("#{workspace.on}")
    private boolean wsOn;

    //  @ManagedProperty("#{libraryVersionController.selectedItem.id}")
    //  private Integer selectedLibVersionID;

    @ManagedProperty("#{libraryVersionController.selectedItem}")
    private LibraryVersion selectedLibVersion;

    @ManagedProperty("#{obsoleteEntry}")
    private PObsoleteEntry pObsoleteEntry;

    @ManagedProperty("#{validationFailedItemsController}")
    private ValidationFailedItemsController validationFailedCont;

    private Domain itemEdited;

    private boolean persisted = false;

    public DomainController() {
        log.info("Creating metadata controller");

        allItems = new ArrayList<Domain>();

        // model for a yes/no/null column filtering
        yesNoNull = new SelectItem[4];
        yesNoNull[0] = new SelectItem("", "All");
        yesNoNull[1] = new SelectItem("true", "yes");
        yesNoNull[2] = new SelectItem("false", "no");
        yesNoNull[3] = new SelectItem("null", "not yet validated");


    }

    @PostConstruct
    public void test() 
    {
        log.info("!!!");
        log.info("WS is ... "+wsOn);
        // NOT CALLED ANYMORE
    }

...

我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>

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

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->

    <context-param>
        <param-name>contextConfigLocation</param-name> <!-- indique le fichier de configuration pour Spring -->
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param> <!-- to really skip comments in xhtml pages -->
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/app/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/spring/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>



    <listener> <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener> <!-- links JSF with spring -->
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <listener> <!-- parses JSF configuration -->
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

    <listener> <!-- vide le cache d’introspection Spring à l’arrêt du serveur. Ce listener n’est pas obligatoire mais conseillé -->
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>




    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

</web-app>

谢谢!

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我,但我不知道如何选择一个,因为Spring管理后端和JSF(+ primefaces)前端。

我认为控制器“可能”是两者之间的接口,这就是我天真地混合它们的原因。

在对你的评论进行一些测试后,我让我的控制器只使用JSF并使用@ManagedBean注入服务(我不知道使用@ManagedBean它可以注入一个@Service Spring管理的bean)以便回答我的问题:))

以下是更正后的代码。

还要感谢我向正确的方向重新定向!

控制器

/**
 * This is the controller for a Domain
 * 
 */
@ManagedBean
public class DomainController implements Serializable {
    private static Logger log = Logger.getLogger(DomainController.class);

    private static final long serialVersionUID = -2862060884914941992L;
    private List<Domain> allItems;
    private Domain[] selectedItem;
    private SelectItem[] yesNoNull;
    private DomainFilter filter = new DomainFilter();

    @ManagedProperty(value="#{domainService}")
    private DomainService domainService;

    @ManagedProperty("#{workspace.on}")
    private boolean wsOn;

    @ManagedProperty("#{libraryVersionController.selectedItem}")
    private LibraryVersion selectedLibVersion;

    private Domain itemEdited;

    private boolean persisted = false;

    /**
     * creates a list populated from the database
     */
    public DomainController() {
        log.info("Creating metadata controller");

        allItems = new ArrayList<Domain>();

        // model for a yes/no/null column filtering
        yesNoNull = new SelectItem[4];
        yesNoNull[0] = new SelectItem("", "All");
        yesNoNull[1] = new SelectItem("true", "yes");
        yesNoNull[2] = new SelectItem("false", "no");
        yesNoNull[3] = new SelectItem("null", "not yet validated");


    }

    @PostConstruct
    public void test()
    {
        Domain d = new Domain();
        d.setDataset("will it work ?"); // yes
        try {
            domainService.saveOrUpdate(d);
        } catch (DataModelConsistencyException e) {
            e.printStackTrace();
        }
    }

    // all functions 

    public DomainService getDomainService() {
        return domainService;
    }

    public void setDomainService(DomainService domainService) {
        this.domainService = domainService;
    }


}

服务

public interface DomainService extends IVersionedServiceBase<Domain> {
    public Domain saveOrUpdate(Domain d) throws DataModelConsistencyException;

    public Domain getRelatedVariables(Domain d, VersionedObjectFilter versionedObjectFilter) throws DataModelConsistencyException;

    StringAndError getVarNameAndKeyOrderForDomain(Domain d, VersionedObjectFilter versionedObjectFilter) throws DataModelConsistencyException;

}

服务的实施

@Service("domainService")
@Transactional(readOnly = true)
public class DomainServiceImpl extends VersionedServiceBase<Domain> implements DomainService {
    /**
     * Private logger for this class
     */
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(DomainServiceImpl.class.getName());

    @Autowired
    private DomainDao domainDao;

    @Autowired
    private VariableDao variableDao;

    @Autowired
    private DomainPurposeDao domainPurposeDao;

    @Autowired
    private DomainClassDao domainClassDao;

etc.