title: Java Spring @Autowired not working as expected
body: <p>My application is not running as expected due to @Autowired. 
  Would it because of the codes below?</p>
  
  <pre><code>@PropertySource("WEB-INF/config.properties")
  public class DBQuery {
      @Autowired
      private Environment env;
  </code></pre>
  
  <p>I had tried with lots of available solutions and examples for 2 days yet no hope in getting my application running properly. Would greatly appreciate those who can correct me with my codes.</p>
  
  <p><strong>Error Messages</strong></p>
  
  <pre><code>2016-12-15 14:23:04 WARN  XmlWebApplicationContext:487 - Exception encountered during context initialization - cancelling refresh attempt
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dbQuery' defined in ServletContext resource [/WEB-INF/test-servlet.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: : No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
  </code></pre>
  
  <p><strong>Updated Full Stack Error Messages after tried the solution below</strong></p>
  
  <pre><code>&lt;bean id="dbQuery" class="com.cdmDP.db.DBQuery" &gt;
      &lt;constructor-arg type="java.lang.String" value="1"/&gt;
  &lt;/bean&gt;
  
  2016-12-15 15:46:49 ERROR ContextLoader:331 - Context initialization failed
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DBQuery' defined in file [D:\program_files\apache-tomcat-8.0.39\webapps\test\WEB-INF\classes\com\test\db\DBQuery.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: : No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
          at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
          at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
          at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
          at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
          at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
          at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
          at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
          at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
          at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
          at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
          at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
          at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1816)
          at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
          at java.util.concurrent.FutureTask.run(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
          at java.lang.Thread.run(Unknown Source)
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
          at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
          ... 28 more
  </code></pre>
  
  <p><strong>com/test/ViewAllData.java</strong></p>
  
  <pre><code>@Controller
  public class ViewAllData {
      DBQuery dbQuery;
  
      @Autowired
      public ViewAllData(DBQuery dbQuery){
          this.dbQuery = dbQuery;
      }
  
      @RequestMapping("/viewAllData")
      public ModelAndView viewData() throws SQLException, ClassNotFoundException{
          String dataTable;
          ResultSet rs = dbQuery.getAllData();
          //processing
  
          return new ModelAndView("viewAllData", "message", dataTable);
      }
  }
  </code></pre>
  
  <p><strong>com/test/db/DBQuery.java</strong></p>
  
  <pre><code>@Controller
  @Service
  @PropertySource("WEB-INF/config.properties")
  public class DBQuery {
      @Autowired
      private Environment env;
      String dbUsername = env.getProperty("db.username");
      String dbPassword = env.getProperty("db.password");
      String dbUrl = env.getProperty("db.url");
  
      String start;
      @Autowired
      public DBQuery(String start) throws ClassNotFoundException, SQLException{
          this.start = start;
      }
  
      public ResultSet getAllDataPassport() throws SQLException, ClassNotFoundException{
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
          Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
  
          Statement statement = conn.createStatement();
          String sql = "select * from testing_table";
          ResultSet rs = statement.executeQuery(sql);
  
          return rs;
      }
  
  }
  </code></pre>
  
  <p><strong>test-servlet.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      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.xsd
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
     &lt;context:component-scan base-package="com.test, com.test.db" /&gt;
     &lt;context:annotation-config /&gt;
  
      &lt;bean id="viewAllData" class="com.test.ViewAllData"/&gt;
      &lt;bean id="dbQuery" class="com.test.db.DBQuery" &gt;
          &lt;property name="start" value="1"/&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource"&gt;
          &lt;property name="basename" value="messages" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>