标签: postconstruct

在任何@PostConstruct之前再次注入所有@Resource

JSR-250表示将在@PostConstruct方法之前调用所有@Resource注释方法.

我的问题是:

这是否意味着在调用任何@PostConstruct注释方法之前,将调用上下文中所有bean的所有@Resource注释方法?或者换句话说,一旦注入了依赖项,就可以调用bean @PostConstruct方法,即使上下文中的其他bean仍没有注入依赖项吗?

此致,蒂姆.

java resources lifecycle spring postconstruct

7
推荐指数
1
解决办法
2365
查看次数

@SessionScoped bean会丢失范围并一直重新创建,字段变为null

我在JSF 2.0项目中使用SessionScoped bean时遇到了一个非常奇怪的问题.使用Netbeans 6.9.1,Glassfish 3服务器和PrimeFaces 3作为JSF组件库.

这是一些代码:

package com.hia.jsf;

import com.hia.netlabel.jpa.Genre;
import com.hia.netlabel.jpa.Label;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

@ManagedBean
@SessionScoped
public class LabelDetailJSF implements Serializable{

@ManagedProperty("#{genreLabelListJSF}")
private GenreLabelListJSF genreLabelListJSF;
private List<Genre> DetailLabelGenreList;
private Label DetailLabel;
/** Creates a new instance of LabelDetailJSF */
public LabelDetailJSF() {
}
@PostConstruct
public void init(){
           System.out.print("Running init LabelDetailJSF");
           if(genreLabelListJSF.getSelectedLabel()!=null)
           {
                System.out.print("genreLabelListJSF was not null");
                this.DetailLabelGenreList=genreLabelListJSF.getSelectedLabel().getGenreList();
                this.DetailLabel= (genreLabelListJSF.getSelectedLabel());
           }
           if(this.DetailLabelGenreList==null){
               System.out.println("Bloody thing became null");

           }
}






/**
 * …
Run Code Online (Sandbox Code Playgroud)

jsf postconstruct primefaces

7
推荐指数
1
解决办法
8910
查看次数

如果ManagedBean在jar库中,则@FostConstruct不会被JSF调用

我正在运行以下问题.

我有一些Managed Beans,目前在两个JSF应用程序之间共享.由于我不想将代码复制并粘贴到两者中(未来会更多),我将这个共享的托管bean放在JAR库中.我关注过这个博客:http://jsflive.wordpress.com/2011/03/24/custom-component-library/

好吧,即使我把faces-config.xml放在JAR/META-INF/@ManagedBean中,@ ViewScoped也不起作用.我无法理解为什么,但如果我在faces-config.xml(JAR,而不是WAR)中注册bean,这个问题就会消失.

我可以忍受这个,但令我惊讶的是,没有为JAR库中的这个托管bean调用@PostConstruct注释.我没有收到任何错误,警告或其他.我想豆子正在加载,但他们的注释没有被处理.

谁有人面对这个?

我的环境:Glassfish 3.1.1(build 12)JSF 2.1.3

提前致谢.

jsf postconstruct managed-bean

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

在@PostConstruct期间使用@Cacheable的Spring缓存不起作用

与spring框架中的提交有关https://github.com/spring-projects/spring-framework/commit/5aefcc802ef05abc51bbfbeb4a78b3032ff9eee3

初始化设置为从afterPropertiesSet()afterSingletonsInstantiated()的后续阶段

简而言之:这可以防止在@PostConstruct用例中使用缓存时缓存工作.

更长的版本:这可以防止您使用的用例

  1. 在methodB上使用@Cacheable创建serviceB

  2. 使用@PostConstruct调用serviceB.methodB创建serviceA

    @Component 
    public class ServiceA{
    
    @Autowired
    private ServiceB serviceB;
    
    @PostConstruct
    public void init() {
        List<String> list = serviceB.loadSomething();
    }
    
    Run Code Online (Sandbox Code Playgroud)

这导致org.springframework.cache.interceptor.CacheAspectSupport现在不进行初始化,因此不会缓存结果.

protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) {
   // check whether aspect is enabled
   // to cope with cases where the AJ is pulled in automatically
   if (this.initialized) {
//>>>>>>>>>>>> NOT Being called
      Class<?> targetClass = getTargetClass(target);
      Collection<CacheOperation> operations = getCacheOperationSource().getCacheOperations(method, targetClass);
      if (!CollectionUtils.isEmpty(operations)) { …
Run Code Online (Sandbox Code Playgroud)

java postconstruct spring-cache

6
推荐指数
2
解决办法
3247
查看次数

条件@PostConstruct注释

我有时可能不需要根据通过JVM参数传递的值在启动时后构造bean.

我尝试了@Conditional注释,但它只适用于@Bean注释.

你以前尝试/需要这样的东西吗?

spring conditional javabeans postconstruct

6
推荐指数
1
解决办法
612
查看次数

使用 Mockito 测试 @Postconstruct

为什么当我通过Mockito我的@Postconstruckt方法注入模拟时没有调用?

@Service
public class MyService {
    public MyService() {
        System.out.println("CONSTRUKTOR");
    }

    @PostConstruct
    public void init() {
        System.out.println("POST CONSTRUCT");
    }

@RunWith(MockitoJUnitRunner.class)
public class Mockito1 {

    @InjectMocks
    private MyService service;

    @Before
    public void init() {
    }
Run Code Online (Sandbox Code Playgroud)

输出: 仅: CONSTRUKTOR

java spring mocking mockito postconstruct

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

@ManagedPropery和@PostConstruct可以放在基类中吗?

我正在使用类的层次结构,我最好尝试做的是@ManagedBean继承一个具有@ManagedProperty成员和@PostConstruct方法的类.

具体来说,这会有用吗?:

public class A {

    @ManagedProperty
    private C c;

    @PostConstruct
    public void init() {
        // Do some initialization stuff
    }

    public C getC() {
        return c;
    }

    public void setC(C c) {
        this.c = c;
    }
}

@ManagedBean
@SessionScoped
public class B extends A {
    // Content...
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

subclass postconstruct jsf-2 managed-bean managed-property

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

如何防止在回发时调用@PostConstruct

当页面第一次加载时,@PostConstruct会调用该页面,但是当我在此页面上执行回发时,@PostConstruct会再次调用该页面.

如何使其仅在初始请求上运行而不是在每次回发时运行?

@PostContruct
public void init() {
    // charge combos....
}

public void submit() { 
    // action 
}
Run Code Online (Sandbox Code Playgroud)

postback postconstruct jsf-2

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

如何在jUnit设置测试上下文之前推迟调用@PostConstruct

我有一个带有受保护的@PostConstruct方法的静态Spring 3.2.4 bean,它在初始化时从DB加载数据.

在创建jUnit测试时,在我的测试方法中,我想在DB中设置数据以适当地测试bean.但是,鉴于bean在我的测试方法之前被实例化,我不知道如何在方法完成之前请求Spring推迟bean的实例化.

鉴于@PostConstruct方法受到保护,我不能直接调用它来重新初始化bean,除非我使用反射.

还有另一种方法可以做到这一点,还是反思唯一的方法?Spring是否有任何Util类使其更容易,或者我是否必须使用标准的java反射?

reflection junit spring postconstruct

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

@Inject和@PostConstruct不能以单例模式工作

我有一个课程如下:

public class UserAuthenticator {

    private static UserAuthenticator authenticator = 

    @Inject
    private UserRepository userRepository;

    @PostConstruct
    public void init() {
        List<User> allUsers = userRepository.findAll();
        for (User user : allUsers) {
            users.put(user.getEmail(), user.getPassword());
            serviceKeys.put(user.getServiceKey(), user.getEmail());
        }
    }

    public static UserAuthenticator getInstance() {
        if (authenticator == null) {
            authenticator = new UserAuthenticator();
        }
        return authenticator;
    }
}
Run Code Online (Sandbox Code Playgroud)

我打电话的时候

UserAuthenticator authenticator = UserAuthenticator.getInstance();
Run Code Online (Sandbox Code Playgroud)

init() 方法未被调用,而userRepository是 null

我的Web应用程序在JBOSS EAP 6.3中运行.

这是怎么造成的,我该如何解决?

java singleton dependency-injection java-ee postconstruct

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