Drop Down

Sunday, December 8, 2019

EmployeeServiceConsumer

Consumer Steps:
--------------

1.Get the wsdl file from the Provider.
2.Test the service through SOAP UI.
3.Generate the artifacts.
C:>wsimport -d src -keep -verbose wsdl location
output: artifacts.
  1.Service class.
  2.Interface name
  3.request and response classes
  4.endpoint url.

4.Write webservice client code.

  1.create service class object
  2.call the port and it returns interface
  3.prepare the webservice request.
  4.call the getOrder API.
  5.call the getPrice API.



Step to develop client in eclipse.
1.Create java project.

2. create lib folder. copy all the jars(jaxws) in to lib folder.

3.generate artifacts using wsdl.
Goto Command prompt execute fillowing commnad

  E:\WorkSpace\webservice\JaxWsConsumer>wsimport -d src -keep -verbose http://localhost:2020/JaxWsOrderServiceProvider/online?wsdl

4. Write client application  code in main method based class.
   Steps to write logic in client class.

    1.create service class object
2.call the port and it returns interface
3.prepare the webservice request.
4.call the getOrder API.
5.call the getPrice API.




Sample example for SAOP using Apache CXF and Spring Boot.

cxf spring boot dependency
    cxf-spring-boot-starter-jars
application.properties
  cxf.jars.component-scan=true
  server.context-path=/helloapp

Steps:
1.Create maven project
2.Create End point class(service)
3.Create configuation class.
4.Test the application

======================================================================
================EmployeeServiceConsumer=======================
======================================================================

*) Parse wsdl to generate stub (artifacts) using wsimport



package test.Client;

import java.util.List;

import com.rameshit.employee.Employee;
import com.rameshit.employee.EmployeeService;
import com.rameshit.employee.EmployeeServiceImplService;

public class EmpServiceConsumer_Test
{

public static void main(String[] args)
{
//1.create service class object
EmployeeServiceImplService employeeServiceImplService = new EmployeeServiceImplService();

//2.call the port and it returns interface
EmployeeService employeeService =  employeeServiceImplService.getEmployeeServiceImplPort();

//3.prepare the webservice request. (Bean ka object bana ke access karo...)
Employee emp = new Employee();
/*emp.setCompany("Apple");
emp.setEmpid(0071);
emp.setFname("Amit Kumar");
emp.setLname("Manjhi");
emp.setLocation("Boston");
emp.setSalary(999999); */
//4.call the getOrder API.
//5.call the getPrice API.

emp = employeeService.getEmployeesInfo(505);
System.out.println("Company Name: "+emp.getCompany());
System.out.println("Employe Id: "+emp.getEmpid());
System.out.println("Employe First Name: "+emp.getFname());
System.out.println("Employe Last Name: "+emp.getLname());
System.out.println("Employe Location: "+emp.getLocation());
System.out.println("Employe Salary: "+emp.getSalary());

//---------
System.out.println("******************************************************");
List<Employee> employeeList = employeeService.getAllEmployee();
for(Employee emp_n : employeeList)
{
System.out.println("Company Name: "+emp_n.getCompany());
System.out.println("Employe Id: "+emp_n.getEmpid());
System.out.println("Employe First Name: "+emp_n.getFname());
System.out.println("Employe Last Name: "+emp_n.getLname());
System.out.println("Employe Location: "+emp_n.getLocation());
System.out.println("Employe Salary: "+emp_n.getSalary());
System.out.println();
System.out.println("**********"+emp_n.getFname()+"***************");
}

}

}



output:
Company Name: IBM
Employe Id: 555
Employe First Name: Amit
Employe Last Name: Kumar
Employe Location: Noida
Employe Salary: 98789
******************************************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999

*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999

*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999

*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999

*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999

*************************Amit*****************************



=====================wsdl===============


















EmployeServiceProvider

package com.rameshit.employee;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface EmployeeService
{
@WebMethod
Employee getEmployeesInfo(Integer eid);
//List<Employee> getAllEmployee();
}
=================================

package com.rameshit.employee;

