Spring4 Basic Prototype Scope Project

Click here to download eclipse supported ZIP file



This is pommmm.html JSP file and it is used display the output for the application.




<div style='background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;'><table><tr><td><pre style='margin: 0; line-height: 125%'> </pre></td><td><pre style='margin: 0; line-height: 125%'><span style='color: #000080; font-weight: bold'>&lt;project</span> <span style='color: #FF0000'>xmlns=</span><span style='color: #0000FF'>&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style='color: #FF0000'>xmlns:xsi=</span><span style='color: #0000FF'>&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <br><span style='color: #FF0000'>xsi:schemaLocation=</span><span style='color: #0000FF'>&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span><span style='color: #000080; font-weight: bold'>&gt;</span>.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<span style='color: #008080; font-weight: bold'>&lt;modelVersion&gt;</span>4.0.0<span style='color: #008080; font-weight: bold'>&lt;/modelVersion&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>com.cv.spring.scope<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>Spring4BasicPrototypeScope<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<name>Spring4BasicPrototypeScope</name>
<span style='color: #008080; font-weight: bold'>&lt;packaging&gt;</span>jar<span style='color: #008080; font-weight: bold'>&lt;/packaging&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>1.0<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;properties&gt;</span>
<java-version>1.8</java-version>
<span style='color: #008080; font-weight: bold'>&lt;log4j-version&gt;</span>1.2.16<span style='color: #008080; font-weight: bold'>&lt;/log4j-version&gt;</span>
<org.springframework-version>4.2.0.RELEASE</org.springframework-version>
<jackson.version>2.5.3</jackson.version>
<span style='color: #008080; font-weight: bold'>&lt;/properties&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependencies&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>org.springframework<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>spring-context<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>${org.springframework-version}<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented 
programming implementation allowing you to define -->
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>org.springframework<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>spring-aop<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>${org.springframework-version}<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<!-- The spring-webmvc module (also known as the Web-Servlet module) contains 
Spring’s model-view-controller (MVC) and REST Web Services implementation 
for web applications -->
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>org.springframework<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>spring-webmvc<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>${org.springframework-version}<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<!-- The spring-web module provides basic web-oriented integration features 
such as multipart file upload functionality and the initialization of the 
IoC container using Servlet listeners and a web-oriented application context -->
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>org.springframework<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>spring-web<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>${org.springframework-version}<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>org.springframework<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>spring-tx<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>${org.springframework-version}<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>com.fasterxml.jackson.core<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>jackson-databind<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>${jackson.version}<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>taglibs<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>standard<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>1.1.2<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>javax.servlet<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>javax.servlet-api<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>3.1.0<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>javax.servlet<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>jstl<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>1.2<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>log4j<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>log4j<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>${log4j.version}<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>javax.inject<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>javax.inject<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>1<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependency&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/dependencies&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;build&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;pluginManagement&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;plugins&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;plugin&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;groupId&gt;</span>org.apache.maven.plugins<span style='color: #008080; font-weight: bold'>&lt;/groupId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;artifactId&gt;</span>maven-compiler-plugin<span style='color: #008080; font-weight: bold'>&lt;/artifactId&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;version&gt;</span>3.1<span style='color: #008080; font-weight: bold'>&lt;/version&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;configuration&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;source&gt;</span>1.8<span style='color: #008080; font-weight: bold'>&lt;/source&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;target&gt;</span>1.8<span style='color: #008080; font-weight: bold'>&lt;/target&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/configuration&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/plugin&gt;</span>

<span style='color: #008080; font-weight: bold'>&lt;/plugins&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/pluginManagement&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/build&gt;</span>
<span style='color: #008080; font-weight: bold'>&lt;/project&gt;</span>
<br><br>


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



	
<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.scope</groupId> <artifactId>Spring4BasicPrototypeScope</artifactId> Spring4BasicPrototypeScope <packaging>jar</packaging> <version>1.0</version> <properties> 1.8 <log4j-version>1.2.16</log4j-version> 4.2.0.RELEASE 2.5.3 </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>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> </dependencies> <build> <pluginManagement> <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> </pluginManagement> </build> </project>


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



 

    
package com.cv.spring.scope;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 @author Chandra Vardhan
 */
@Component
@Scope(value = "prototype")
public class Knife {
}


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



 

    
package com.cv.spring.scope;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Provider;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 @author Chandra Vardhan
 */
@Component(value = "jugglername")
public class KnifeJuggler {
 
  private final static Logger LOGGER = Logger.getLogger(KnifeJuggler.class);
  
    @Autowired
    Provider<Knife> knifeProvider;
 
    public void juggle() {
      LOGGER.info("Control entered into KnifeJuggler.juggle() method!!!");
      
        List<Knife> list = new ArrayList<Knife>();
 
 
        for (int i = 0; i < 5; i++) {
            //create 5 prototype scoped instances
            list.add(createKnifeInstance());
        }
 
        //print each instance
        for (Knife knife : list) {
          LOGGER.info(knife);
        }
        LOGGER.info("Control exited KnifeJuggler.juggle() method!!!");
    }
 
    private Knife createKnifeInstance() {
        return knifeProvider.get();
    }
}


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



 

    
package com.cv.spring.scope;

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

/**
 @author Chandra Vardhan
 */
public class MainProgram {
  
  private final static Logger LOGGER = Logger.getLogger(MainProgram.class);
 
    public static void main(String[] args) {
      LOGGER.info("Control entered into MainProgram.main(-) method!!!");
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        KnifeJuggler juggler = ProviderUtil.getBeanByType(ctx, KnifeJuggler.class);
        juggler.juggle();
        LOGGER.info("Control exited MainProgram.main(-) method!!!");
    }
}


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



 

    
package com.cv.spring.scope;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

/**
 @author Chandra Vardhan
 */
@Component
public class ProviderUtil {

  private final static Logger LOGGER = Logger.getLogger(ProviderUtil.class);
  @Autowired
  private static ApplicationContext ctx;

  public static <T> T getBeanByType(final Class<T> claz)
      throws UnsupportedOperationException, BeansException {
    LOGGER.info("Control entered into ProviderUtil.getBeanByType(-) method!!!");
    Map beansOfType = ctx.getBeansOfType(claz);
    final int size = beansOfType.size();
    LOGGER.info("Control exited ProviderUtil.getBeanByType(-) method!!!");
    switch (size) {
    case 0:
      throw new UnsupportedOperationException("No bean found of type"
          + claz);
    case 1:
      String name = (StringbeansOfType.keySet().iterator().next();
      return claz.cast(ctx.getBean(name, claz));
    default:
      throw new UnsupportedOperationException(
          "Ambigious beans found of type" + claz);
    }
  }

  public static KnifeJuggler getBeanByType(
      ClassPathXmlApplicationContext ctx2, Class<KnifeJuggler> class1) {
    ctx = ctx2;
    return getBeanByType(class1);
  }
}


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.scope</groupId> <artifactId>Spring4BasicPrototypeScope</artifactId> <name>Spring4BasicPrototypeScope</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.2.0.RELEASE</org.springframework-version> <jackson.version>2.5.3</jackson.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>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> </dependencies> <build> <pluginManagement> <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> </pluginManagement> </build> </project>


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



 
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<annotation-driven />
<context:component-scan base-package="com.cv.spring.scope" />
</beans: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