jsf-spring:不调用managedbean postconstruct [英] jsf-spring : managedbean postconstruct not called

查看:84
本文介绍了jsf-spring:不调用managedbean postconstruct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在学习,所以这可能是一个愚蠢的问题,但我找不到任何答案.

I'm rather learning by doing so it might be a stupid question but I couldn't find any answer.

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

I have an JSF application which worked well as used with simple JDBC.

以"domain.xhtml"为例,其中有一个表列出了"DomainController" bean中的元素.一切都很好,然后我们改用了JPA.该控制器必须使用服务,因此将其声明为@Component并从那里包含(@Autowired)服务.除了我的所有JSF注入(@ManagedProperty)不再注入,也不再调用我的@PostConstruct 之外,它也能很好地工作.

Take an example of the "domain.xhtml", which had a table listing elements from a "DomainController" bean. It was all working great then we switched to JPA. That controller has to use services so it's declared as @Component and includes (@Autowired) services from there. It also works well EXCEPT that all my JSF injections (@ManagedProperty) are not injected anymore, my @PostConstruct is not called anymore either.

有什么我想念的东西吗,或者这种方式做错了吗?

Is there something I missed, or is wrong with that manner to proceed ?

@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>

谢谢!

推荐答案

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

Correct me if I'm wrong but I don't see how I could just choose one, as Spring manages the back-end and JSF(+primefaces) the front-end.

我认为控制器可以"是两者之间的接口,这就是为什么我天真地将它们混合在一起的原因.

I thought that the controller "could" have been the interface between the two, that's why I naively mixed them.

在对您的评论进行了一些测试之后,我使控制器仅使用JSF,并且使用@ManagedBean注入了服务(我也不知道使用@ManagedBean可以注入@Service Spring托管的bean),因此答案我的问题:)

After some testing around your comments, I made my controller only use JSF and it injects the services using @ManagedBean (I didn't know either that using @ManagedBean it could inject a @Service Spring-managed bean) so that answers my question :)

下面是正确的代码.

也感谢您将我重定向到正确的方向!

Thanks also for redirecting me in the right direction !

/**
 * 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.

这篇关于jsf-spring:不调用managedbean postconstruct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