0

The code below is used to create many beans and add them to a list for further usage

        final List<Flow<Result>> tasks = new ArrayList<>();

        for (long i : manyLongs) {
            tasks.add((Flow<Result>) ctx.getBean("flowBeanName", "string" number));
        }

manyLongs is an ArrayList of about 350000 items. Maybe more. I have noticed that this piece of code takes about 2 minutes to run. The Bean I am trying to create and add to tasks is a Prototype.

I can understand that creating 350K new objects may take time but 2 minutes seems a bit too much for a lightweight object of two fields. Is there a way to efficiently create those spring objects without altering the design altogether?

UPDATE

Luiggi Mendoza was right. I had two points in the constructor that took a toll on the object creation. The first was that I was a lazy loading call but the worst thing was the de-serialization of a JSON object in the constructor although it does not need to be there.

Just to test I checked creating the bean without Spring and although it is noticeably faster it is just a couple of seconds faster than spring.

idipous
  • 2,868
  • 3
  • 30
  • 45
  • Probably the constructor of the class of `blowBeanName` executes something we're not seeing here, and that piece of code takes this amount of time. I recommend you to use a profiler to see if there's a problem when executing this code, otherwise your concern is futile and you may want/need to rethink your design, in terms of software or hardware. – Luiggi Mendoza Jun 01 '15 at 16:19
  • Do you need those objects to be spring managed? Why not construct them with new? By the way, how long does it takes if you do that? – Robert Jun 01 '15 at 16:23
  • The Spring bean creation process is very involved. It's not as simple as `new Flow(..)`. – Sotirios Delimanolis Jun 01 '15 at 16:47

0 Answers0