Drop Down

Showing posts with label SPRING. Show all posts
Showing posts with label SPRING. Show all posts

Saturday, September 21, 2019

Spring_MVC_1.1_Login (Using annotations)

-------LoginController

package com.amit.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LoginController implements Controller
{
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception
{
String uname = req.getParameter("uname");
String upwd = req. getParameter("upwd");
ModelAndView mav = null;
if(uname.equals("amit") && upwd.equals("amit"))
{
mav = new ModelAndView("success");
}else {
mav = new ModelAndView("failure");
}


return mav;
}

}
==================================================================
---- LoginPageController


package com.amit.controllers;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LoginPageController implements Controller
{

@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception
{

return new ModelAndView("loginForm");
}

}


=====================================================================
-----ds-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="/loginPage" class="com.amit.controllers.LoginPageController"/>
<bean name="/login"  class="com.amit.controllers.LoginController"/>
<bean name = "handlerMapping" class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <bean name = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/>
     <property name="suffix" value = ".jsp"/>
        </bean>
</beans>        

===================================================================
------failure.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Amit Software Solutions</h2>
<h3>User Login Page</h3>
<br><br>
<h1>Login Failure</h1>
</body>
</html>


=====================================================================
-------loginform.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h2>Amit Software Solutions</h2>
<h3>User Login Page</h3>
<form method="post" action="login">
<table>
<tr>
<td>User Name</td>
<td><input type="text" name ="uname"/></td>
</tr>
<tr>
<td>User Password</td>
<td><input type="password" name ="upwd"/></td>
</tr>
<tr>
<td><input type="submit" value="Login"/></td>
</tr>
</table>

</form>
</center>
</body>
</html>

========================================================================

---------success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Amit Software Solutions</h2>
<h3>User Login Page</h3>
<br><br>
<h1>Login Success</h1>
</body>
</html>

========================================================================
-------- web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>mvc1.3_Login_xml</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
<servlet-name>ds</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ds</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> 
  
</web-app>



=======================================================================
-------index.jsp



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="loginPage"/>
</body>
</html>

========================================================================




Spring_MVC_1.0 (Login using xml)

----LoginController

package com.amit.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LoginController implements Controller
{
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception
{
String uname = req.getParameter("uname");
String upwd = req. getParameter("upwd");
ModelAndView mav = null;
if(uname.equals("amit") && upwd.equals("amit"))
{
mav = new ModelAndView("success");
}else {
mav = new ModelAndView("failure");
}


return mav;
}

}

=====================================================================
-----LoginPageController

package com.amit.controllers;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LoginPageController implements Controller 
{

@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception 
{
return new ModelAndView("loginForm");
}

}

======================================================================
-----ds-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="/loginPage" class="com.amit.controllers.LoginPageController"/>
<bean name="/login"  class="com.amit.controllers.LoginController"/>
<bean name = "handlerMapping" class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <bean name = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/>
     <property name="suffix" value = ".jsp"/>
        </bean>
</beans>        

=======================================================================
-----web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>mvc1.3_Login_xml</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
<servlet-name>ds</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ds</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> 
  
</web-app>

=======================================================================
--- index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="loginPage"/>
</body>
</html>
======================================================================
--failure.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Amit Software Solutions</h2>
<h3>User Login Page</h3>
<br><br>
<h1>Login Failure</h1>
</body>
</html>
=======================================================================
---loginform.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h2>Amit Software Solutions</h2>
<h3>User Login Page</h3>
<form method="post" action="login">
<table>
<tr>
<td>User Name</td>
<td><input type="text" name ="uname"/></td>
</tr>
<tr>
<td>User Password</td>
<td><input type="password" name ="upwd"/></td>
</tr>
<tr>
<td><input type="submit" value="Login"/></td>
</tr>
</table>

</form>
</center>
</body>
</html>
======================================================================
-----success.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Amit Software Solutions</h2>
<h3>User Login Page</h3>
<br><br>
<h1>Login Success</h1>
</body>
</html>
=======================================================================

Spring_App_20_Data_Validation

package com.amit.beans;

