gpt4 book ai didi

Spring session bean 将 FacesContext.getCurrentInstance() 视为 Null 引用

转载 作者:行者123 更新时间:2023-12-05 06:43:37 25 4
gpt4 key购买 nike

<分区>

我有一个处理用户 session 范围数据的 Actor

    import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component(value = "actor")
@Scope(value = "session")
public class Actor{
public Actor() {
System.out.println("Inside void init Actor..........");
System.out.println(JsfUtils.getFacesContext());// This prints Null

}
}

网络.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">      
<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>Faces Servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<context:load-time-weaver aspectj-weaving="off" />
<task:scheduler id="taskScheduler"/>
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="2" />
<property name="maxPoolSize" value="8" />
<property name="queueCapacity" value="2000" />
</bean>
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>


<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="contextApplicationContextProvider" class="net.main.spring.context.ApplicationContextProvider"></bean>

// <context:component-scan base-package="net.main.x">//</context:component-scan> more goes here


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="Default_Persistence_Unit"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="showSql" value="false"/>
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</property>
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="jpaTransactionManager" proxy-target-class="true"/>

</beans>

在旧项目中运行良好,所以我认为 JSF 的版本是我在 Spring 4.1.4 中使用 2.2.12 版的原因。我尝试降级 JSF 版本或升级 Spring 版本,但没有成功。另一个问题是 Actor 构造函数被多次调用。

那么这里是否有一个隐藏的行为,Spring 应该看到 FacesContext 是真的吗,因为请求是由 JSF servlet 映射的,或者它不能因为 FacesContext 在另一个范围。

我假设 Spring 是在 JSF 之前初始化的,因此它将 FacesContext 视为 null,如果是这样的话,您如何订购初始化容器

提前致谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com