Categories
Programming & Dev

Software Dev phase 7: Beans and its configurations

In this chapter, we will talk about the scopes of the bean, bean life and its configurations

Bean life

so now, there's a way to execute some statements on the initialization and deletion of the beans.
This can be done in many ways. First, we need to declare a statement right after the:

--> ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
--> context.registershutdownHook();

now next step will be to make the changes in the bean class file

so for example,

class Triangle implements InitiazlingBean, DesposableBean{

//two overridden methods will be there, one for executing the initialization
-->System.out.println(): bean created
and another for -> destroy: bean destroyed

}

but, what we can do is, just declare a simple method named called InitBean and DestBean
and execute the same System.out.println() commands.

After that in the configuration file, just get into the bean id command and do:

--> init-method="InitBean' delet-method="dltBean" >

or we can make it global for all beans too, for that we just have to head on to the topmost single bean tag and type:

--> <bean default-init-method="InitBean" default-delet-method="dltBean" >

Now, for the inheritance of beans, not exactly inheritance because it doesn't happen so :/
but what we can do is definition-inheritance, which is done by adding a tag in the derived bean: parent="ParentClassID" and it will fetch all its properties from it.

BeanPostProcessor

Now, we will talk about the Beanpostprocessor

Beanpostprocessor is the class that is executed when a bean is initialized in order to extend the functionalities of the spring. Therefore by the name, it is clear that Bean-Post_processor, processing post initialization of the beans

just create a class and implements it with the Beanpostprocessor interface and overrides the methods. That particular statement will call the class and execute those commands before and after the initialization of the beans

BeanFactoryPostProcessor

BeanFactory post-processor is a class implementing the beanfactorypostprocessor interface, which is used
to add extra functionalities after the initialization of the bean factory.

property placeholder configures an extra .property file to initialize the values of the properties of the beans.

just give the name and value to the variable name in .properties file

{
point1.pointx=10
point1.pointy=20
}


now declare bean,

now one may use it,by just putting the value in the bean as ${point1.pointX}

Coding to interfaces

The actual use of dependency injection -> just one interface and two normal classes.

let's suppose, we have 3 classes now: a triangle, circle and shape interface, which is implemented by triangle and circle classes having an overridden method draw().

Now, In the main(), write the below line of code:

Shape shape=(Shape) get.Beans("Bean_name/triangle/circle"); 

and now whatever the bean we put into the get.beans() method and do shape.draw(), it will show the draw method of only mentioned bean name, which is a type of pure dependency injection.

Configuring Bean for a Class

let's suppose we have a class triangle then,

class triangle{
private int x;
private int y;
getter & setter
void function to draw(){};
}

now in the bean spring.xml file, we will add the dependencies first.. and configure the bean
starting with the

<beans>
            // this is the main big body
</beans>

Inside it, everything will be like:

<beans>
<bean id=" " class=" ">  
// now here the id is the name you want to give it to the bean , class from where u want that bean

<property name=" " value=" "> 
// property is the values to it, name= name of the variable from that class, and value= what value you want to assign it to

</bean>
</beans>

The first method is the setter method and another is constructor, setter can be done by normal bean initialization and that is

<bean id="point1" class="adresss.point">  // id= the name we want to give to bean
<property name="x' value=10 />;           //name= name of the variable to be intialized

now to inject it into the class in which we want to use those values by the constructor method,, we can just simply but the

<constructor-arg index="0" value="10" />;     ---> for      public triangle(int x, String type) { this.x=x; this.type=type;}
<constructor-arg index="1" value="equilateral" />;

for classes having objects of different classes as their member variables, we can do as
declare everything like the setter method with a name given to it and in the class in which we want to use it
just reference it with the below code line;

<bean id ="Triangle" class=" .Triangle" >
<property name="PointA" ref="point1" />
</bean>

//for collections like list it will be like:

bean id="triangle" class="com.SpringCore.beans.Triangle">
  <property name="point">
  <list>
   <ref bean="point1"/>
   <ref bean="point2" />
   <ref bean="point3" />
  </list>
  </property>
 </bean>

And then it is auto wiring to refer automatically if the bean type is the same throughout the program and the name is also the same.

Get the latest tech news and updatesethical hacking tutorials and cybersecurity tips and tricks. Check out MeuSec for more.

Sometimes we include links to online retail stores and/or online campaigns. If you click on one and make a purchase we may receive a small commission.

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *