Posts

Showing posts from July, 2019

Print random message or string from an array in java

Print random message or string from an array in java --------------------------------------------------------------------------------- import java.util.Random; public class RandomFortuneService  { public static void main(String[] args) { // create an array of strings String[] data =  {  "Beware of the wolf in sheep's clothing", "Diligence is the mother of good luck", "The journey is the reward" }; // create a random number generator  Random myRandom = new Random(); System.out.println(myRandom); int index = myRandom.nextInt(data.length); System.out.println(index); String theFortune = data[index]; System.out.println(theFortune); } } #generate random number #java random number generator #print random values from an array

E-Mail notification in java ee

package com.service; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.db.Users; public class ResetPassword { public static void sendPassword(Users u) { // TODO Auto-generated method stub String to=u.getEmail(); String subject="Password Sent"; String message="Hello Dear , Your Password is : "+u.getPassword(); String from="lit.bishnu.java@gmail.com"; // give ur mailid String password="litbishnu"; // give ur passsword try { //Authentication with Gmail server Properties props=new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465...

Dependency Injection In Spring using constructor

* Dependency Injection refers to remove the dependency and make the code loosely coupled so that it is easy to test the code. In such a case, we provide the information from the external source such as XML file . It makes our code loosely coupled and easier for testing . In such cases, we write the code as: Two types of dependency injection are there. 1.Constructor Injection     2.Setter Injection <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Constructor Injection ( primitive type injection ) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This is the base class( abc.java ) ---------------------------...

Custome log in spring 5.1 onwords

Add a new class to your package myLoggerConfig.java ------------------------------------------------------------------------------ package com.mgt.springdemo; import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MyLoggerConfig { private String rootLoggerLevel; private String printedLoggerLevel; public void setRootLoggerLevel(String rootLoggerLevel) { this.rootLoggerLevel = rootLoggerLevel; } public void setPrintedLoggerLevel(String printedLoggerLevel) { this.printedLoggerLevel = printedLoggerLevel; } public void initLogger() { // parse levels Level rootLevel = Level.parse(rootLoggerLevel); Level printedLevel = Level.parse(printedLoggerLevel); // get logger for app context Logger applicationContextLogger = Logger.getLogger(AnnotationConfigAppli...

Spring container and sequential steps

package com.mgt.springdemo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringApp { public static void main(String[] args) { // load the spring configuration file ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // retrieve bean from spring container Book b= context.getBean("myBook", Book.class); // call methods on the bean System.out.println(b.getBookDetail()); // close the context context.close(); } }

ApplicationContext file in Spring

create a file named applicationContext.xml ------------------------------------------------------------------------- P u t th e f oll owing basic containts inside ==================================== <?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">     <!-- Define your beans here -->      </beans> =================================================================== Example ==============================================================...