【问题标题】:BeanCreationException encountered in Standalone Spring Application独立 Spring 应用程序中遇到 BeanCreationException
【发布时间】:2015-07-10 23:23:57
【问题描述】:

我正在创建一个基于 Spring Framework 和 Hibernate 的独立应用程序。

Application类的main方法如下所示:

public static void main(String[] args) {
    System.out.println("Starting Application....");
    ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
    Ingest ingest = context.getBean(Ingest.class);
    ingest.ingest(args[1]);
}

在 IngestionImpl 中,我已经:

@ComponentScan
@Component
public class IngestImpl implements Ingest {

    private static final Logger logger = LogManager.getLogger(IngestImpl.class);

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    private MappingDao mappingDao;

MappingDao 如下所示:

@Component
@Transactional
public interface MappingDao extends CrudRepository<Mapping, Long> {
    public List<Mapping> findByType(String type);
}

当我运行它时,我得到了

BeanCreationException:无法自动装配字段:私有 com.xxx.MappingDao。

我做错了什么?

【问题讨论】:

  • 您尚未配置创建MappingDao 实例所需的任何内容。我可以想象一些 jdbc 配置。
  • 当我从 Spring Boot 应用程序调用它时,相同的代码可以工作。我唯一要做的就是将其更改为独立应用程序。是的,我同意我在某处遗漏了一些配置。

标签: java spring hibernate spring-jdbc


【解决方案1】:

您必须创建一个 Configuration 类并在该类中使用 @ComponentScan 配置,例如 Application.class,还必须在此应用程序中创建 bean,请参阅下面的有效示例。

package com.brito.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@EnableCaching
@ComponentScan("com.brito.service.impl")
@PropertySource("classpath:/redis.properties")
public class CacheConfig extends CachingConfigurerSupport {

    @Value("${redis.host-name}") 
    private String redisHostName;

    @Value("${redis.port}") 
    private int redisPort;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
        redisConnectionFactory.setHostName(redisHostName);
        redisConnectionFactory.setPort(redisPort);
        return redisConnectionFactory;
    }

     @Bean
     public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
         RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
         redisTemplate.setConnectionFactory(cf);
         return redisTemplate;
     }

     @Bean
     public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);

        // Number of seconds before expiration. Defaults to unlimited (0)
        cacheManager.setDefaultExpiration(300);
        return cacheManager;
      }
}

package com.brito.service.impl;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.brito.service.MessageService;


@Service("messageService")
public class MessageServiceImpl implements MessageService {

    @Override
    @Cacheable("messages")
    public String getMessage(String name) {
         System.out.println("Executing MessageServiceImpl" + ".getHelloMessage(\"" + name + "\")");
         return "Hello " + name + "!";
    }
}

【讨论】:

  • 我确实有一个应用程序类。问题是“MappingDao”是一个扩展“CrudRepository”的接口。如何创建“MappingDao”类型的 Bean?
  • componentScan 必须包含您的包,如果该类不在上层包中...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 2012-09-20
  • 2016-07-04
  • 1970-01-01
  • 2012-12-20
  • 2021-05-24
相关资源
最近更新 更多