标签: postconstruct

当f:ajax被执行时,总是调用JSF2 preRenderComponent

我有一个由NewsBean.java支持的JSF页面,它具有<f:event type="preRenderComponent" listener="#{newsBean.init}" />bean初始化程序.

有一个在发送它有评论页面底部的按钮: <f:ajax event="click" execute="@form" render="@form" listener="#{newsBean.sendComment}" />并通过包围<h:form>.单击按钮时,NewsBean.init()始终会调用该按钮.

我的bean范围是视图.这是一个有效的行为(总是调用init())?我怎样才能防止总是打电话init()

ajax jsf initializer postconstruct jsf-2

5
推荐指数
1
解决办法
9906
查看次数

每次请求都会调用@ViewScoped的@PostConstruct

我在JSF 2.2.7和Primefaces 5中打开对话框时遇到问题.我有按钮打开一个对话框,每次单击按钮@PostConstruct方法时都会出现问题.为什么?

我只想调用@PostConstruct一次,但我不想将范围更改为Session(使用@SessionScope注释它完美地工作).

这是我的观点:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>

</h:head>
<h:body>
    <h:form id="f1">
        <p:dialog widgetVar="trainingDialog2" id="d1">
            <h:outputText value="#{userViewBean.errorMessage}" />
        </p:dialog>
        <br />

        <p:dataTable id="dt1" value="#{userViewBean.infoList}" var="item">
            <p:column>
                <p:commandButton id="btn" update=":f1:d1"
                    oncomplete="PF('trainingDialog2').show()"
                    styleClass="ui-icon ui-icon-calendar">
                    <f:setPropertyActionListener value="#{item.id}"
                        target="#{userViewBean.errorMessage}" />
                </p:commandButton>
            </p:column>
        </p:dataTable>
    </h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是我的豆子:

package pl.jrola.java.www.vigym.viewcontroller.beans.userview;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;

@ManagedBean(name = "userViewBean")
@ViewScoped
public class UserViewBean implements Serializable {

    private static final long serialVersionUID = …
Run Code Online (Sandbox Code Playgroud)

jsf postconstruct view-scope

5
推荐指数
1
解决办法
5029
查看次数

Spring @PostConstruct取决于@Profile

我想在一个配置类中有多个@PostConstruct注释方法,应该根据@Profile调用它们.您可以想象一个代码剪切如下:

@Configuration
public class SilentaConfiguration {

    private static final Logger LOG = LoggerFactory.getLogger(SilentaConfiguration.class);

    @Autowired
    private Environment env;

    @PostConstruct @Profile("test")
    public void logImportantInfomationForTest() {
        LOG.info("********** logImportantInfomationForTest");
    }

    @PostConstruct @Profile("development")
    public void logImportantInfomationForDevelopment() {
        LOG.info("********** logImportantInfomationForDevelopment");
    }   
}
Run Code Online (Sandbox Code Playgroud)

但是根据@PostConstruct的javadoc,我只能有一个用这个注释注释的方法.在Spring的Jira https://jira.spring.io/browse/SPR-12433中有一个开放的改进.

你是如何解决这个要求的?我总是可以将这个配置类拆分成多个类,但也许你有更好的想法/解决方案.

BTW.上面的代码运行没有问题,但无论配置文件设置如何,都会调用这两种方法.

java configuration spring postconstruct

5
推荐指数
2
解决办法
4388
查看次数

在ManagedProperty中使用ResourceBundle中的Properties

我有一个JSF验证器,我正在构建其中包含我希望从ResourceBundle加载的属性.但是,我不太确定如何使用它,因为它没有正确加载.关于如何使这项工作的任何想法?

我尝试使用a @PostContruct来做,但我在Eclipse中遇到以下错误:

访问限制:由于对所需库/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar的限制,无法访问PostConstruct类型

所以,我不太清楚最好的方法是什么.我正在谈论的一个样本如下......

验证者......

@FacesValidator("usernameValidator")
public class UserNameValidator implements Validator {

  @ManagedProperty(value="#{props_userNamePattern}")
  private String userNamePattern;  

  @ManagedProperty(value="#{props_minUserNameLength}")
  private int minUserNameLength;  

  @ManagedProperty(value="#{props_maxUserNameLength}")
  private int maxUserNameLength;

  public void validate(FacesContext context, UIComponent component, Object
        value) throws ValidatorException {
    //My validations here...   
  }

  //Setters for the class properties

}
Run Code Online (Sandbox Code Playgroud)

faces-config.xml中

<resource-bundle>
    <base-name>settings</base-name>
</resource-bundle>
Run Code Online (Sandbox Code Playgroud)

settings.properties

props_userNamePattern = /^[a-z0-9_-]+$/
props_minUserNameLength = 3
props_maxUserNameLength = 30
Run Code Online (Sandbox Code Playgroud)

jsf resourcebundle postconstruct jsf-2

4
推荐指数
1
解决办法
5579
查看次数

无法在 PostConstruct 上设置 cookie

我有这段代码可以在页面加载期间设置 cookie,但是它不起作用:

标记:

<ui:fragment rendered="#{surveyWebBean.showQuestions}">
    <ui:include src="/general/survey.xhtml" />
</ui:fragment>
Run Code Online (Sandbox Code Playgroud)

代码:

调查WebBean.java

@ManagedBean(name = "surveyWebBean")
@SessionScoped
public class EncuestasWebBean extends BaseBean {

