0

i am studying spring framework i have some doubts to confirm:

  1. I see that class objects are created as beans in xml file ... but my doubt is ... only pojo class beans need to be defined in xml for instatiation or all classes eg: my custom code class EncryptionUtil class that helps in encrypting data and so on custom logic classes also need to be instantiated using beans?This is my primary concern

  2. what about cases like i use

    JSONObject j = new JSONOBJect() (External libs);
    ArrayList<String> a = new ArrayList<String>();
    

( java default object and collections) do these classes also need to have bean in xml?

  1. i exactly dont know if spring ioc will instantiate each and every object or we need to instantiate only some objects also, in spring app does "new " keyword work for creating objects

  2. What should we use to instantiate bean in spring mvc ? ie : like i used ApplicationContext in my spring app , should i get bean everywhere i need

  3. will there be any problem if i use multiple annotations ie : of spring as well as hibernate on same class at same time? eg: something like this

    @Id @GeneratedValue
    @Column(name = "id")
    private int id;
    

    but if i want id to be autowired too ...

    @Autowired @Id @GeneratedValue @Column(name = "id") private int id;

will this work?

1 Answer 1

3
  1. Declare only classes as Spring Beans which you don't want to create with new. Further Spring Beans are normally singletons. This means there is only one instance of this class. In most cases Spring Beans are not POJOs. E.g mostly i declare DAOs, Service, Controller classes as Spring Beans.
  2. No there is no need to declare these as Spring Beans.
  3. Yes, new works. But these instances are not managed by the spring container. And you should only instantiate non spring beans with new. Spring Beans itself are instantiated by the Spring IOC container.
  4. Inject beans to other beans with xml config or by annotation config (@Autowired).
  5. You can mix spring annotations with annotations of other frameworks. But your example with the id doesn't work and makes no sense, since entity id's wont' be injected. Also your Hibernate entity must be also a spring bean (in this case declared as prototype). You can inject values and beans only to spring beans.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.