Spring Hello world Project

Click here to download eclipse supported ZIP file



This is MainProgram.java file having the source code to execute business logic.


 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!

 @author Chandra Vardhan
 *
 *
 */
public class MainProgram {  

  private static final Logger LOGGER = Logger.getLogger(MainProgram.class);
  
  public static void main(String[] args) {
    
    LOGGER.info(
        "LOGGER : Entered into main(String[]) of MainProgram class... ");
    
    LOGGER.info(
        "LOGGER : Reading spring-config.xml file... ");
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    
    LOGGER.info(
        "LOGGER : getting the User bean... ");
    User obj = (Usercontext.getBean("user");
    LOGGER.info("LOGGER : Name of the user is : :  "+obj.getNameAndMessage());
  }
}


This is User.java file having the source code to execute business logic.


 

    
package com.cv.spring.core;

import org.apache.log4j.Logger;

/**
 * /** Hello world!
 
 @author Chandra Vardhan
 *
 *
 */
public class User {

  private static final Logger LOGGER = Logger.getLogger(User.class);

  private String name;

  private String message;

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

  public String getName() {
    return name;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public String getNameAndMessage() {
    LOGGER.info("IN User.getNameAndMessage()");
    LOGGER.info("Name is : : " + name + " and message is : : " + message);
    LOGGER.info("OUT User.getNameAndMessage()");
    return name + " welcome to " + message;
  }

}




This is pom.xml file having the entries of dependency jars and information to build the application .


	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.core</groupId> <artifactId>SpringHelloworldProject</artifactId> <version>1.0</version> <packaging>jar</packaging> <properties> <log4j-version>1.2.16</log4j-version> <java.version>1.8</java.version> <spring.version>4.2.0.RELEASE</spring.version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </project>


This is spring-config.xml file having the spring configuration properties.


 
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="user" class="com.cv.spring.core.User">
<property name="name" value="CHANDRA VARDHAN" />
<property name="message" value="Hello world to Spring!!!" />
</bean>

</beans>



This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


No comments:

Post a Comment