Spring :- Every one think's it is a frame-work need to learn it . But person who is strong in Java/J2ee can write framework by him self first be strong in java,j2ee before moving to spring. Spring internal implementation is servlet/jsp if you open the jar files and read it.
Spring definition :- Its a globally used framework which easily integrates with most of the existing java framework. Its easily usability and reducing the amount of code written by developer made it more popular. It has concepts for every layer like core level,application level,database level,service level.i mean it has templates readily available .Which will make your work easy.
Modules in spring ;-
1) Core layer :- Contains beans,context, expression language
2) Test layer using which we can mock spring request and response.
3) Aspect layer :- It contains aspects, point-cut etc.. using which we can target already implemented method and inject code in starting and ending.
4) Database layer :- It contains readily available templates for jdbc,jms,orm,oxm etc...
5) Web layer :- It contains all web related features with readily integration with struts,portal etc.
First spring application :- pre-requite google it and download latest eclipse , tomcat and latest spring jars.
Create a Person class :- Spring does not insist you to implement any interfaces or extend classes. You can just create a pojo classes.
package com.spring.core.example;
public class Account {
private String accountNumber;
private String name;
private int phone;
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
Create spring xml :- Beans are important part in spring every things need's to be declared as beans given below. spring will make sure to create a object for it and make it available in jvm.
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="personBean" class="com.spring.core.example.Account">
<property name="name" value="krishna"></property>
<property name="accountNumber" value="4880908191827"></property>
<property name="phone" value="979"></property>
</bean>
</beans>
Create spring class to invoke it :- We need to load xml and give it to spring initiator class Beanfactory which will parse the xml and create object in JVm using java reflection.
package com.spring.core.example;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringClass {
public static void main(String args[]){
Resource resource = new ClassPathResource("com/spring/core/example/applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Account account = (Account) factory.getBean("personBean");
System.out.println(account.getAccountNumber());
System.out.println(account.getName());
System.out.println(account.getPhone());
}
}
Spring definition :- Its a globally used framework which easily integrates with most of the existing java framework. Its easily usability and reducing the amount of code written by developer made it more popular. It has concepts for every layer like core level,application level,database level,service level.i mean it has templates readily available .Which will make your work easy.
Modules in spring ;-
1) Core layer :- Contains beans,context, expression language
2) Test layer using which we can mock spring request and response.
3) Aspect layer :- It contains aspects, point-cut etc.. using which we can target already implemented method and inject code in starting and ending.
4) Database layer :- It contains readily available templates for jdbc,jms,orm,oxm etc...
5) Web layer :- It contains all web related features with readily integration with struts,portal etc.
First spring application :- pre-requite google it and download latest eclipse , tomcat and latest spring jars.
Create a Person class :- Spring does not insist you to implement any interfaces or extend classes. You can just create a pojo classes.
package com.spring.core.example;
public class Account {
private String accountNumber;
private String name;
private int phone;
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
Create spring xml :- Beans are important part in spring every things need's to be declared as beans given below. spring will make sure to create a object for it and make it available in jvm.
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="personBean" class="com.spring.core.example.Account">
<property name="name" value="krishna"></property>
<property name="accountNumber" value="4880908191827"></property>
<property name="phone" value="979"></property>
</bean>
</beans>
Create spring class to invoke it :- We need to load xml and give it to spring initiator class Beanfactory which will parse the xml and create object in JVm using java reflection.
package com.spring.core.example;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringClass {
public static void main(String args[]){
Resource resource = new ClassPathResource("com/spring/core/example/applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Account account = (Account) factory.getBean("personBean");
System.out.println(account.getAccountNumber());
System.out.println(account.getName());
System.out.println(account.getPhone());
}
}
Output :- Below output clearly shows spring has initiated the class with required details and constructed the object. In this way we can create any database class by passing values in it and use it every were .
No comments:
Post a Comment