JSF(2.2)ViewScope使用带有JDK 8的Weblogic 12.2.1.2上的Spring 4.3.7(在Mac OS和Docker Oracle Linux环境中)

时间:2017-04-08 13:58:23

标签: java spring jsf weblogic weblogic12c

我尝试使用JSF 2.2.14和Primefaces 6.0配置Spring 4.3.7集成。 Spring应负责管理范围,JSF的ManagedBean成为Spring的bean。

我在Mac OS和Docker上使用Oracle Linux配置了Weblogic 12.2.1.2。

我阅读并测试了这个解决方案(http://www.concretepage.com/spring-4/spring-4-jsf-2-integration-example-using-autowired-annotation),但没有成功。我发现这个(https://github.com/michail-nikolaev/primefaces-spring-scopes/)可以防止内存泄漏问题,但也没有用。

我可以感知独立于范围(会话或视图)Spring正在像请求bean一样工作。

我的配置和bean样本。当我访问导航* .xhtml。如果我将Scope配置为视图或会话,则独立调用PostConstruct上定义的方法,并且分页不起作用,因为视图状态不会持久存在。我不知道会导致这种行为的原因。

WebConfig Spring:

@Configuration
@ComponentScan(basePackages = { "packageC" },basePackageClasses {PackageA.class,PackageB.class})
@lombok.extern.slf4j.Slf4j
public class WebAppConfig{

@Bean
public static CustomScopeConfigurer customScopeConfigurer() {
    log.info("create customScopeConfigurer bean");
Map<String, Object> scopes = new HashMap<>();
    scopes.put("view", new CustomViewScope());

    CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
    customScopeConfigurer.setScopes(scopes);
    return customScopeConfigurer;
}
}

faces-config.xml中:

 <?xml version="1.0" encoding="UTF-8"?>
 <faces-config 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-facesconfig_2_2.xsd"
  version="2.2">

<application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

</application>

 <lifecycle>
    <phase-listener>packagetest.listener.PhaseListener</phase-listener>
</lifecycle>

</faces-config>

WebAppInitializer:

public class WebAppInitializer implements WebApplicationInitializer {

public void onStartup(ServletContext servletContext) throws ServletException {
    log.info("call WebAppInitializer.onStartup");
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(ServiceConfig.class);
    ctx.setServletContext(servletContext);
    servletContext.addListener(new ContextLoaderListener(ctx));
    servletContext.addListener(new RequestContextListener());
    log.info("end call WebAppInitializer.onStartup");
}
}

JSF Bean示例:

@Scope("session")
@Component("accountBean")
@lombok.extern.slf4j.Slf4j
public class AccountManagedBeanImpl extends GenericManagedBeanAb<AccountEntity, Long> implements AccountManagedBean {

private static final long serialVersionUID = 4359183101897060165L;

@Autowired
AccountService accountService;

@PostConstruct
public void init() {    
    log.info("init do bean: AccountManagedBeanImpl. Escopo = session");
    this.entity = new AccountEntity();
    if (getRequest().getParameter("codigo") != null) {
        Optional op = accountService.findByPrimaryKey(Long.parseLong(getRequest().getParameter("codigo")));
        if (op.isPresent()) {
            this.entity = (AccountEntity) op.get();
        }
    }
    this.entityFilter = new AccountEntity();
    this.listEntitySelected = new ArrayList<AccountEntity>(0);
}

1 个答案:

答案 0 :(得分:0)

我在Wildfly 9.x上放了相同的应用程序并且它有效。我可以得出结论,Weblogic上的CDI有不同的行为,并且它不会将上下文委托给SPRING DI。

我不想使用EJB,我更喜欢使用与Spring Data + Spring AOP集成的Spring Service Layer ...

我仍然在寻找解决方案,但这些信息对某人有用