public class Employee 
{
private int empid;
private String fname;
private String lname;
private long salary;
private String company;
private String location;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
=====================================
package com.rameshit.employee;

import javax.jws.WebService;

@WebService(endpointInterface ="com.rameshit.employee.EmployeeService")
public class EmployeeServiceImpl implements EmployeeService 
{

@Override
public Employee getEmployeesInfo(Integer eid) 
{
// DB level code ie. DAO, get values from db.
Employee emp = new Employee();
if(eid!= null && eid > 0)
{
emp.setCompany("IBM");
emp.setEmpid(555);
emp.setFname("Amit");
emp.setLname("Kumar");
emp.setLocation("Noida");
emp.setSalary((long) 98789.89);
}
return emp;
}

}
=======================================
<?xml version="1.0" encoding="UTF-8"?>

<!--
 The contents of this file are subject to the terms
 of the Common Development and Distribution License
 (the "License").  You may not use this file except
 in compliance with the License.
 You can obtain a copy of the license at
 https://jwsdp.dev.java.net/CDDLv1.0.html
 See the License for the specific language governing
 permissions and limitations under the License.
 When distributing Covered Code, include this CDDL
 HEADER in each file and include the License file at
 https://jwsdp.dev.java.net/CDDLv1.0.html  If applicable,
 add the following below this CDDL HEADER, with the
 fields enclosed by brackets "[]" replaced with your
 own identifying information: Portions Copyright [yyyy]
 [name of copyright owner]
-->

<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
    <endpoint
        name='EmployeeService'
        implementation='com.rameshit.employee.EmployeeServiceImpl'
        url-pattern='/employee'/>
</endpoints>
=================================================

<?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>EmployeeServiceProvider</display-name>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>JAXWSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAXWSServlet</servlet-name>
<url-pattern>/employee</url-pattern>
</servlet-mapping>
</web-app>
===============================================
===================================================================
****************TO GENERATE ARTIFACTS USING JAXWS**********************
--------------------------------------------------------------------------------------------------------------------
D:\JavaWorkSpace\webservices2\EmployeeServiceProvider>wsgen -d src -cp build/classes -keep -verbose com.rameshit.employee.EmployeeServiceImpl



=========================
* add jars and run it.   (http://localhost:8080/EmployeeServiceProvider/employee)
* output



=====
JARS
=====


===========PUBLISH WEBSERVICE===========


package test_webPublisher;

import javax.xml.ws.Endpoint;

import com.rameshit.employee.EmployeeServiceImpl;

public class SoapPublisher {

public static void main(String[] args)
{
  Endpoint.publish("http://localhost:8888/EmployeeServiceProvider/employee", new EmployeeServiceImpl());
  //System.out.println("SuccessFully Published. ");
  //System.out.println("RUNNING ON :   http://localhost:8888/EmployeeServiceProvider/employee");
  //System.out.println("Use: http://localhost:8888/EmployeeServiceProvider/employee?wsdl");
}

}











pics_Soap

Tuesday, October 8, 2019

Rough Notes for Maven

java 8 compiler
Tomcat Plugin
Servlet API
Driver (jdbc)
=========================================================

javax servlet dependency maven
---------------------------------------
https://mvnrepository.com/artifact/javax.servlet/servlet-api/3.0-alpha-1


maven tomcat plugin
--------------------------
http://tomcat.apache.org/maven-plugin-2.2/




java 8 plugin maven
------------------------
https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html


mysql8 dependency maven
-----------------------------------
https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11

Sunday, October 6, 2019

Send Free SMS using Java code

package freesms;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class SendSMS
{

public static void main(String[] args)
{
try {
// Construct data
String apiKey = "apikey=" + "2D5i+ah7Dtw-6Ggu4kukZUJrnHddMJLXjNvFGstF3A"; // only 10 credits left
String message = "&message=" + "This is your message";
String sender = "&sender=" + "TXTLCL"; //not to be changed
String numbers = "&numbers=" + "8744973456";

// Send data
HttpURLConnection conn = (HttpURLConnection) new URL("https://api.textlocal.in/send/?").openConnection();
String data = apiKey + numbers + message + sender;
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", Integer.toString(data.length()));
conn.getOutputStream().write(data.getBytes("UTF-8"));
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
stringBuffer.append(line);
System.out.println(">>> "+stringBuffer);

}
rd.close();

//return stringBuffer.toString();

  }
catch (Exception e)
{
System.out.println("Error SMS " + e);
//return "Error " + e;
}

}

}
==================================================
---------
 NOTE
---------
Website :  https://control.textlocal.in/settings/apikeys/
id: amitin2015@gmail.com
P/W: S****g***19
===================================================

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>

Java 8 Notes Pics