0

I'm upgrading my app from spring 3.x to 4.3. I'm removing xml configuration and using annotation for it. I'm getting following exception:

Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'authorities' and 'username' are required for bean 'getJobUsingQuartz'

What is annotation for following xml code:

<bean id="mcn" 
   class="quartzJobs.jobs.MyClassName">
    <property name="username" value="CronUser"/>
    <property name="authorities">
        <list>
            <value>ROLE_SYSTEM</value>
        </list>
    </property>
 </bean>

I want to inject above value to property using annotation. I'm trying like following:

MyClassName.java

@Configuration
@ComponentScan(basePackages = "quartzJobs.jobs")
public class MyClassName{
   @Autowired
        @Value("CronUser")
        private String username;

        @Autowired
        @Value(value = "{ROLE_SYSTEM}")
        private List<String> authorities;

        @Required
        public
        void setUsername(final String aUsername)
        {
             username = aUsername;
        }

        @Required
        public
        void setAuthorities(final List<String> aAuthorities)
        {
            authorities = aAuthorities;
        }
      }

What I'm doing wrong? please help me.

Anil Jagtap
  • 1,740
  • 4
  • 27
  • 44
  • If I remember correctly, you can't inject collections using the `@Value` annotation. But you can create a list anywhere and turn that into a bean using the `@Component` annotation: `@Component("authorities") private List authorities = new ArrayList(){{add("ROLE_SYSTEM");}}; /* bad style */` You can then inject it, from memory I think like so: `@Resource(name="authorities") private List authorities;` Also check the Spring Expression Language (SpEL); it just might offer something to help you. :) – Christian Apr 13 '17 at 16:49
  • @Christian, Thank you for your response. I tried your suggestion but no luck. still getting same exception – Anil Jagtap Apr 14 '17 at 08:08

2 Answers2

1

This can be achieved with Spring Expression Language (SpEL for short), a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.

This will work for your case:

@Value("#{new String[]{\"ROLE_SYSTEM\"}}")
private List<String> authorities;
Bruno Leite
  • 542
  • 4
  • 12
  • Thank you for response. Still I'm getting BeanInitializationException. I tried your code. Please check my http://stackoverflow.com/questions/43389047/what-is-annotation-for-taskannotation-driven-in-spring-4-3 question which is related to this question – Anil Jagtap Apr 14 '17 at 06:51
0

change the class as

@Configuration
@ComponentScan(basePackages = "quartzJobs.jobs")
public class MyClassName{


    private String username;



    private List<String> authorities;

   @Required
    public void setUsername(final String aUsername)
    {
         username = aUsername;
    }

   @Required
    public void setAuthorities(final List<String> aAuthorities)
    {
        authorities = aAuthorities;
    }
  }

wherever you're going to use an instance of MyClassName you can inject that object as

@Autowired
private MyClassName myClassName;

autowiring will be based on Type. and please refer this Link