2017-02-15 63 views
0

我使用配置類創建了Spring Boot應用程序。通常使用Spring庫的RestAPI。在裏面我使用我自己的Logger類來記錄每一個動作。Spring Boot無法啓動 - Bean之間存在循環依賴關係

此外,我在我的應用程序類中包含application.properties,其中main函數是。 xml中沒有bean - 只有該配置類中的所有內容。

下面是配置:

@SpringBootConfiguration 
public class ApplicationConfiguration { 

@Value("${hibernate.dialect}") 
private String dialect; 
@Value("${hibernate.show_sql}") 
private String showSql; 
@Value("${hibernate.mappingResources}") 
private String[] mappingResources; 
@Value("${jdbc.driverClassName}") 
private String driverClassName; 
@Value("${jdbc.url}") 
private String url; 
@Value("${jdbc.username}") 
private String username; 
@Value("${jdbc.password}") 
private String password; 

@Autowired 
private DataSource dataSource; 
@Autowired 
private LocalSessionFactoryBean localSessionFactoryBean; 

@Bean 
public LocalSessionFactoryBean localSessionFactoryBean() { 
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 
    sessionFactoryBean.setDataSource(dataSource); 
    Properties hibernateProperties = new Properties(); 
    hibernateProperties.setProperty("hibernate.dialect", dialect); 
    hibernateProperties.setProperty("hibernate.show_sql", showSql); 
    sessionFactoryBean.setHibernateProperties(hibernateProperties); 
    sessionFactoryBean.setMappingResources(mappingResources); 
    return sessionFactoryBean; 
} 

@Bean 
public DataSource dataSource() { 
    DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName(driverClassName); 
    dataSource.setUrl(url); 
    dataSource.setUsername(username); 
    dataSource.setPassword(password); 
    return dataSource; 
} 

} 

另外,我創建的Logger類:

@Service 
public class LinksLogger { 

private static final Logger logger = Logger.getLogger("MyLogs"); 

// methods here... 

} 

在這裏,我如何用我的記錄:

@RestController 
@RequestMapping("/api/v1") 
public class HelloController { 

@Autowired 
private LinksLogger logger; 

@RequestMapping(value = "/hello", method = RequestMethod.GET) 
public Map<String, String> hello() { 
    Map<String, String> data = new HashMap<>(); 
    data.put("data", "Hello#_#World"); 
    logger.log("Hello#_#World"); 
    return data; 
} 
} 

看起來好,對不對? 但是,當我編譯我的代碼,我得到異常:

There is a circular dependency between 2 beans in the application context: 
- helloController (field private com.defaultxyz.linksapi.util.LinksLogger com.defaultxyz.linksapi.controller.HelloController.logger) 
- applicationConfiguration (field private org.springframework.orm.hibernate3.LocalSessionFactoryBean com.defaultxyz.linksapi.util.ApplicationConfiguration.localSessionFactoryBean) 
- localSessionFactoryBean 

爲什麼此異常出現?我在代碼中做了什麼錯誤?

回答

4

你正在嘗試@Autowire bean在實例化的類中。

@Autowired 
private DataSource dataSource; 
@Autowired 
private LocalSessionFactoryBean localSessionFactoryBean; 

但你也定義這些作爲@Bean,因爲它們是在同一個班級創建容器不能注入他們在自動裝配領域。刪除@Autowired字段,而是在需要的地方使用方法。的

sessionFactoryBean.setDataSource(dataSource()); 

代替

sessionFactoryBean.setDataSource(dataSource); 
+1

謝謝!它的作品,我會記得那個練習! :) – DefaultXYZ