0

I'm trying to inject messageSource bean to one my component classes.

Here is part of bean xml:

<context:annotation-config />

<context:component-scan base-package="com.mattis.test"/>

<bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

<bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basename" value="messages" />
</bean>

And in my component I've got this:

@Component
public class TestClass {

@Autowired
private MessageSource messageSource;

<-- more code goes here -->
}

Always when I instantiate TestClass, messageSource is null. I tried more bean xml and class configurations but none of them worked.

mattis
  • 211
  • 3
  • 10

1 Answers1

5

You're manually calling new TestClass(). Spring autowiring only works on managed beans.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152