public class User
{
private String uname;
private String updw;
private String user;
private String umobile;
private String uemail;

public String getUname()
{
return uname;
}
public void setUname(String uname)
{
this.uname = uname;
}
public String getUpdw()
{
return updw;
}
public void setUpdw(String updw)
{
this.updw = updw;
}
public String getUser()
{
return user;
}
public void setUser(String user)
{
this.user = user;
}
public String getUmobile()
{
return umobile;
}
public void setUmobile(String umobile)
{
this.umobile = umobile;
}
public String getUemail()
{
return uemail;
}
public void setUemail(String uemail)
{
this.uemail = uemail;
}

public void getUserDetails()
{
System.out.println("--------------USer Details------------");
System.out.println("User Name: "+uname);
System.out.println("User password: "+updw);
System.out.println("User UserName: "+user);
System.out.println("User Mobile: "+umobile);
System.out.println("User Email: "+uemail);

}

}

======================================================================
package com.amit.test;

import java.util.HashMap;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.MapBindingResult;
import org.springframework.validation.ObjectError;

import com.amit.beans.User;
import com.amit.validators.UserValidator;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("userbean");
user.getUserDetails();
 
UserValidator userValidator = (UserValidator) context.getBean("userValidatorBean");
HashMap<String,String> map = new HashMap<String,String>();
MapBindingResult results = new MapBindingResult(map,"com.amit.beans.User");
userValidator.validate(user, results);
List<ObjectError> list = results.getAllErrors();
for(ObjectError err : list)
{
System.out.println(err.getDefaultMessage());
}

}

=====================================================================

package com.amit.validators;

import java.io.IOException;
import java.util.Properties;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.amit.beans.User;

public class UserValidator implements Validator 
{
private Resource resource;  // to make available property file resorce here
public Resource getResource() {
return resource;
}

public void setResource(Resource resource) {
this.resource = resource;
}
@Override
public boolean supports(Class type) 
{
return User.class.equals(type);
}

@Override
public void validate(Object obj, Errors error) 
{
User user = (User)obj;
try 
{
Properties p = (Properties) PropertiesLoaderUtils.loadProperties(resource);
if(user.getUname() == null || user.getUname().equals(""))
{
  error.rejectValue("uname", "error.uname.required" , p.getProperty("error.uname.required"));
}
catch (IOException e) 
{
e.printStackTrace();
}

}

}

======================================================================
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        <bean id= "userbean"  class = "com.amit.beans.User">
        <property name = "uname" value = ""></property>
        <property name = "updw" value = "pass@123"></property>
        <property name = "user" value = "manjhi"></property>
        <property name = "umobile" value = "8744973456"></property>
        <property name = "uemail" value = "amit@gmail.com"></property>
        </bean>
        
        <bean id="userValidatorBean" class = "com.amit.validators.UserValidator">
        <property name="resource" value="Messages.properties"></property>
        </bean>
        
</beans>        
================================Messages.properties========================

error.uname.required = UserName is Required

Spring_App_19_BankApp_AutoDiscovery_Service_2

package com.amit.Dao;

public interface AccountDao
{
public String create(String accNo, String accName, String accType, int balance);
public String search(String accNo);
public String update(String accNo, String accName, String accType, int balance);
public String delete(String accNo);

}

=====================================================================
package com.amit.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

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

import oracle.jdbc.pool.OracleDataSource;

@Repository("dao")    //-->to making it autodiscover     //you can also use @component("dao") 
public class AccountDaoImpl implements AccountDao 
{
@Autowired   //-------> this will inject datasource object into AccountDao (setter/getters not required)
private OracleDataSource datasource;
String status = "";
@Override
public String create(String accNo, String accName, String accType, int balance) 
{
  try 
  {
  Connection con = datasource.getConnection();
  PreparedStatement pst = con.prepareStatement("insert into account values (?,?,?,?)");
 
  pst.setString(1, accNo);
  pst.setString(2, accName);
  pst.setString(3, accType);
  pst.setInt(4, balance);
  pst.executeUpdate();
  status= "success";
  }
  catch(Exception e)
  {
  status = "failure";
  e.printStackTrace();
  }
return status;
}

@Override
public String search(String accNo) {
return null;
}

@Override
public String update(String accNo, String accName, String accType, int balance) {
return null;
}

@Override
public String delete(String accNo) {
return null;
}
public void showDetails()
{
}

}

==================================================================
package com.amit.service;

public interface AccountService 
{
public String createAccount(String accNo, String accName, String accType, int balance);
public String searchAccount(String accNo);
public String updateAccount(String accNo, String accName, String accType, int balance);
public String deleteAccount(String accNo);
}

==================================================================
package com.amit.service;

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

import com.amit.Dao.AccountDao;
@Service("accService")
public class AccountServiceImpl implements AccountService 
{
@Autowired(required=true)
    private AccountDao dao;
@Override
public String createAccount(String accNo, String accName, String accType, int balance) 
{
return dao.create(accNo, accName, accType, balance);
}

@Override
public String searchAccount(String accNo) 
{


return null;
}

@Override
public String updateAccount(String accNo, String accName, String accType, int balance) 
{


return null;
}

@Override
public String deleteAccount(String accNo) 
{


return null;
}

}
====================================================================
package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amit.Dao.AccountDaoImpl;
import com.amit.service.AccountService;

public class Test 
{

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accserv = (AccountService)context.getBean("accService");
String status = accserv.createAccount("abc123", "Amit", "Saving", 20000);
if(status.equalsIgnoreCase("success"))
{
System.out.println("Account created successfully");
}
else 
{
System.out.println("Oops! something went wrong");
}
 
}

}

