标签: postconstruct

我可以在接口方法上使用@PostConstruct吗?

我有许多实现接口的bean,我希望它们都具有相同的@PostConstruct.我已经将@PostConstruct注释添加到我的接口方法中,然后添加到我的bean定义中:

<bean class="com.MyInterface" abstract="true" />
Run Code Online (Sandbox Code Playgroud)

但这似乎并没有起作用.如果可能的话,我哪里出错了?

编辑:我已经将注释添加到界面中,如下所示:

package com;
import javax.annotation.PostConstruct;
public interface MyInterface {
    @PostConstruct
    void initSettings();
}
Run Code Online (Sandbox Code Playgroud)

java spring interface postconstruct

15
推荐指数
1
解决办法
4569
查看次数

JSF bean:设置ViewParam后调用@PostConstruct函数

我有一个product.xhtml和一个ProductBean.我使用/ product/{id}来访问产品,所以我在product.xhtml中有一个viewParam,其值为value = ProductBean.id.问题是在bean内部我使用带有PostConstruct注释的init函数来填充产品的细节.为此,我需要id来调用外部函数.我想虽然在viewParam设置bean的id之前调用了init,因此在init中我无法调用外部函数,因为id尚未设置.我做错了什么,如何解决这个问题?

UPDATE

我发现了什么问题.我认为viewParam方法适用于CDI bean,但ManagedProperty方法适用于JSF bean.

我现在还有另一个问题.我的CDI bean是RequestScoped,当渲染product.xhtml时,bean被创建,我猜想以后会被丢弃.有趣的是,我在该bean中有一个函数,当我调用时,我可以读取id(我认为这是因为连接到视图参数)而不是任何其他属性.任何想法如何解决这一问题?

postconstruct jsf-2 managed-bean viewparams

11
推荐指数
1
解决办法
9034
查看次数

即使ManagedBean已经被实例化(例如,在AJAX调用上),也会调用@PostConstruct方法

我有一个@ViewScope ManagedBean和一个@PostConstruct初始化方法.创建新实例时会调用此方法,但也会在每次调用ajax时调用此方法.为什么会这样?

在AJAX调用中,调用并执行init-Method,但不会看到任何更改.例如,如果我在init-Method中更改属性,则仅在实例化时可见,而不是在AJAX调用中可见.对于AJAX调用,值更改在@ViewScoped Bean中不是持久的.

任何人都可以告诉为什么会这样吗?我怎么能改变这个?

ajax jsf postconstruct jsf-2 view-scope

10
推荐指数
1
解决办法
9848
查看次数

如何在测试期间在Spring中禁用@PostConstruct

在Spring组件中我有一个@PostConstruct声明.类似如下:

@Singleton
@Component("filelist")
public class FileListService extends BaseService {

    private List listOfFiles = new Arrays.list();

    //some other functions


    @PostConstruct
    public void populate () {

        for (File f : FileUtils.listFiles(new File(SystemUtils.JAVA_IO_TMPDIR), new String[]{"txt"},true)){
            listOfFiles.add(f.getName());
        }   
    }

    @Override
    public long count() throws DataSourceException {
        return listOfFiles.size();
    }

    //  more methods .....
}
Run Code Online (Sandbox Code Playgroud)

在单元测试期间,我不希望@PostConstruct调用该函数,有没有办法告诉Spring不要进行后期处理?或者是否有一个更好的注释用于在非测试中调用类的启动方法?

java spring annotations postconstruct

10
推荐指数
3
解决办法
7399
查看次数

Mockito和CDI bean注入,@ InjectMocks调用@PostConstruct?

我有这个代码:

class Patient {

  @Inject Syringe syringe;

  @PostConstruct
  void sayThankyouDoc() {

    System.out.println("That hurt like crazy!");

  }

}

@RunWith(MockitoJUnitRunner.class)
class TestCase {

  @Mock
  Syringe siringeMock;

  @InjectMocks
  Patient patient;

  //...

}
Run Code Online (Sandbox Code Playgroud)

我希望Mockito能够调用PostConstruct,但我必须添加:

