1

Most of you probably will say that I should use google for this problem, since it's quite simple, but I can't find a truly correct solution that works.

I have two session-scoped models: Playlist and Track. As you can guess, playlist contains a list of tracks:

@Component
public class Playlist {
    String name;
    List<Track> tracklist;

@Component
public class Track {
    int duration;
    String artist;

I use @Autowired annotation in controllers to get the playlist. In the first controller I set the attributes of Playlist, in the second one I get them. Everything works fine with the name attribute of Playlist, but tracklist is null. I know that I must use something like @Resource or @Qualifier, but I don't understand how to make this annotations works. Simply writing

@Resource
private List<Track> tracks; 

does not seems to work. In my servlet-context.xml the two beans are declared like this:

<bean id="track" class="com.foo.bar.models.Track" scope="session">
    <aop:scoped-proxy/>
</bean>

<bean id="playlist" class="com.foo.bar.models.Playlist" scope="session">
    <aop:scoped-proxy/>
</bean>

I've already look this Spring autowire a list but did not help me, since tracks are not declared via .xml but setted in a controller.

Community
  • 1
  • 1
Fabio
  • 644
  • 1
  • 8
  • 17
  • I fail to see the point in declaring track as a session-scoped bean. You don't store one track in session, you store a playlist. So each time you need the track list, just get the playlist from the session, and iterate through the list. The list is null because you never initialized it. Initialize it to `new ArrayList()`, since it's probably empty initially. – JB Nizet Feb 04 '12 at 12:39
  • Obviously in the first controller I initialize the List and fill it. But when i try to get it from another controller (after calling the first one) the list contains just one track with all fields null. This seems to be quite strange – Fabio Feb 05 '12 at 08:43
  • 1
    Remove the @Resource annotation on tracks and don't configure Track as a component. And show us your code. – JB Nizet Feb 05 '12 at 08:47

1 Answers1

0

Problem solved. I think that my error was overwriting the autowired playlist in the controller with a non-autowired one passed from a service.

The controller code was:

@Autowired
private Playlist playlist;
@Autowired
private MyService service;

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ModelAndView uploadAndParse(@RequestParam("file") MultipartFile file) {
    String name = file.getOriginalFilename();

    playlist = service.getPlaylistFromFile(file); //<--wrong step
    playlist.setCompleteName(name);

    ModelAndView view = new ModelAndView("upload", "list", playlist.getTracks());

    return view;
}

And the service was:

public Playlist getPlaylistFromFile(MultipartFile file) {
    Playlist playlist = new Playlist();
    //do something here...
    return playlist;
 }

Now, I solved removing @Component annotation from Track (as JB Nizet suggest), modifying controller as follow:

playlist.setTracks(service.getListOfTrackFromFile(file));

and service as follow:

public List<Track> getPlaylistFromFile(MultipartFile file) {
    List<Track> tracklist = new ArrayList<Track>();
    //do something here...
    return tracklist;
 }

Like this works. If you think that i'm not hitting the point, please tell me.

Fabio
  • 644
  • 1
  • 8
  • 17