0

I have a Java Project which is written using Spring and am having trouble with @Autowire of a bean. The declaration of the bean

<beans:bean id="messageSource"  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basenames" value="classpath:messages"/>
    <beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>

The bean is being autowired without issue in my class BaseController which falls under the component-scan

<context:component-scan base-package="com.myproj.webservice.controller" />

I want to @Autowire in another class MessageEnum which does not fall under the component-scan. I have tried to add autowire="byType" and autowire="byName" to my bean declaration but the autowiring still did not work.

Please find here also my code for the MessageEnum class

public enum MessageEnum{
    ENUM1("someparam"),
    ENUM2("someparam2");

    @Autorwire
    private MessageSource messageSource;

    private String param;

    //Constructors & getParam and setParam methods

    public MessageSource getMessageSource() {
        return messageSource;
    }
}

What am I missing here for the messageSource to be autowired?

EDIT

I tried to create the MessageEnum as a bean as advised here. I created the default constructor MessageEnum() {}

and set the bean in my xml

<beans:bean id="messageEnum" class="com.myproj.webservice.util.MessageEnum">

but receive the error message

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageEnum' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.myproj.webservice.util.MessageEnum]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.myproj.webservice.util.MessageEnum.<init>()
L. Young
  • 163
  • 3
  • 7
  • 24
  • Why wouldn't you just set new scope for component-scan? I.e. "com.myproj"? – Branislav Lazic Aug 06 '15 at 09:37
  • I have a lot of other classes under that package that don't need to be scanned. My concern is scanning classes uselessy may have an adverse affect on the permformance of the project. If you can do that why don't you just have it scan the entire project right away and avoid ever having these issues? – L. Young Aug 06 '15 at 09:38
  • Oh god... seriously? Did you even google: "Does component-scan affects performances"? – Branislav Lazic Aug 06 '15 at 09:39
  • Have you tried wiith `@Qualifier("messageSource")` ? – Fran Montero Aug 06 '15 at 09:40
  • Component scan affect performance at startup only. – Fran Montero Aug 06 '15 at 09:40
  • @BranislavLazic and found this http://stackoverflow.com/questions/19314922/does-performance-of-spring-component-scan-depend-on-the-size-of-the-scanned-pack "... the number of classes matters. You have to scan only packages witch contains annotated classes" so found it better to go ahead and ask advice from people more experienced than I am – L. Young Aug 06 '15 at 09:42
  • 1
    why dont you create bean of MessageEnum in your xml, that will solve your problem – Keval Aug 06 '15 at 09:46
  • @FranMontero I have tried @Qualifier("messageSource") and it didn't work – L. Young Aug 06 '15 at 09:46
  • You want to autowire inside enum. I have never came across this type of auto-wiring. – Naman Gala Aug 06 '15 at 10:01
  • @Keval I have tried your method, created a default constructor in `MessageEnum` and keep receiving the error message "Could not instantiate bean class ... No default constructor found" – L. Young Aug 06 '15 at 10:13

0 Answers0