Friday 7 July 2017

Spring-App

Application In Spring

Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developing robust Java applications very easily and very rapidly. Spring Framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.
Now Move towards the coding area which describes simple application with complete configuration and class files.

This tutorial has following structure of files.

1.web.xml inside (WEB-INF folder)
2.request-servlet.xml
3.applicationContext.xml
4.LoginController.java
5.LoginService.java
6.AddressEntity.java
7.DemoEntity.java


1.Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name></display-name>

<servlet>
<servlet-name>request</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:request-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>request</servlet-name>
<url-pattern>/request/*</url-pattern>
</servlet-mapping>
</web-app>


2.request-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan
base-package="com.techlabs.demo.controller,com.techlabs.demo.service" />
<mvc:annotation-driven />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:4040/techlabs" />
<property name="username" value="root" />
<property name="password" value="admin" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.techlabs.demo.entity"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

</beans>




3.applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<context:component-scan base-package="" />
<tx:annotation-driven />

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:common.properties</value>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="" />
<property name="username" value="" />
<property name="password" value="" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value=""></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>


</beans>



4.LoginController.java

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.techlabs.demo.service.LoginService;

@Controller
public class LoginController {

@Autowired
private LoginService loginservice;

@RequestMapping(value = "/login", method = RequestMethod.GET)
public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
loginservice.authenticateService();
loginservice.insert();

System.out.println("Im inside service controller");
}




5.LoginService.java

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.techlabs.demo.entity.AddressEntity;
import com.techlabs.demo.entity.DemoEntity;

@Controller
public class LoginService {
@Autowired
private SessionFactory sessionFactory;

public void authenticateService() {
System.out.println("From the login service ");
}

public void insert() {
Session session = sessionFactory.openSession();
Set<AddressEntity> addressEntities = new HashSet<AddressEntity>();

AddressEntity addressEntity1 = new AddressEntity(1, "mumbai");
AddressEntity addressEntity2 =new AddressEntity(2, "pune");
addressEntities.add(addressEntity1);
addressEntities.add(addressEntity2);


DemoEntity demoEntity =new DemoEntity(100, "sanket", addressEntities);
Transaction transaction = (Transaction) session.beginTransaction();
for(AddressEntity addressEntity : addressEntities)
{
addressEntity.setAddressEntity(demoEntity);
}
session.save(demoEntity);
transaction.commit();
session.close();
}

}



6.AddressEntity.java

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.springframework.beans.factory.annotation.Autowired;

@Entity
public class AddressEntity {
@Id
private long id;
private String address;

@JoinColumn
@ManyToOne
private DemoEntity demoEntity;

public DemoEntity geDemoEntity() {
return demoEntity;

}

public void setDemoEntity(DemoEntity demoEntity) {
this.demoEntity = demoEntity;
}

public AddressEntity(long id, String address) {
super();
this.id = id;
this.address = address;
}

public AddressEntity() {

}

public void setAddressEntity(DemoEntity demoEntity2) {
// TODO Auto-generated method stub

}

}


7.DemoEntity.java

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
public class DemoEntity {
@Id
private long id;
private String name;
@OneToMany(mappedBy = "demoEntity", cascade = CascadeType.ALL)
private Set<AddressEntity> addressEntities;

public DemoEntity(long id, String name, Set<AddressEntity> addressEntities) {
this.id = id;
this.name = name;
this.addressEntities = addressEntities;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public DemoEntity() {
}

public void addAddressEntity(AddressEntity addressEntity) {
addressEntities.add(addressEntity);
}
}


 output:

Spring With MySql Database

Spring-App

Application In Spring Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developi...