0
 public class DAOManager {
      @Autowired
      private DataSource dataSource;
 .....

When I go to run the above, dataSource remains as null.

How I've configured the bean in web-context.xml:

<!-- Datasource (database) bean -->
<bean id="dataSource"
     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName" value="com.mysql.jdbc.Driver" />
     <property name="url" value="XXXXX" />
     <property name="username" value="XXXX" />
     <property name="password" value="XXXX" />
</bean>

I feel like there's something I need to be adding to my web-context.xml to add the DAOManager class into its jurisdiction.. am I right with this? Sorry, first time Spring MVC user.

Thank you for your time.

tereško
  • 58,060
  • 25
  • 98
  • 150
Samuel Smith
  • 340
  • 4
  • 8
  • 21
  • Do you have a bean definition for `DAOManager`? Spring has to instantate it as well in order to apply autowiring. – Jim Garrison Dec 02 '13 at 17:20
  • @JimGarrison - I do not, no. I'm not really sure where to do this, either. – Samuel Smith Dec 02 '13 at 17:28
  • Let me know if neither the injection or `@Configurable` approaches I describe in my writeup looks like it'll work. Usually, reorganizing the code a bit so that everything's injected is the best long-term approach, but DAO is one common pattern where `@Configurable` is really useful (though usually with Active Record; a full `DAOManager`-type class is usually injected). – chrylis -cautiouslyoptimistic- Dec 02 '13 at 17:29
  • @chrylis - I'm reading through it all now, there's a lot to read/get my head around. I'll get back to you in a bit. – Samuel Smith Dec 02 '13 at 17:36
  • Are you running this in a container such as Tomcat or JBoss? If not you will need to have a top-level Spring-aware class to initiate the whole wiring process. – Jim Garrison Dec 02 '13 at 17:50
  • Yeah, there's a good bit going on under the hood in Spring. It's powerful, but there are a few gotchas with all the moving parts. Short version is that unless you use AspectJ, Spring doesn't know about anything you call `new` on. – chrylis -cautiouslyoptimistic- Dec 02 '13 at 17:59

4 Answers4

0

You ask:

I feel like there's something I need to be adding to my web-context.xml to add the DAOManager class into its jurisdiction.. am I right with this?

You are right :)

Michael Wiles
  • 20,902
  • 18
  • 71
  • 101
0

Put @Repository above your class DAOManager and see.

Though idealy the configuration you did, seems fine, and is a simple DI, and should work fine as it is now.

Abhishek Anand
  • 1,940
  • 14
  • 27
0

Just add

<bean id="daoManager" class="DAOManager">
        <property name="dataSource" ref="dataSource" />
</bean>
-1

Add to your spring xml

<context:component-scan base-package="com.your.package" />
andi
  • 240
  • 1
  • 2
  • 10