=======================================================================
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.amit.service"/>
    <context:component-scan base-package="com.amit.Dao"/> <!-- Aee, IOC go to this package and scan it for (component,repository,service,controller) & no need to check for bean config-->
    
    <bean id="datasource" class="oracle.jdbc.pool.OracleDataSource">  <!--Driver class Name is not required as it will take automatically -->
    <property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
    <property name="user" value = "scott"></property>
    <property name="Password" value = "tiger"></property>
    </bean>
    
    <!-- <bean id="dao" class ="com.amit.Dao.AccountDaoImpl"> </bean>  -->
    <!-- Not required when we are using Auto-Discovery/streotype ..As ID (dao) is mentioned in AccontDAOImpl @Repository("dao") -->
</beans>

Spring_App_18_BankApp_AutoDiscovery_ServiceLayer_Impl

package com.amit.doa;

public interface AccountDao
{
public String create(String accNo, String accName, String accType, int balance);
public String search(String accNo);
public String update(String accNo, String accName, String accType, int balance);
public String delete(String accNo);


}

======================================================================
package com.amit.doa;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import oracle.jdbc.pool.OracleDataSource;

@Repository("accdao")     //reference for ioc , to avoid <bean id="acc_dao" clas....>
public class AccountDaoImpl implements AccountDao 
{
String status= " ";
@Autowired(required=true)   // injected without setter & getters
private OracleDataSource datasource;

@Override
public String create(String accNo, String accName, String accType, int balance) 
{
try 
{
Connection con = datasource.getConnection();
PreparedStatement pst = con.prepareStatement("insert into accout values (?,?,?,?)");
pst.setString(1, accNo);
pst.setString(2, accName);
pst.setString(3, accType);
pst.setInt(4, balance);
pst.executeUpdate();
System.out.println("r_val >> ");
/*if(r_val!=0)
{
*/
status = "success";
/*
* } else { status = "fail"; }
*/
}
catch(Exception e)
{
e.printStackTrace();
status = "not";
}
return status;
}

@Override
public String search(String accNo) 
{
 
return null;
}

@Override
public String update(String accNo, String accName, String accType, int balance) 
{
return null;
}

@Override
public String delete(String accNo) 
{
return null;
}

}

======================================================================

package com.amit.service;

public interface AccountServiceDao 
{
public String createAccount(String accNo, String accName, String accType, int balance);
public String searchAccount(String accNo);
public String updateAccount(String accNo, String accName, String accType, int balance);
public String deleteAccount(String accNo);
}

======================================================================
package com.amit.service;

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

import com.amit.doa.AccountDao;

@Service("accservice")
public class AccountServiceDaoImpl implements AccountServiceDao 
{
@Autowired(required=true)
private AccountDao dao;
@Override
public String createAccount(String accNo, String accName, String accType, int balance) 
{
return dao.create(accNo, accName, accType, balance);
}

@Override
public String searchAccount(String accNo) 
{


return null;
}

@Override
public String updateAccount(String accNo, String accName, String accType, int balance) 
{


return null;
}

@Override
public String deleteAccount(String accNo) 
{


return null;
}

}

