package main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.annotation.Student;
public class MainApplication {
public static void main(String[] args) {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("config/hello.xml");
//setEmployeeName example
/*
Employee emp = (Employee)ac.getBean("employeeProp");
System.out.println(emp);
emp.setEmpId(emp.getEmpId()+50);
Employee employeeProp1 = (Employee)ac.getBean("employeeProp");
System.out.println(employeeProp1);
Employee empCons = (Employee)ac.getBean("employeeCons");
System.out.println(empCons);
Employee empCons1 = (Employee)ac.getBean("employeeCons1");
System.out.println(empCons1);
//Constructor injection use index attribute to avoid constructor args ambiguity
Employee empCons2 = (Employee)ac.getBean("employeeCons2");
System.out.println(empCons2);
//Constructor injection use type attribute to avoid constructor args ambiguity
Employee empCons3 = (Employee)ac.getBean("employeeCons3");
System.out.println(empCons3);
Employee empCons4 = (Employee)ac.getBean("employeeCons4");
System.out.println(empCons4);
*/
/*
//Singleton Scope Singleton design pattern .. return the same singleton object on every getBean call
Employee empCons5 = (Employee)ac.getBean("employeeCons5");
System.out.println(empCons5);
Employee empCons5_1 = (Employee)ac.getBean("employeeCons5");
System.out.println(empCons5_1);*/
/*
//lazy init to avoid eager loading .. spring container creating beans
Employee empCons6 = (Employee)ac.getBean("employeeCons6");
System.out.println(empCons6);*/
/*
//Prototype Scope Singleton design pattern // create new object and return on every getBean call
Employee empCons7 = (Employee)ac.getBean("employeeCons7");
System.out.println(empCons7);
Employee empCons7_1 = (Employee)ac.getBean("employeeCons7");
System.out.println(empCons7_1);*/
/*
//Bean life cycle methods
Employee empCons8 = (Employee)ac.getBean("employeeCons8");
System.out.println(empCons8);
//empCons8=null;
//System.gc();
//Runtime.getRuntime().gc();
ac.registerShutdownHook();*/
/*
//Bean life cycle methods
Employee empCons8_ = (Employee)ac.getBean("employeeCons8_");
System.out.println(empCons8_);
ac.registerShutdownHook();*/
//Setter based Injection demo.. without property declaration settor methods will be called (Set Prefix)
/*
Employee employeeProp9 = (Employee)ac.getBean("employeeProp9");
System.out.println(employeeProp9);*/
/*
// setter injecton not working if the method prefix not have "Set"
Employee employeeProp10 = (Employee)ac.getBean("employeeProp10");
System.out.println(employeeProp10);
*/
/*
// bean ref
Employee employeeProp11 = (Employee)ac.getBean("employeeProp11");
System.out.println(employeeProp11);*/
/*
//Inner beans
Employee employeeProp12 = (Employee)ac.getBean("employeeProp12");
System.out.println(employeeProp12);*/
/*
//Association or collection or group of bean references - List
Employee employeeProp13 = (Employee)ac.getBean("employeeProp13");
System.out.println(employeeProp13);*/
/*
//Association or collection or group of bean references - Set
Employee employeeProp14 = (Employee)ac.getBean("employeeProp14");
System.out.println(employeeProp14);*/
/*//using multiple configuration files
Employee employeeProp15 = (Employee)ac.getBean("employeeProp15");
System.out.println(employeeProp15);*/
/*
//error
//using multiple configuration files
ac = new ClassPathXmlApplicationContext(new String[]{"config/department.xml","config/hello.xml"});
Employee employeeProp16 = (Employee)ac.getBean("employeeProp16");
System.out.println(employeeProp16);*/
/*//using FileSystemXmlApplicationContext use relative file path
ApplicationContext fsac = new FileSystemXmlApplicationContext("D:/Balaji/Demo1/src/config/hello.xml");
Employee employeeProp17 = (Employee)fsac.getBean("employeeProp17");
System.out.println(employeeProp17);
*/
/*//using autowire byName
Employee employeeProp18 = (Employee)ac.getBean("employeeProp18");
System.out.println(employeeProp18);
*/
/*//using autowire byType
Employee employeeProp19 = (Employee)ac.getBean("employeeProp19");
System.out.println(employeeProp19);*/
/*
Address address = (Address)ac.getBean("address");
System.out.println(address);*/
/********** Notes **********/
//light weight component - many way of implementations
/* beans Tag default methods declaration
1. default-init-method
2.default-destroy-method
3. default-lazy-init
Life Cycle Method Invocation order:
1.Constructor invoke
2.Application Context Aware - setApplicationContext invoke
3. InitializingBean - afterPropertiesSet invoke
4.Custom Init Method invoke (preference order1) or Default Init Method Invoke (preference order2)
5.DisposableBean - Destroy life cycle method of the bean called
6.Custom Destroy Method invoke (preference order1) or Default Destroy Method Invoke (preference order2)
where we have to use ID attribute and Name attribute in Bean Tag
without ID attribute and Name attribute in Bean Tag - not error but how to retrive .. spring xml based
*/
}
}
package main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.annotation.Arithmatic;
import beans.annotation.Course;
public class MainApplicationAnnotation {
public static void main(String args[]){
ApplicationContext ac = new ClassPathXmlApplicationContext("config/annotationbased.xml");
// ApplicationContext ac = new AnnotationConfigApplicationContext("config/annotationbased.xml");
//using Annotations
//Student student = (Student)ac.getBean("student");
//System.out.println(student);
/* //using Annotations
Course course = (Course)ac.getBean("cours");
System.out.println(course);*/
//using AOP
Arithmatic arithmatic = (Arithmatic)ac.getBean("arithmatic");
System.out.println(arithmatic.add(5, 2));
/*********** Notes *********/
/*
1.Setter based injection via @Value(value = "002")
2.Constructor based injection via @autowired + @Value
@Autowired
public Student(@Value(value = "001") int studentNo, @Value(value = "Bala") String studentName) {
3. class annotations
@Component
@Service
@Repository
@Controller
@RestController
@Configuration
4.@Required Mandatory propery fields.. we need to inject via setter methods.. oly applicable for setter methods
// AnnotationConfigApplicationContext getting error
*
*
*/
}
}
(beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" default-init-method="defaultInitMethod" default-destroy-method="defaultDestoryMethod" )
(!--
(bean id="employeeProp" class="beans.Employee" )
(property name="empName" value="balaji" /)
(/bean)
(bean id="employeeCons" class="beans.Employee")
(constructor-arg value="001" )(/constructor-arg)
(constructor-arg value="balaji" ) (/constructor-arg)
(/bean)
(bean id="employeeCons2" class="beans.Employee")
(constructor-arg value="balaji" index="1") (/constructor-arg)
(constructor-arg value="001" index="0")(/constructor-arg)
(/bean)
(bean id="employeeCons3" class="beans.Employee" )
(constructor-arg value="balaji3" type="String" ) (/constructor-arg)
(constructor-arg value="003" type="int" )(/constructor-arg)
(/bean)
(bean id="employeeCons4" class="beans.Employee" )
(constructor-arg value="balaji4" type="java.lang.String" ) (/constructor-arg)
(constructor-arg value="004" type="int" )(/constructor-arg)
(/bean)
(bean id="employeeCons5" class="beans.Employee")
(constructor-arg value="005" )(/constructor-arg)
(constructor-arg value="balaji5") (/constructor-arg)
(/bean)
(bean id="employeeCons5_" class="beans.Employee" scope="singleton")
(constructor-arg value="005_" )(/constructor-arg)
(constructor-arg value="balaji5_") (/constructor-arg)
(/bean)
(bean id="employeeCons6" class="beans.Employee" lazy-init="true")
(constructor-arg value="006" )(/constructor-arg)
(constructor-arg value="balaji6" ) (/constructor-arg)
(/bean) --)
(!-- (bean id="employeeCons7" class="beans.Employee" scope="prototype")
(constructor-arg value="007" )(/constructor-arg)
(constructor-arg value="balaji7") (/constructor-arg)
(/bean) --)
(!-- (bean id="employeeCons8" class="beans.Employee" init-method="customInitMethod" destroy-method="customDestoryMethod" )
(constructor-arg value="008" )(/constructor-arg)
(constructor-arg value="balaji8") (/constructor-arg)
(/bean) --)
(!-- (bean id="employeeCons8_" class="beans.Employee" )
(constructor-arg value="008" )(/constructor-arg)
(constructor-arg value="balaji8_") (/constructor-arg)
(/bean) --)
(!-- (bean id="employeeProp9" class="beans.Employee" )
(property name="empName" value="balaji9" /)
(property name="sayHello" value="Hello!!" /)
(/bean) --)
(!-- (bean id="employeeProp10" class="beans.Employee" )
(property name="empName" value="balaji10" /)
(property name="sayHello1" value="Hello1!!" /)
(/bean) --)
(!-- (bean id="employeeProp11" class="beans.Employee" )
(property name="empId" value="11" /)
(property name="empName" value="balaji11" /)
(property name="empAddress" ref="address11" /)
(/bean)
(bean id="address11" class="beans.Address")
(property name ="doorNo" value="41" /)
(property name="street" value="Periyar Street" /)
(property name="state" value="Tamil Nadu" /)
(/bean)
--)
(!--
(bean id="employeeProp12" class="beans.Employee" )
(property name="empId" value="12" /)
(property name="empName" value="balaji12" /)
(property name="empAddress")
(bean id="address12" class="beans.Address")
(property name ="doorNo" value="42" /)
(property name="street" value="Periyar Street" /)
(property name="state" value="Tamil Nadu" /)
(/bean)
(/property)
(/bean)
(bean id="employeeProp13" class="beans.Employee" )
(property name="empId" value="13" /)
(property name="empName" value="balaji13" /)
(property name="empSkills")
(list)
(ref bean="skillProp13" /)
(ref bean="skillProp13" /)
(ref bean="skillProp13_" /)
(bean id="skillProp13__" class="beans.Skill" )
(property name="skillId" value="003" /)
(property name="skillName" value="j2ee" /)
(/bean)
(value)skill1,skill2,skill3(/value)
(/list)
(/property)
(/bean)
(bean id="skillProp13" class="beans.Skill" )
(property name="skillId" value="001" /)
(property name="skillName" value="Spring" /)
(/bean)
(bean id="skillProp13_" class="beans.Skill" )
(property name="skillId" value="002" /)
(property name="skillName" value="webservice" /)
(/bean)
(bean id="employeeProp14" class="beans.Employee" )
(property name="empId" value="14" /)
(property name="empName" value="balaji14" /)
(property name="empSkills")
(set)
(ref bean="skillProp14" /)
(ref bean="skillProp14" /)
(ref bean="skillProp14_" /)
(bean id="skillProp14__" class="beans.Skill" )
(property name="skillId" value="003" /)
(property name="skillName" value="j2ee" /)
(/bean)
(value)skill1,skill2,skill3(/value)
(/set)
(/property)
(/bean)
(bean id="skillProp14" class="beans.Skill" )
(property name="skillId" value="001" /)
(property name="skillName" value="Spring" /)
(/bean)
(bean id="skillProp14_" class="beans.Skill" )
(property name="skillId" value="002" /)
(property name="skillName" value="webservice" /)
(/bean) --)
(!-- (import resource="department.xml"/)
(bean id="employeeProp15" class="beans.Employee" )
(property name="empId" value="15" /)
(property name="empName" value="balaji15" /)
(property name="department" ref="dept" /)
(/bean) --)
(!-- (bean id="employeeProp16" class="beans.Employee" )
(property name="empId" value="16" /)
(property name="empName" value="balaji16" /)
(property name="department" ref="dept" /)
(/bean) --)
(!-- (bean id="employeeProp17" class="beans.Employee" )
(property name="empId" value="17" /)
(property name="empName" value="balaji17" /)
(/bean) --)
(!-- (bean id="employeeProp18" class="beans.Employee" autowire="byName" )
(property name="empId" value="18" /)
(property name="empName" value="balaji18" /)
(/bean)
(bean id="department" class="beans.Department" )
(property name="deptId" value="002" /)
(property name="deptName" value="Computer" /)
(/bean)
(bean id="empAddress" class="beans.Address")
(property name ="doorNo" value="41" /)
(property name="street" value="Periyar Street" /)
(property name="state" value="Tamil Nadu" /)
(/bean) --)
(!-- (bean id="employeeProp19" class="beans.Employee" autowire="byType" )
(property name="empId" value="19" /)
(property name="empName" value="balaji19" /)
(/bean)
(bean id="department" class="beans.Department" )
(property name="deptId" value="001" /)
(property name="deptName" value="Computer" /)
(/bean)
(bean id="empAddress" class="beans.Address")
(property name ="doorNo" value="41" /)
(property name="street" value="Periyar Street" /)
(property name="state" value="Tamil Nadu" /)
(/bean)
--)
(bean class="beans.Address")
(property name ="doorNo" value="41" /)
(property name="street" value="Periyar Street" /)
(property name="state" value="Tamil Nadu" /)
(/bean)
(/beans)
(?xml version="1.0" encoding="UTF-8"?)
(beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" default-init-method="defaultInitMethod" default-destroy-method="defaultDestoryMethod" )
(bean id="dept" class="beans.Department" )
(property name="deptId" value="001" /)
(property name="deptName" value="Computer" /)
(/bean)
(/beans)
(?xml version="1.0" encoding="UTF-8"?)
(beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd")
(context:component-scan base-package="beans.annotation,beans" /)
(aop:aspectj-autoproxy /)
(!-- (bean id="arithmatic" class="beans.annotation.Arithmatic"/)
(bean id="myAdvice" class="beans.annotation.MyAdvice"/) --)
(/beans)
package beans;
public class Address {
private int doorNo;
private String street;
private String state;
public int getDoorNo() {
return doorNo;
}
public void setDoorNo(int doorNo) {
this.doorNo = doorNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "Address [doorNo=" + doorNo + ", street=" + street + ", state=" + state + "]";
}
}
package beans;
public class Department {
private String deptId;
private String deptName;
public String getDeptId() {
return deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
}
}
package beans;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Employee implements InitializingBean,DisposableBean,ApplicationContextAware {
private int empId;
private String empName;
private Address empAddress;
private List(Skill) empSkills;
private Department department;
public Employee() {
super();
System.out.println("default constructor called");
}
public Employee(int empId, String empName) {
super();
this.empId = empId;
this.empName = empName;
System.out.println("2-arg constructor called ");
}
public Employee(int empId, String empName, Address empAddress) {
super();
this.empId = empId;
this.empName = empName;
this.empAddress = empAddress;
System.out.println("3-arg constructor called ");
}
public void setSayHello(String str){
System.out.println("setSayHello - "+str);
}
public void sayHello1(String str){
System.out.println("sayHello1 - "+str);
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Address getEmpAddress() {
return empAddress;
}
public void setEmpAddress(Address empAddress) {
this.empAddress = empAddress;
}
public List(Skill) getEmpSkills() {
return empSkills;
}
public void setEmpSkills(List(Skill) empSkills) {
this.empSkills = empSkills;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean - Destroy life cycle method of the bean called");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean - afterPropertiesSet method of the bean called");
}
public void customInitMethod(){
System.out.println("Custom Init Method Called");
}
public void customDestoryMethod(){
System.out.println("Custom Destroy Method Called");
}
public void defaultInitMethod(){
System.out.println("Default Init Method Called");
}
public void defaultDestoryMethod(){
System.out.println("Default Destroy Method Called");
}
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
System.out.println("setApplicationContext method called"+ac);
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + ", empAddress=" + empAddress + ", empSkills="
+ empSkills + ", department=" + department + "]";
}
}
package beans;
public class Skill {
private String skillId;
private String skillName;
public String getSkillId() {
return skillId;
}
public void setSkillId(String skillId) {
this.skillId = skillId;
}
public String getSkillName() {
return skillName;
}
public void setSkillName(String skillName) {
this.skillName = skillName;
}
@Override
public String toString() {
return "Skill [skillId=" + skillId + ", skillName=" + skillName + "]";
}
}