Standalone Spring Application中遇到BeanCreationException

时间:2015-07-10 23:23:57

标签: java spring hibernate spring-jdbc

我正在创建一个基于Spring Framework& amp;的独立应用程序。冬眠。

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:无法自动装配字段:private com.xxx.MappingDao。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

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 + "!";
    }
}