Sunday, October 28, 2012

Sample Java Application using Spring Framework with xmlns:p



Related article:

Java HelloWorld sample with decoupled

Simple Java app using Spring Framework

Sample Java Application using Spring Framework with xmlns:p="http://www.springframework.org/schema/p"

Environment:
Jdk1.7.07
Maven 3.0.4
Spring3.1.1
Spring Tools Suits(STS) 3.0
Download Source code here
To open the source code:
File --> Import --> Maven --> Existing Maven Projects
If you are new to Maven, please visit  Maven Tutorial.




package org.sample.app;

import org.sample.message.MessageRenderer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class HelloWorldSpringDI {

 public static void main(String[] args) {

  // Initialize Spring application context
  ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");
  
  MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
  mr.render();
  
 }
}
package org.sample.message;

public interface MessageProvider {

 public String getMessage();
 
}

package org.sample.message;

public interface MessageRenderer {

 public void render();
 
 public void setMessageProvider(MessageProvider provider);
 
 public MessageProvider getMessageProvider();
 
}

package org.sample.message.impl;

import org.sample.message.MessageProvider;

public class HelloWorldMessageProvider implements MessageProvider {

 public String getMessage() {
  
  return "Hello World!";
 }
 
}

package org.sample.message.impl;

import org.sample.message.MessageProvider;
import org.sample.message.MessageRenderer;

public class StandardOutMessageRenderer implements MessageRenderer {

    private MessageProvider messageProvider = null;
 
 public void render() {
        if (messageProvider == null) {
            throw new RuntimeException(
                    "You must set the property messageProvider of class:"
                            + StandardOutMessageRenderer.class.getName());
        }

        System.out.println(messageProvider.getMessage()); 
 }

 public void setMessageProvider(MessageProvider provider) {
        this.messageProvider = provider;  
 }

 public MessageProvider getMessageProvider() {
        return this.messageProvider;
 }
 
}

app-context.xml


 

 



log4j.xml


 
 
  
  
   
  
 
 
 



 
 



 



 








 
 
  
  
 
 


pom.xml

 4.0.0
 org.springframework.samples.spring
 app2010
 1.0.0.CI-SNAPSHOT
 jar
 Spring Utility
 http://www.springframework.org
 
  <![CDATA[
      This project is a minimal jar utility with Spring configuration.
    ]]>
 
 
  true
  3.1.0.RELEASE
 
 
  
   strict
   
    false
   
  
 

 
  
   junit
   junit
   4.7
   test
  
  
   org.springframework
   spring-test
   ${spring.framework.version}
   test
  
  
   org.springframework
   spring-context
   ${spring.framework.version}
  
  
   log4j
   log4j
   1.2.14
  
 

 
  
   
    
     maven-assembly-plugin
     false
     
      
       project
      
     
    
    
     org.apache.maven.plugins
     maven-jar-plugin
     
      
       target/classes/META-INF/MANIFEST.MF
      
     
    
   
  
  
   
    org.apache.maven.plugins
    maven-compiler-plugin
    
     1.7
     1.7
    
   
   
    org.apache.maven.plugins
    maven-surefire-plugin
    
     
     
      **/*Tests.java
     
     
      **/Abstract*.java
     
    
   
  
  
 

 
  
   Codehaus
   http://repository.codehaus.org/
   
    false
   
  
 
 
  http://www.springframework.org/download
  
   staging
   file:///${user.dir}/target/staging/org.springframework.batch.archetype/${pom.artifactId}
  
  
   spring-release
   Spring Release Repository
   file:///${user.dir}/target/staging/release
  
  
   spring-snapshot
   Spring Snapshot Repository
   file:///${user.dir}/target/staging/snapshot
  
 


No comments:

Post a Comment