2015-07-10 35 views
1

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

在应用类的主要方法如下:

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。

我在做什么错?

+0

您还没有配置任何需要创建'MappingDao'的一个实例。一些jdbc配置我会想象。 –

+0

当我从Spring Boot应用程序调用它时,它的作用相同。我正在做的唯一事情就是将其改为独立应用程序。是的,我同意我在某处丢失了一些配置。 – DilTeam

回答

0

您必须创建一个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 + "!"; 
    } 
} 
+0

我有一个应用程序类。问题是'MappingDao'是一个扩展'CrudRepository'的接口。我如何创建一个'MappingDao'类型的Bean? – DilTeam

+0

componentScan必须包含您的包装,如果该类不在上部包装中...... –