Spring 4 Constructor And Setter Injection

spring+4+constructor+and+setter+injection

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring;
/**
 @author Chandra Vardhan
 */
//Demo.java(POJI)
interface Demo {
  String wishMsg(String uname);
}

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


 

    
package com.cv.spring;

import java.util.Calendar;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 */
public class DemoBean implements Demo {

  static final Logger LOGGER = Logger.getLogger(DemoBean.class);

  private String msg;

  public DemoBean() {
    System.out.println("DemoBean:0-arg constructor");
    System.out
        .println("DemoBean obj is created  b4 setter injection is done..");
  }

  public DemoBean(String msg) {
    System.out.println("DemoBean:1-arg constructor");
    System.out
        .println("DemoBean obj is created  constructor injection is done..");
    this.msg = msg;
    System.out
        .println("DemoBean obj is created  constructor injection is done..and its value is===="
            + msg);
  }

  public void setMsg(String msg) {
    this.msg = msg;

    System.out.println("DemoBean:setMsg(-)");
    System.out.println("DemoBean:setMsg(-)====val of msg is=====" + msg);

  }

  /*
   * public String getMsg() { System.out.println("DemoBean:getMsg()"); return
   * msg; //System.out.println("DemoBean:getMsg()"); we wont write any stmt
   * after return stmt
   
   * }
   */
  public String wishMsg(String uname) {
    Calendar cl = Calendar.getInstance();
    System.out.println("Calendar class obj is==========" + cl.getClass());

    int h = cl.get(Calendar.HOUR_OF_DAY);
    if (h < 12)
      return msg + "---->good mrng      " + uname;
    else if (h <= 16)
      return msg + "---->good aftrnn   " + uname;
    else
      return msg + "---->good n8   " + uname;
  }

}

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


 

    
package com.cv.spring;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.xml.XmlBeanFactory;//(c)
import org.springframework.core.io.FileSystemResource;//(c)

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

  static final Logger LOGGER = Logger.getLogger(DemoClient.class);

  public static void main(String[] args) {

    FileSystemResource fsr = new FileSystemResource(
        "src/main/resources/DemoCfg.xml");

    LOGGER.info("FileSystemResource class obj is loaded cfg file : : "
        + fsr.getClass());// in first line we r getting this o/p

    XmlBeanFactory factory = new XmlBeanFactory(fsr);

    LOGGER.info("XmlBeanFactory class obj is loaded fsr obj======"
        + factory.getClass());
    Object o1 = factory.getBean("db");

    DemoBean beanobjref = (DemoBeano1;// Demo beanobjref=(Demo)o1; both
                      // one at the same
    LOGGER.info(beanobjref.wishMsg("chote"));

  }
}



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">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.annotation</groupId> <artifactId>Spring4ConstructorAndSetterInjection</artifactId> <name>Spring4ConstructorAndSetterInjection</name> <packaging>jar</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.1.6.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>


This is DemoCfg.xml spring configuration file and these entries are used in the application.


 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="db" class="com.cv.spring.DemoBean">
<property name="msg"><value>hello</value></property>
<constructor-arg>
<value>namashte</value>
</constructor-arg>
</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