    private boolean showQuestions;

    @PostConstruct
    public void init() {
        showQuestions = true;
        UUID uuid = UUID.randomUUID();
        CookieHelper ch = new CookieHelper();
        ch.setCookie("COOKIE_UUID_SURVEY", uuid.toString(), 60 * 60 * 24 * 365 * 10);
    }

    //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

CookieHelper.java

public class CookieHelper {

    public void setCookie(String name, String value, int expiry) {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest(); …
Run Code Online (Sandbox Code Playgroud)

cookies jsf postconstruct

4
推荐指数
1
解决办法
851
查看次数

如何在 @Configuration 或 @SpringBootApplication 类中访问 ServletContext

我正在尝试更新旧的 Spring 应用程序。具体来说,我试图从旧的 xml 定义形式中提取所有 bean 并将它们拉入 @SpringBootApplication 格式(同时显着减少定义的 bean 总数,因为其中许多不需要豆子)。我当前的问题是我无法弄清楚如何使 ServletContext 对需要它的 bean 可用。

我当前的代码如下所示:

package thing;

import stuff

@SpringBootApplication
public class MyApp {

    private BeanThing beanThing = null;

    @Autowired
    private ServletContext servletContext; 

    public MyApp() {
        // Lots of stuff goes here.
        // no reference to servletContext, though
        // beanThing gets initialized, and mostly populated.
    }

    @Bean public BeanThing getBeanThing() { return beanThing; }

    @PostConstruct
    public void populateContext() {
        // all references to servletContext go here, including the
        // …
Run Code Online (Sandbox Code Playgroud)

java spring autowired postconstruct spring-boot

4
推荐指数
1
解决办法
5787
查看次数

@PostConstruct 中没有会话休眠

MyDao 类具有通过 Hibernate SessionFactory 执行整个持久性任务的方法,它工作得很好。

我在 MyService 中注入了 MyDao,如上所示,但是当注入 MyDao 后调用 @PostConstruct init() 方法时(调试我可以看到 MyDao 注入得很好),会得到下一个 Hibernate 异常:

org.hibernate.HibernateException:没有找到当前线程的会话

我的服务实施。

@Service("myService")
@Transactional(readOnly = true)
public class MyServiceImpl implements MyService {

    @Autowired
    private MyDao myDao;
    private CacheList cacheList;

    @PostConstruct
    public void init() {

        this.cacheList = new CacheList();
        this.cacheList.reloadCache(this.myDao.getAllFromServer());
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

解决方法

正如@ Yogi上面向我推荐的那样,我已经使用 TransactionTemplate 来获取一个有效/活动的事务会话,在这种情况下,我已经通过构造函数实现了并且对我来说工作得很好。

@Service("myService")
@Transactional(readOnly = true)
public class MyServiceImpl implements MyService {

    @Autowired
    private MyDao myDao;
    private CacheList cacheList;

    @Autowired
    public void MyServiceImpl(PlatformTransactionManager transactionManager) { …
Run Code Online (Sandbox Code Playgroud)

spring hibernate postconstruct

3
推荐指数
1
解决办法
4590
查看次数

@Configurable不适用于在@PostConstruct方法中初始化的对象

我通过编译时编织将Spring与AspectJ结合使用,以将@Configurable spring注释用于不受容器管理的对象。

这是一个@Configurable注释的对象示例:

@Configurable(autowire = Autowire.BY_TYPE)
public class TestConfigurable {

    private TestComponent component;

    public TestComponent getComponent() {
        return component;
    }

    @Autowired
    public void setComponent(TestComponent component) {
        this.component = component;
    }
}
Run Code Online (Sandbox Code Playgroud)

我要注入此对象的组件:

@Component
public class TestComponent {}
Run Code Online (Sandbox Code Playgroud)

当我在创建上下文之后创建TestConfigurable时,可以很好地注入TestComponent,但是当我从@ PostConstruct-annotated方法执行此操作时,不会自动装配。

具有@PostConstruct的组件:

@Component
public class TestPostConstruct {

    @PostConstruct
    public void initialize() {
        TestConfigurable configurable = new TestConfigurable();
        System.out.println("In post construct: " + configurable.getComponent());
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在执行的应用程序:

public class TestApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
        applicationContext.registerShutdownHook(); …
Run Code Online (Sandbox Code Playgroud)

spring aspectj configurable compile-time-weaving postconstruct

3
推荐指数
1
解决办法
1203
查看次数

Spring AOP和后期构造

我想写与@PostConstruct一起使用的方法的名称。但是我发现AOP无法“绕过” PostConstruct方法。有什么方法可以将AOP与PostConstruct方法一起使用吗?

aop spring aspectj spring-mvc postconstruct

3
推荐指数
1
解决办法
1499
查看次数

Spring:为什么用@PostConstruct注释的方法不能是静态的?

我正在阅读有关@PostConstruct此网站的文档:https://www.baeldung.com/spring-postconstruct-predestroy

这个已经写完了:

用 @PostConstruct 注解的方法可以具有任何访问级别,但不能是静态的。

有人能告诉我为什么用这个注释注释的方法不能是静态的吗?

java spring postconstruct spring-boot

2
推荐指数
1
解决办法
3583
查看次数