=====================================================================
package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amit.service.AccountServiceDao;
import com.amit.service.AccountServiceDaoImpl;

public class Test 
{

public static void main(String[] args) 
{
  ApplicationContext context= new ClassPathXmlApplicationContext("ApplicationContext.xml");
  AccountServiceDao service = (AccountServiceDao) context.getBean("accservice");
  String result = service.createAccount("8761001", "Manjhi", "Current", 800000);
      System.out.println(result);
}

}

=====================================================================
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.amit.service"/> <!-- Aee, IOC go to this package and scan it for (component,repository,service,controller) & no need to check for bean config-->
    <context:component-scan base-package="com.amit.dao"/>
    
    <bean id="datasource" class="oracle.jdbc.pool.OracleDataSource">  <!--Driver class Name is not required as it will take automatically -->
    <property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
    <property name="user" value = "scott"></property>
    <property name="Password" value = "tiger"></property>
    </bean>
    
    <!-- <bean id="dao" class ="com.amit.Dao.AccountDaoImpl"> </bean>  -->
    <!-- Not required when we are using Auto-Discovery/streotype ..As ID (dao) is mentioned in AccontDAOImpl @Repository("dao") -->
</beans>

Spring_App_17_BankApp_AutoDiscovery

package com.amit.Dao;

public interface AccountDao
{
public String createAccount(String accNo, String accName, String accType, int balance);
public String searchAccount(String accNo);
public String updateAccount(String accNo, String accName, String accType, int balance);
public String deleteAccount(String accNo);

}
========================================================
package com.amit.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

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

import oracle.jdbc.pool.OracleDataSource;

@Repository("dao")    //-->making it autodiscover     //you can also use @component("dao") 
public class AccountDaoImpl implements AccountDao 
{
@Autowired   //-------> this will inject datasource object into AccountDao (setter/getters not required)
private OracleDataSource datasource;
String status = "";
@Override
public String createAccount(String accNo, String accName, String accType, int balance) 
{
  try 
  {
  Connection con = datasource.getConnection();
  PreparedStatement pst = con.prepareStatement("insert into account values (?,?,?,?)");
 
  pst.setString(1, accNo);
  pst.setString(2, accName);
  pst.setString(3, accType);
  pst.setInt(4, balance);
  pst.executeUpdate();
  status= "success";
  }
  catch(Exception e)
  {
  status = "failure";
  e.printStackTrace();
  }
return status;
}

@Override
public String searchAccount(String accNo) {
return null;
}

@Override
public String updateAccount(String accNo, String accName, String accType, int balance) {
return null;
}

@Override
public String deleteAccount(String accNo) {
return null;
}
public void showDetails()
{
}

}

===============================================================
package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amit.Dao.AccountDaoImpl;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountDaoImpl dao = (AccountDaoImpl)context.getBean("dao");
String status = dao.createAccount("abc123", "Amit", "Saving", 20000);
if(status.equalsIgnoreCase("success"))
{
System.out.println("Account created successfully");
}
else 
{
System.out.println("Oops! something went wrong");
}
 
}

}

==============================================================
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.amit.Dao"/> <!-- Aee, IOC go to this package and scan it for (component,repository,service,controller) & no need to check for bean config-->
    
    <bean id="datasource" class="oracle.jdbc.pool.OracleDataSource">  <!--Driver class Name is not required as it will take automatically -->
    <property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
    <property name="user" value = "scott"></property>
    <property name="Password" value = "tiger"></property>
    </bean>
    
    <!-- <bean id="dao" class ="com.amit.Dao.AccountDaoImpl"> </bean>  -->
    <!-- Not required when we are using Auto-Discovery/streotype ..As ID (dao) is mentioned in AccontDAOImpl @Repository("dao") -->
</beans>

Spring_App_16_BankApp_By_Autowing

package com.amit.Dao;

public interface AccountDao
{
public String createAccount(String accNo, String accName, String accType, int balance);
public String searchAccount(String accNo);
public String updateAccount(String accNo, String accName, String accType, int balance);
public String deleteAccount(String accNo);

}

===================================================================
package com.amit.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

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

import oracle.jdbc.pool.OracleDataSource;

