Gadgets

Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Saturday, 5 March 2016

Store the details into database using spring framework

Store the details in database using spring framework

 

Steps to create spring MVC web application:

1.      Load the required jar file.
2.      Create the request page to give input.
3.      Create the Controller class.
4.      Create the web.xml file.
5.      Create the spring configuration xml file.
6.      Create output page
7.      Run the project.

Friday, 4 March 2016

Spring MVC Login application

Spring MVC Login application

In this page, I have given spring MVC login application step by step.


Steps to create spring MVC web application:

1.      Load the required jar file.
2.      Create the request page to give input.
3.      Create the Controller class.
4.      Create the web.xml file.
5.      Create the spring configuration xml file.
6.      Create output page
7.      Run the project.

spring MVC web application

First spring MVC web application

In this page, I have given first spring MVC web application step by step.
 In this spring MVC web application we will get the output message on the web browser along with input field as shown in the bellow.


Steps to create spring MVC web application:


Note: load the jar files in lib folder also
1.      Load the required jar file.
2.      Create the request page to give input.
3.      Create the Controller class.
4.      Create the web.xml file.
5.      Create the spring configuration xml file.
6.      Run the project.

Thursday, 17 December 2015

Can we access private constructor in other package

How to access private constructor in other package:
Yes we can access by using reflection classes, we can create instance for private constructors.
To get the class.
Class c=Class.forName("test.Test");
To get all the constructors (public, protected, default, private) in the form of array from the class
Constructor con[]=c.getDeclaredConstructors();
con[0] is indicates first constructor, con[1] indicates second constructor and so an
if setAccessible(true) then all the constructors make as public
if setAccessible(false) then we can’t access all the constructors, we access only public constructors(depending on access specifier rules).
To create object for the class
con[0].newInstance(); 
Test.java
========
package test;
public class Test {
               private Test()
               {
                               System.out.println("private constructor...");                              
               }              
}
Client.java
=========
package core;
import java.lang.reflect.Constructor;
public class Client {
               public static void main(String[] arg)
               {
                               try{
                                              //to get the class
                                              Class c=Class.forName("test.Test");
                                              //to access all the constructors in the array form from the class
                                              Constructor con[]=c.getDeclaredConstructors();
                                              //to make constructor as public
                                              con[0].setAccessible(true);
                                              //to create object
                                              con[0].newInstance();
                               }catch(Exception ex){}
               }
}
/*
Output:
private constructor...
*/

Wednesday, 16 December 2015

First Spring application

Example of spring application
Creating first spring application in eclipse, here we have clear steps to create spring application
1. Create the java project
2. Add spring capabilities
3. Create the class file(POJO class)
4. Create the xml file
5. Create the Driver class
Create New Project:
Go to File menu - New - project - Java Project. Write the Project name and click Finish
Add Spring jar files:
Right click on project – Build Path – configure Build Path – select Libraries tab – Add External JARs


Create POJO class:
Test.java
=======
package test;
public class  Test
{
               public void hello()
               {
                               System.out.println("My first Spring Project");
               }
}

Create xml File:
Open org.springframework.beans-3.0.1.RELEASE-A.jar file with winJAR archiver
 and open folder org\springframework\beans\factory\xml
 in xml folder open spring-beans.dtd file
 in spring-beans.dtd file copy line 35 - 37 below code
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">




spring.xml
========
package resources;
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean class="test.Test" id="t">
</bean>
</beans>
Create Driver Class:
Client.java
========
package test;
class Client{
               public static void main(String[] args)
               {
                               //find xml file
                              Resource r=new ClassPathResource("resources/spring.xml");
                               //load xml file into container
                               BeanFactory factory=new XmlBeanFactory(r);
                               //creat test class object
                               Object o=factory.getBean("t");
                               //type casting object
                               Test t=(Test)o;
                               t.hello();
               }
}
/*
Output:
My first Spring Project
*/