2015-07-10 33 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必須包含您的包裝,如果該類不在上部包裝中...... –