public class AccountDaoImpl implements AccountDao 
{
@Autowired   //-------> this will inject datasource object into AccountDao (to avoid setters and getters for datasource)
private OracleDataSource datasource;
String status = "";
@Override
public String createAccount(String accNo, String accName, String accType, int balance) 
{
  try 
  {
  Connection con = datasource.getConnection();
  PreparedStatement pst = con.prepareStatement("insert into account values (?,?,?,?)");
 
  pst.setString(1, accNo);
  pst.setString(2, accName);
  pst.setString(3, accType);
  pst.setInt(4, balance);
  pst.executeUpdate();
  status= "success";
  }
  catch(Exception e)
  {
  status = "failure";
  e.printStackTrace();
  }
return status;
}

@Override
public String searchAccount(String accNo) {
return null;
}

@Override
public String updateAccount(String accNo, String accName, String accType, int balance) {
return null;
}

@Override
public String deleteAccount(String accNo) {
return null;
}
public void showDetails()
{
}

}

====================================================================
package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amit.Dao.AccountDaoImpl;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountDaoImpl dao = (AccountDaoImpl)context.getBean("dao");
String status = dao.createAccount("abc123", "Amit", "Saving", 20000);
if(status.equalsIgnoreCase("success"))
{
System.out.println("Account created successfully");
}
else 
{
System.out.println("Oops! something went wrong");
}
 
}

}

=======================================================================

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>  <!-- When we use @annotation in bean we have to use <contest:ann..../> -->
    
    <bean id="datasource" class="oracle.jdbc.pool.OracleDataSource">  <!-- this id "datasource" is not used any where you can change it -->
    <property name="URL" value = "jdbc:oracle:thin:@localhost:1521:orcl"></property>
    <property name="user" value = "scott"></property>  <!--Driver class Name is not required as it will take automatically -->
    <property name="Password" value = "tiger"></property>
    </bean>
    
    <bean id="dao" class ="com.amit.Dao.AccountDaoImpl">  <!-- id "dao" is used by IOC container (getbean uses it) -->
    <!-- we don't need to set value of datasource, as we are using autowiring ie. 
    ....<property name="datasource" ref ="datasource"></property> -->
    </bean>
</beans>
===================================================================

Spring_App_15_Autowiring_By_Annotations

package com.amit.Beans;

public class BGV
{
private String fathersName;
private String mothersName;
private String state;
private String distict;
private String postoffice;
private String street;
private String house_No;
private String pincode;
private String phone;

public String getFathersName() {
return fathersName;
}
public void setFathersName(String fathersName) {
this.fathersName = fathersName;
}
public String getMothersName() {
return mothersName;
}
public void setMothersName(String mothersName) {
this.mothersName = mothersName;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getDistict() {
return distict;
}
public void setDistict(String distict) {
this.distict = distict;
}
public String getPostoffice() {
return postoffice;
}
public void setPostoffice(String postoffice) {
this.postoffice = postoffice;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getHouse_No() {
return house_No;
}
public void setHouse_No(String house_No) {
this.house_No = house_No;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}

}
====================================================================
package com.amit.Beans;

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

public class Employee 
{
private String emp_name;
private String emp_code;
private String joining_date;
 
//@Autowired(required = true) --> write here or above setter method of bgv (To inject bgv into Employee)
private BGV bgv;
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public String getEmp_code() {
return emp_code;
}
public void setEmp_code(String emp_code) {
this.emp_code = emp_code;
}
public String getJoining_date() {
return joining_date;
}
public void setJoining_date(String joining_date) {
this.joining_date = joining_date;
}
public BGV getBgv() {
return bgv;
}
@Autowired(required = true)  //--> we can keep it above declaration Line no 11
public void setBgv(BGV bgv) {
this.bgv = bgv;
}

public void getEmpDetails()
{
System.out.println("     Employee Details");
System.out.println("---------------------------------");
System.out.println("Emp Name :"+emp_name);
System.out.println("Emp Code :"+emp_code);
System.out.println("Joining Date     :"+joining_date);
System.out.println();
System.out.println("     Employee BGV Details");
System.out.println("---------------------------------");
System.out.println("Fathers Name :"+bgv.getFathersName());
System.out.println("Mothers Name :"+bgv.getMothersName());
System.out.println("State :"+bgv.getState());
System.out.println("Distict :"+bgv.getDistict());
System.out.println("Post Office :"+bgv.getPostoffice());
System.out.println("Street :"+bgv.getStreet());
System.out.println("House No. :"+bgv.getHouse_No());
System.out.println("Pincode :"+bgv.getPincode());
System.out.println("Phone :"+bgv.getPhone());
}

}

======================================================================
package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amit.Beans.Employee;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("emp");
emp.getEmpDetails();
}

}

