Announcement Announcement Module
Collapse
No announcement yet.
Factory with argument at runtime Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Factory with argument at runtime

    Hello!

    I'm a noob, I know...I'm facing an issue that maybe it doesn't have a solution for how I'm thinking of it..

    Is it possible this:

    Code:
    <bean id="randomSoccerPlayerFactory" class="com.peppe.mysoccergame.factory.RandomSoccerPlayerFactory"/>
    
        <bean id="soccerPlayer" factory-bean="randomSoccerPlayerFactory" factory-method="createPlayer">
            <constructor-arg name="nationality" value=""/>
        </bean>
    Code:
        @Resource
        private Map<String, List<String>> namesMap;
        @Autowired
        private RandomSoccerPlayerFactory factory;
    
        @Test
        public void testGetFieldsEnglishDefender() {
            String nationality = "England";        
            SoccerPlayer soccerPlayer = factory.createPlayer("England");
            assertTrue(namesMap.get(nationality).contains(soccerPlayer.getName()));
        }
    I get a failed loaded applicationcontext.xml and it seems trivial because I set an empty constructor-arg ... But.. it's a factory right?

    Shouldn't a factory receive a parameter, an argument and then give an Object?

    I don't understand how it can be useful to declare it if I can't tell it which argument it needs to create an object..

    Maybe the solution is obvious.. but I'm on it too much time..
    Any help? Thanks in advance..
Working...
X