6

The normal way without IOC containers would be:

new User("Names", 22);

where the parameter values here are dynamic, whereby, for example, they are fetched via a user submission form, thereby can't be stored in a file.

TextField userNames = new TextField();

names = userNames.getText()

same for the other parameters.

where:

@Component
public class User {
    public User(String names, int age) {
        .
        .
        .
    }
}

How do I initialize User, while passing the constructor's parameters, where User is Autowired into another class:

@Component
public class AnotherClass {
    @Autowired
    User user(....)????? // How do I do it here
    .
    .
    .
}
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • 2
    like that ... http://stackoverflow.com/questions/3153546/how-does-autowiring-work-in-spring – LeTex Dec 21 '14 at 20:40
  • All you need is the @Component annotation and spring takes care of the rest. Make sure you have ComponentScan enabled [see here](http://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/context/annotation/ComponentScan.html) – Jamie White Dec 21 '14 at 20:49
  • 1
    possible duplicate of [Spring : how to replace constructor-arg by annotation?](http://stackoverflow.com/questions/13725165/spring-how-to-replace-constructor-arg-by-annotation) – sol4me Dec 21 '14 at 20:49
  • 1
    Please take a look at my update @sol4me. I don't think that is addressed in your reference - How to collect values from TextFields, for example? – Program-Me-Rev Dec 21 '14 at 21:01
  • @user3663765 You are getting values from form submission, In controller layer can't you use @ModelAttribute? – sol4me Dec 21 '14 at 21:06

2 Answers2

5

I doubt that is what you really want to do. My guess is that User is some kind of model object that should not be handled by Spring's dependency injection.

Dependency injection (which is greatly explained here) creates and wires beans together typically when the container is started or for Spring MVC when a request is executed. The User object must therefore be created before the instance of AnotherClass is created.

If this is part of a request using Spring MVC the @ModelAttribute together with @RequestParam and @PathVariable are likely to be your friends. For some great documentation of this please check the Spring docs

Community
  • 1
  • 1
wassgren
  • 18,651
  • 6
  • 63
  • 77
3
public User(  @Value("Ganesh") String names,               
@Value("27")  int age) {
names=names;
this.age=age;
}

Other than @value, using index in XML also comes with flexibility, if index=0, say, is used two times, the later value is used to overwrite the older value. Similarly, type can be specified in case of overloaded and parametrised constructors and the IOC takes care by itself to select the appropriate constructor. If type or index are not provided, the default order of constructor args is considered and if the parametrised cons does not match, it gIves exception. Suppose, setters are used and inside the bean tag, two property tags are provided for same name, in that case, exception is thrown instead of overriding the value. In setters two parameters are not allowed.

hi.nitish
  • 2,732
  • 2
  • 14
  • 21