Thread: Private constructor with spring

Forum : Spring Core and Aspect Oriented Programming (AOP).   02-11-2018 07:52:01 PM
User Image
Pk25124

Member

Joined: Jun 25, 2017

Points: 810

Threads: 67

Posts: 18

Private constructor with spring

| Quote Date : Nov 2, 2018    Views:160    

Is it possible that ioc container can create the object of 0 Param private constructor if no reason if Yes then how

Comments

User Image
satya1995

Newbie

Joined : Feb 21, 2018

Points : 100

Threads: 11

Posts: 23

Re: Private constructor with spring

Reply Date : Dec 5, 2018

Zero param private constructor means the default constructor of the class is private, which means we cannot call the constructor outside the class not even spring so, IOC container cannot create the been if default constructor is private, it will throw default constructor is private exception (or something like this).
to create the bean of that class we have to use "spring factory method" and we have to give the name of the method who can call the class's private constructor and return the object. or, we have to manually create the object of the class using reflection as you mentioned in below code and give it to IOC container, but spring itself cannot create the object if the default constructor of the class is private.

yes IOC container can create the object for a bean definition even though the corresponding class has private constructor by using Reflection
By following snippet:
[code]
Person.java
--------------

package com.object.create.test;

public class Person {
private Person()
{
}
public void getAdd(int i,int j)
{
System.out.println(i+j);
}

}

Test.java:
----------
package com.object.create.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test {

public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException,
SecurityException, IllegalArgumentException, InvocationTargetException {
Class<?> clazz = Class.forName("com.object.create.test.Person");
Constructor constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
Object object = constructor.newInstance();
Person person=(Person)object;
person.getAdd(10, 20);
}

}
I hope this is correct.



Replied by tharak
User Image
tharak

Newbie

Joined : Oct 14, 2018

Points : 100

Threads: 0

Posts: 1

Re: Private constructor with spring

Reply Date : Dec 5, 2018

yes IOC container can create the object for a bean definition even though the corresponding class has private constructor by using Reflection
By following snippet:
[code]
Person.java
--------------

package com.object.create.test;

public class Person {
private Person()
{
}
public void getAdd(int i,int j)
{
System.out.println(i+j);
}

}

Test.java:
----------
package com.object.create.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test {

public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException,
SecurityException, IllegalArgumentException, InvocationTargetException {
Class<?> clazz = Class.forName("com.object.create.test.Person");
Constructor constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
Object object = constructor.newInstance();
Person person=(Person)object;
person.getAdd(10, 20);
}

}
I hope this is correct.



User Image
Dakshyani

Newbie

Joined : Jul 6, 2017

Points : 100

Threads: 0

Posts: 7

Re: Private constructor with spring

Reply Date : Nov 13, 2018

Ioc Container can create the beans in two ways 1)Constructor injection 2)Setter injection
0 param private constructor means default constructor. 
Using Setter injection first an empty bean is created after that values are injected using setter injection.

Load More

No More Comments