======================================================================
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

   
<context:annotation-config/>
<bean id ="emp" class="com.amit.Beans.Employee">
<property name="emp_name" value="A. K. Manjhi"></property>
<property name="emp_code" value="1444"></property>
<property name="joining_date" value="4th Aug"></property>
<!-- <property name="bgv" ref="bgv"></property> -->
</bean>

<bean id = "bgv" class="com.amit.Beans.BGV">
<property name="fathersName" value="G. Manjhi"></property>
<property name="mothersName" value="B. Devi"></property>
<property name="state" value="Jharkhand"></property>
<property name="distict" value="Ranchi"></property>
<property name="postoffice" value="Mahuda"></property>
<property name="street" value="Power House"></property>
<property name="house_No" value="121"></property>
<property name="pincode" value="828305"></property>
<property name="phone" value="7532051119"></property>
</bean>

</beans>        

Spring_App_14_Autowiring_By_Constructor

package beans;

public class Account
{
public String acc_number;
public String ifsc;

public String getAcc_number() {
return acc_number;
}
public void setAcc_number(String acc_number) {
this.acc_number = acc_number;
}
public String getIfsc() {
return ifsc;
}
public void setIfsc(String ifsc) {
this.ifsc = ifsc;
}



}
=====================================================================
package beans;

public class Address 
{
public String city;
public String state;
public String pincode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}

Spring_App_13_Autowiring_By_Type

package beans;

public class Account
{
public String acc_number;
public String ifsc;

public String getAcc_number() {
return acc_number;
}
public void setAcc_number(String acc_number) {
this.acc_number = acc_number;
}
public String getIfsc() {
return ifsc;
}
public void setIfsc(String ifsc) {
this.ifsc = ifsc;
}



}

=====================================================================

package beans;

public class Address 
{
public String city;
public String state;
public String pincode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}

=====================================================================
package beans;

public class Employee 
{
private String name;
private String emp_code;
private String salary;
private Address addr;   
private Account acc;
public String getName() {
return name;
}



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



public String getEmp_code() {
return emp_code;
}



public void setEmp_code(String emp_code) {
this.emp_code = emp_code;
}



public String getSalary() {
return salary;
}



public void setSalary(String salary) {
this.salary = salary;
}



public Address getAddr() {
return addr;
}



public void setAddr(Address addr) {
this.addr = addr;
}



public Account getAcc() {
return acc;
}



public void setAcc(Account acc) {
this.acc = acc;
}



public void display_details()
{
System.out.println("--------BASIC DETAILS------------");
System.out.println("Name : "+name);
System.out.println("emp_code : "+emp_code);
System.out.println("salary : "+salary);
System.out.println("--------ACCOUNT DETAILS------------");
System.out.println("acc_number  : "+acc.getAcc_number());
System.out.println("ifsc        : "+acc.getIfsc());
System.out.println("----------ADDRESS DETAILS----------");
System.out.println("city        : "+addr.getCity());
System.out.println("state       : "+addr.getState());
System.out.println("pincode     : "+addr.getPincode());
}

}

=====================================================================
package Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.Employee;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("emp");
emp.display_details();

}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
<bean id="account" class="beans.Account">      <!-- In byType autowiring ioc look for type LIMITATION: only supports for unique beans in config.  -->
<property name="acc_number" value="8789654410911"></property>
<property name="ifsc" value="ICIC00004839"></property>
</bean>

<bean id= "address" class = "beans.Address">
<property name="city" value ="Noid"></property>
<property name="state" value ="UP"></property>
<property name="pincode" value ="120130"></property>
</bean>

<bean id="emp" class ="beans.Employee" autowire="byType">
<property name="name" value ="Amit"></property>
<property name="emp_code" value ="NSTL-9372"></property>
<property name="salary" value ="3,00,000 p.m"></property>

<!-- 
<property name="addr" ref="address"></property>
<property name="acc" ref ="account"></property>  
-->

</bean>

</beans>

Java 8 Notes Pics