16

我已将基于 Spring 4.0 的项目从 xml 转换为 javaconfig。

在初始化时,我的一个 bean 需要通过 Spring @Service ( buildingService) 访问 Hibernate 以从 DB 中获取一些配置数据。bean 初始化如下所示:

@Bean
@DependsOn({ "transactionManager", "webSocketHandler", "buildingService" })
Smarty smarty() {
    Smarty bean = new Smarty();
    bean.init(); // I also tried @Bean(initMethod = "init") with no difference
    return bean;
}

问题是在 中bean.init(),服务被访问,失败并带有NullPointerException.

我添加buildingService了,@DependsOn但没有帮助。

可能在!?@Service之后处理 -annotated 类。@Bean

我可以自己初始化@Service-annotated 类吗?

编辑 1

感谢到目前为止的所有反馈!

我需要添加一些细节:

buildingService 不是 a @Bean,而是 a @Service,看起来像这样:

@Service("buildingService")
@Transactional
public class BuildingService {

...

    public List<Building> getAll() {
        final Session session = sessionFactory.getCurrentSession();
        final Query query = session.createQuery("from Building order by name");
        return query.list();
    }

...

}

Smarty 是一个 Spring 托管的 Bean,并在一个带注释的@Configuration类中初始化,该类正在执行根上下文的初始化。

Smarty 直接依赖于 buildingService,如下所示:

@Resource(name = "buildingService")
private BuildingService     buildingService;

我尝试使用注释Smarty.init()@PostConstruct但这并没有改变任何东西。

请注意,第一件事Smarty.init()是调用buildingService.getAll();

4

3 回答 3

19

您对 bean 的生命周期感到困惑。Spring 必须先创建 bean,然后才能注入任何东西。在您的@Bean方法中,您已经创建了 bean

Smarty bean = new Smarty(); 

然后立即调用其方法之一

bean.init();

这似乎取决于注入的字段。

这两个电话之间没有任何关系。您如何期望 Spring 做任何事情?

相反,您可以init()使用@PostConstruct. 一旦Spring完成初始化你的bean,即。当您的@Bean方法返回并且 Spring 注入所有对象的注入目标时,它将自动调用该方法。

@DependsOn这里没有必要。

于 2014-01-26T14:35:44.847 回答
3

@Sevice带注释的 bean 通过组件扫描自动发现和初始化,以@ComponentScan在 Spring Configuration 上启用此用途。

@ComponentScan

配置与@Configuration类一起使用的组件扫描指令。

@Bean用于手动创建bean,无需使用特殊注释@Service或组件扫描。

@Bean

指示一个方法生成一个由 Spring 容器管理的 bean。(...) 通常,@Bean 方法在 @Configuration 类中声明。在这种情况下,bean 方法可以通过直接调用它们来引用同一类中的其他 @Bean 方法。


上下文配置

@Autowired
EntityManager entityManager; //needs to access Hibernate

@Bean
Smarty smarty() {
   return = new Smarty(entityManager);
}

还有你的Smarty豆子

public Smarty {

   final EntityManager entityManager;

   public Smarty(EntityManager entityManager){
      this.entityManager = entityManager;
   }
}
于 2014-01-26T12:42:47.693 回答
1

您不需要@DependsOn注释,因为 Smarty bean 具有(或应该具有)对 BuildingService 的直接依赖关系。@DependsOn有关何时使用它的更多信息,请参阅javadoc。

以下示例演示了如何解决您的问题:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SmartyTest.TestConfig.class)
public class SmartyTest {

@Autowired
Smarty1 smarty1;

@Autowired
Smarty2 smarty2;

@Test
public void testSmarty() throws Exception {
}

@Configuration
static class TestConfig {

    @Bean
    public BuildingService buildingService() {
        return new BuildingService();
    }

    @Bean
    public Smarty1 smarty1(BuildingService buildingService) {
        Smarty1 smarty = new Smarty1(buildingService);
        smarty.init();
        return smarty; // manually inject dependencies & handle initialisation
    }

    @Bean
    public Smarty2 smarty2() {
        // injecting the building service & initialising the component is handled by spring
        // by using @Autowired & @PostConstruct(-> alternative to @Bean(initMethod="init"))
        return new Smarty2();
    }
}


static class BuildingService {
    public void buildSomething() {
        System.out.println("BuildingService: I am building something!");
    }
}


static class Smarty1 {
    BuildingService buildingService;

    Smarty1(BuildingService buildingService) {
        this.buildingService = buildingService;
    }

    public void init() {
        System.out.println("Smarty 1: initialising ...");
        buildingService.buildSomething();
    }
}

static class Smarty2 {
    @Autowired
    BuildingService buildingService;

    @PostConstruct
    public void init() {
        System.out.println("Smarty 2: initialising ...");
        buildingService.buildSomething();
    }
}
}
于 2014-01-26T17:28:52.030 回答