首页 > 解决方案 > ContextRefreshedEvent 未被捕获

问题描述

我正在使用 postgres 数据库设置 spring boot 应用程序 postgres 服务器已启动,与该问题没有太大关系。

以下是我的课程:

package com.example.demo.service.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextStartedEvent;

@SpringBootApplication 
public class ServiceConfiguration {

  public static void main(String[] args) {
      SpringApplication.run(ServiceConfiguration.class, args);
  }
}

package com.example.demo.service.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableAutoConfiguration
@EntityScan(basePackages = {"com.example.demo.persistence"})
@EnableJpaRepositories(basePackages = {"com.example.demo.repositories"})
@EnableTransactionManagement
public class RepositoryConfiguration {
}

package com.example.demo.repositories;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;

import com.example.demo.persistence.Product;

@Component
public interface ProductRepository extends CrudRepository<Product, Integer>{
}

package com.example.demo.persistence;

import javax.persistence.*;
import java.math.BigDecimal;

@Entity
public class Product {
    @Id   
    @GeneratedValue(strategy = GenerationType.AUTO)
    @SequenceGenerator(name="product_id_sequence", sequenceName="product_id_sequence", allocationSize=1)
    @Column(name="ID")
    private Integer id;

    @Version
    private Integer version;

    private String productId;
    private String description;
    private String imageUrl;
    private BigDecimal price;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}

package com.example.demo.bootstrap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.example.demo.persistence.Product;
import com.example.demo.repositories.ProductRepository;

import java.math.BigDecimal;

@Component
public class ProductLoader implements ApplicationListener<ContextRefreshedEvent> {

    private ProductRepository productRepository;

    private Logger log = Logger.getLogger(ProductLoader.class);

    @Autowired
    public void setProductRepository(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

      System.err.println("Flow never comes here !!");

        Product shirt = new Product();
        shirt.setDescription("Spring Framework Guru Shirt");
        shirt.setPrice(new BigDecimal("18.95"));
        shirt.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg");
        shirt.setProductId("235268845711068308");
        productRepository.save(shirt);

        log.info("Saved Shirt - id: " + shirt.getId());

        Product mug = new Product();
        mug.setDescription("Spring Framework Guru Mug");
        mug.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_coffee_mug-r11e7694903c348e1a667dfd2f1474d95_x7j54_8byvr_512.jpg");
        mug.setProductId("168639393495335947");
        productRepository.save(mug);

        log.info("Saved Mug - id:" + mug.getId());
    }
}

ProductLoader 类是一个组件并实现了ApplicationListener,但流程永远不会出现在onApplicationEvent(ContextRefreshedEvent event) 方法中。有人可以帮忙吗?

标签: spring-bootapplicationcontext

解决方案


Got the issue resolved.

Added following annotations to the ServiceConfiguration class 
@SpringBootApplication(scanBasePackages = {"com.example.demo.bootstrap","com.example.demo.persistence"})
@EnableJpaRepositories(basePackageClasses = {ProductRepository.class})
@EntityScan(basePackageClasses=Product.class)
public class ServiceConfiguration {

  public static void main(String[] args) {
      SpringApplication.run(ServiceConfiguration.class, args);
  }
}

推荐阅读