SpringCore - Session2
SpringCore - Session2
Possible values
are singleton, prototype, requestScope, sessionScope..
Default scope is “singleton” i.e only one instance will be created in the container.
Now change the bean definition in entities.xml as below to verify the singleton scope
Modify the App.java as shown below to verify the scope of the account bean object.
Account.java
—------------------
package com.raghtech.entities;
public Account() {
accNo=111;
holderName="John";
balance = 5000;
}
App.java
—-------------------
package org.example;
import com.raghtech.entities.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
}
}
}
Even though we are asking for the account object a second time, it is passing the same object
reference created with the first request.
Entities.xml
—-----------------
And rerun the above program. We can see else part statement is printed now as per each
request it will create a new instance
init-method attribute:
If the requirement is acquiring the resources and initializing the bean as soon as it is
created, we can write a method and use init-method attribute to notify the container.
In our case we are defining init() method in Account.java class to intialize the members as
shown below:
Add the init-method attribute in the bean definition in entities.xml file as shown in below:
destroy-method attribute:
destroy-method: Specifies the name of a method to be called when the bean is being
destroyed, allowing you to perform cleanup or release resources associated with the bean.
Add the below method to the Account.java class to perform the cleanup activity.
Close the context object explicitly by writing the below statement in App.java as shown
below:
package org.example;
import com.raghtech.entities.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
((ClassPathXmlApplicationContext)context).close();
}
}
Autowiring:
In Spring Framework, autowiring is a feature that allows the automatic injection of dependencies into a
Spring bean. Instead of manually configuring each dependency with setter methods or constructor
arguments, you can let Spring handle the wiring based on certain rules. Autowiring simplifies
configuration and reduces the amount of XML or Java-based configuration code needed.
No autowiring (no): This is the default behavior. You need to wire the beans explicitly using <property>
or <constructor-arg> tags in XML configuration or using @Autowired annotation in Java configuration.
ByName (byName): Spring looks for a bean with the same name as the property that needs to be
autowired. If found, it injects that bean into the property.
ByType (byType): Spring looks for a single bean of the same type as the property. If exactly one bean is
found, it injects that bean into the property.
Constructor (constructor): Similar to byType, but it's used for constructor injection.
Autodetect (autodetect): This mode first tries byType autowiring. If it fails, it falls back to byName.
Address.java
—------------------
package org.raghtech.bank.model;
public Address() {
hno = "1-397";
street="Liberty Road";
city = "London";
}
public String getHno() {
return hno;
}
Account.java
—-------------------------
package org.raghtech.bank.model;
Using “byName” to inject Address bean into Account bean. Make the below change in
entities.xml file
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-
beans.xsd">
<bean id="account"
class="org.raghtech.bank.model.Account"
init-method="init"
destroy-method="destroy"
autowire="byName"
/>
<bean id="addr" class="org.raghtech.bank.model.Address"/>
</beans>
App.java
—------------------------
package org.example;
import org.raghtech.bank.model.Account;
import org.raghtech.bank.model.Address;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new
ClassPathXmlApplicationContext("app-beans.xml");
Account acc = (Account) context.getBean("account");
System.out.println(acc.getName());
System.out.println(acc.getBalance());
acc.deposit(5000);
Address addr = acc.getAddr();
System.out.println(addr.getCity());
}
}
<bean id="account"
class="org.raghtech.bank.model.Account"
init-method="init"
destroy-method="destroy"
autowire="byType"
/>