I'm using the following configuration for Sprinc IOC container. My app-config.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myDatasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bus_db" />
<property name="username" value="sysadmin" />
<property name="password" value="adminadmin" />
</bean>
<!-- <context:property-placeholder location="src/main/java/com/bus/assets/dbinfo.properties" /> -->
</beans>
My project sutructure is:
My Bean class that will be used for injection is:
package com.bus.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import com.bus.model.Bus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
@Component("busDAO")
public class BusDAO {
private JdbcTemplate myJdbcTemplate;
public List<Bus> getBusList(){
return myJdbcTemplate.query("select * from bus.bus",
new RowMapper<Bus>() {
@Override
public Bus mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
Bus bus = new Bus();
bus.setBrandName(rs.getString("brand_name"));
bus.setBusId(rs.getInt("bus_id"));
bus.setBusNumber(rs.getString("bus_number"));
bus.setModelNumber("model_number");
//bus.setProdDate(prodDate);
return bus;
}
});
}
public JdbcTemplate getMyJdbcTemplate() {
return myJdbcTemplate;
}
@Autowired
public void setMyJdbcTemplate(DataSource ds) {
this.myJdbcTemplate = new JdbcTemplate(ds);
}
}
However, when I try to test Spring bean injection I'm getting the error that the bean named "busDAO" could not be found. I've also outputed into my Spring Tool Suite console the array of existing beans which is empty.
public class TestConnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext appcon = new FileSystemXmlApplicationContext("/src/main/webapp/WEB-INF/config/app-config.xml");
System.out.println("Bean names: " + Arrays.toString(appcon.getBeanNamesForType(BusDAO.class)));
BusDAO myBusDAO = appcon.getBean("busDAO", BusDAO.class);
List<Bus> myBusList = myBusDAO.getBusList();
for(Bus b: myBusList) {
System.out.println(b.toString());
}
}
}
What I could do better in order to allow my Spring Bean context to find the missing bean?
What I have done: added a property named private DataSource dataSource to busDAO
@Component("busDAO")
public class BusDAO {
private JdbcTemplate myJdbcTemplate;
private DataSource dataSource;
.....
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.myJdbcTemplate = new JdbcTemplate(dataSource);
}
and after that configured the bean acordingly in xml configuration: <bean id="busDAO" class="com.bus.dao.BusDAO"> <property name="dataSource" ref="dataSource" /> </bean>