0

I have problem with @Autowired and @Qualifier annotations. I want to create object of this class:

public class DatabaseUtils {
private String driver, url, username, password;

public void setDriver(String driver) { this.driver = driver; }
public void setUrl(String url) { this.url = url; }
public void setUsername(String username) { this.username = username; }
public void setPassword(String password) { this.password = password; } }

using @Autowired and @Qualifier annotations:

@ContextConfiguration(locations = {"/beans.xml"}) public abstract class AbstractTest {

@Autowired
@Qualifier("dataBase")
public static DatabaseUtils db; }

but as db I got null during debugging. But when I will use construction like that:

ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
    db = (DatabaseUtils)context.getBean("dataBase");

everything is fine. Some suggestion? Here is xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-init-method="true"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
   default-lazy-init="true">

<bean id="dataBase" class="uk.co.coral.DatabaseUtils">
    <property name="driver" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/sports_data"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean>

0 Answers0