@Before
public void simulate_post_construct() throws Exception {
    Method postConstruct = Patient.class.getDeclaredMethod("sayThankyouDoc", null);
    postConstruct.setAccessible(true);
    postConstruct.invoke(patient);
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

java junit mockito postconstruct cdi

10
推荐指数
1
解决办法
6877
查看次数

Java 11 - 将 Spring @PostConstruct 替换为 afterPropertiesSet 或使用 initMethod

我正在使用 spring 应用程序,有时用于@PostConstruct代码和测试中的设置

看来注释将被排除在Java 11之外:

请注意,@PostConstruct 和 @PreDestroy 注释都是 Java EE 的一部分。由于 Java EE 已在 Java 9 中弃用并在 Java 11 中删除,我们必须添加额外的依赖项才能使用这些注释

文章建议全部替换@PostConstructafterPropertiesSet方法

我建议您将 @PostConstruct 注释的实现更改为实现 org.springframework.beans.factory.InitializingBean 接口。

我可以在所有情况下盲目更换它吗?还是有其他考虑?

编辑

正如 @JBNizet 所建议的,这可能不是必须或需要的,因为Spring文档建议相反

我们建议您不要使用 InitializingBean接口,因为它不必要地将代码耦合到 Spring。或者,我们建议使用@PostConstruct注释或指定POJO初始化方法。

编辑2

另一种选择是使用initMethod

通过Java配置,可以使用@Bean的initMethod属性

@Bean(initMethod = "init")
public BeanOne beanOne() {
    return new BeanOne();
}
Run Code Online (Sandbox Code Playgroud)

java spring postconstruct spring-bean java-11

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

在super bean和扩展bean上调用@PostConstruct

我有一个带有@PostConstruct的BaseBean,以及一个扩展它的bean,我想调用另一个@PostConstruct.我已经阅读了几个可能的地方,但是,似乎首先调用扩展类的@postConstruct(如果第二个被调用的话).然后我在"上下文"上得到一个NPE,因为我假设已经调用了超级bean的PostConstruct.

这可以吗?如果是这样,我做错了什么?

基豆:

@ManagedBean
@RequestScoped
public class BaseBean {
@ManagedProperty(value = "#{contextBean}")
  private ContextBean contextBean;
  Context context;
@PostConstruct
public void setupContext() {
    context = getContextBean().getContext();
}
Run Code Online (Sandbox Code Playgroud)

扩展bean:

@ManagedBean
@RequestScoped
public class SmartBoxSearchBean extends BaseBean {
@PostConstruct
public void setUp() {
    jsonHelper = context.get(SmartBoxJsonHelper.class);
}
Run Code Online (Sandbox Code Playgroud)

谢谢,Yotam.

jsf postconstruct

9
推荐指数
1
解决办法
3804
查看次数

春天的单身人士被召唤两次

在我的春季申请中遇到一些问题.

我有非常简单的春豆,它们被注入各种其他春豆.在我发现调试时,它们被调用了两次,Constructor和@PostConstruct都被调用了两次.

我的应用程序没有前端技术.它仅用于与后端任务相关.

弹簧配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">


    <context:component-scan base-package="com.green.integration" />

    <!-- ######################################################## -->
    <!-- EXPOSING SPRING BEAN VIA HTTPINVOKER SPRING REMOTING -->
    <!-- ######################################################## -->

    <bean name="/switch"
        class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="SwitchController" />
        <property name="serviceInterface"
            value="com.green.ISwitchController" />
    </bean>

    <!-- Load in application properties reference -->
    <bean id="applicationProperties"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:application.properties" />
    </bean>


    <bean id="mongo" class="com.mongodb.Mongo">
        <constructor-arg value="${mongo.server}" />
        <constructor-arg value="${mongo.port}" />
    </bean>

    <bean id="morphia" class="com.google.code.morphia.Morphia"> …
Run Code Online (Sandbox Code Playgroud)

java singleton spring postconstruct

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

@PostConstruct在Grails服务上静默失败

我认为Spring注释应该在Grails环境中开箱即用,但我根本无法工作.我也尝试了afterProperties方法,它也没有用.

谁能发现错误?我需要做一些配置吗?

package dashboard

import javax.annotation.PostConstruct

class EmailJobSchedulerService
{
    def grailsApplication

    @PostConstruct
    def init() {
        def cronExpression = grailsApplication.config.emailAt8AmTrigger
        println(cronExpression)
        EmailSubscribersJob.schedule(cronExpression, new HashMap())
    }
}
Run Code Online (Sandbox Code Playgroud)

grails spring annotations postconstruct

8
推荐指数
1
解决办法
3097
查看次数

用post构造测试spring bean

我有一个类似于这个的bean:

@Service
public class A {

    @Autowired
    private B b;

    @PostConstruct
    public void setup() {
       b.call(param);
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { Application.class, Config.class })
@WebIntegrationTest(randomPort = true)
public class Test {

    @Autowired
    B b;

    @Before
    public void setUp() throws Exception {
        when(b.call(any())).thenReturn("smth");
    }

    @Test
    public void test() throws Exception {
        // test...
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是在测试运行PostConstruct之前调用setUp.

junit spring postconstruct

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