Reputation: 11
Recently I've started my adventure with Spring. But I was stoped by strange error. During my test I use Spring Boot, my code look like this:
package m3.watermeters.srv;
..imports...
@ComponentScan
@EnableAutoConfiguration
public class Application {
static Logger log = Logger.getLogger(Application.class.getName());
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring-bean.xml"});
SpringApplication.run(Application.class, args);
}
}
and two most important classes:
public class AppCfgDAO extends JdbcDaoSupport {
static Logger log = Logger.getLogger(AppCfgDAO.class.getName());
public String getGeneralOwnerName() {
return generalOwnerName;
}
private String generalOwnerName;
public void loadCfg() {
String SQL = "SELECT * FROM utl.cfg_dictionary";
List<CfgItemModel> cfgItemModels = (List<CfgItemModel>) getJdbcTemplate().query(SQL, new CfgRowMapper());
Iterator<CfgItemModel> iterator = cfgItemModels.iterator();
while (iterator.hasNext()) {
CfgItemModel cfgItemModel = iterator.next();
log.info("Cfg value [" + cfgItemModel.getKey() + "=" + cfgItemModel.getValue() + "] valueType=[" + cfgItemModel.getValueType() + "]");
if(cfgItemModel.getKey().equals("general.owner_name")) {
generalOwnerName = cfgItemModel.getValue();
} else {
log.error("Configuration variable [" + cfgItemModel.getKey() + "] defined in config file is not allowed");
}
}
}
}
and
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
public AppCfgDAO getAppCfgDAO() {
return appCfgDAO;
}
@Autowired
public void setAppCfgDAO(AppCfgDAO appCfgDAO) {
this.appCfgDAO = appCfgDAO;
}
public AppCfgDAO appCfgDAO;
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
appCfgDAO.loadCfg();
System.out.println("!!!!!!!!!!!!!!" + appCfgDAO.getGeneralOwnerName());
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
and spring conf. XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder system-properties-mode="OVERRIDE" ignore-resource-not-found="true"
location="classpath:config-default.properties, classpath:config.properties"/>
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="DataSourceBean"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://${db.host}:${db.port}/${db.name}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.password}"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<bean id="AppCfgDAOBean" class="m3.watermeters.srv.config.AppCfgDAO">
<property name="dataSource" ref="DataSourceBean" />
</bean>
<bean id="GreetingControlerBean" class="m3.watermeters.srv.GreetingController">
</bean>
</beans>
When I start server I've got such error msg:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'greetingController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void m3.watermeters.srv.GreetingController.setAppCfgDAO(m3.watermeters.srv.config.AppCfgDAO); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [m3.watermeters.srv.config.AppCfgDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
Do you know where is error in my code? :(
Upvotes: 1
Views: 4283
Reputation: 21411
This is probably happening because Spring Boot config class Application is not aware of your xml configuration. Try telling Spring Boot to take into account your XML config by adding the @ImportResource annotation:
@ComponentScan
@EnableAutoConfiguration
@ImportResource("classpath:to/xml/config")
public class Application {
//..
}
Upvotes: 2