Results 1 to 4 of 4

Thread: How To Application Context

  1. #1

    Default How To Application Context

    Hi,

    I looked everywhere for a simple example of use of the AppContext, but i could find any... Did I miss it ? or Can you just show a sample of code that describe how you get back your bean with appContext ?

    Thanks++

  2. #2
    Join Date
    Oct 2004
    Location
    Not Redmond
    Posts
    26

    Default

    Hiya

    How about this, that demonstrates both Dependency Lookup (DL) and Dependency Injection (DI)...

    In beans.xml

    Code:
    <?xml version="1.0" ?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    
    	<bean id="bingoService" class="foo.DefaultBingoService">
    	    <constructor-arg index="0" ref="bingoRegistry"/>
    	</bean>
    	
    	<bean id="bingoRegistry" class="foo.DefaultBingoRegistry"/>
    </beans>
    In Main.java

    Code:
    package foo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public final class Main &#123;
    
    	public static void main&#40;final String&#91;&#93; args&#41; &#123;
    		ApplicationContext ctx 
    			= new FileSystemXmlApplicationContext&#40;"beans.xml"&#41;;
    		IBingoService service = &#40;IBingoService&#41; ctx.getBean&#40;"bingoService"&#41;;
    		service.registerPlayer&#40;".NET Guy"&#41;;
    	&#125;
    &#125;
    In IBingoService.java

    Code:
    package foo;
    
    public interface IBingoService &#123;
    	void registerPlayer&#40;final String id&#41;;
    &#125;
    In DefaultBingoService.java

    Code:
    package foo;
    
    public final class DefaultBingoService implements IBingoService &#123;
    	
    	private final IBingoRegistry _registry;
    	
    	public DefaultBingoService&#40;final IBingoRegistry registry&#41; &#123;
    		_registry = registry;
    	&#125;
    
    	public void registerPlayer&#40;final String id&#41; &#123;
    		// add the player identified by the supplied id to the registry...
    		_registry.addPlayer&#40;id&#41;;
    	&#125;
    &#125;
    In IBingoRegistry.java

    Code:
    package foo;
    
    public interface IBingoRegistry &#123;
    	void addPlayer&#40;final String id&#41;;
    &#125;

    In DefaultBingoRegistry.java

    Code:
    package foo;
    
    public final class DefaultBingoRegistry implements IBingoRegistry &#123;
    
    	public void addPlayer&#40;final String id&#41; &#123;
    		System.out.println&#40;"Adding player '" + id + "'."&#41;;
    	&#125;
    &#125;
    Thats about as simple an application as one can get. It doesn't do anything other than demonstrate DL and DI, but there ya go. The IBingoRegistry dependency expressed by the DefaultBingoService class is injected into the 'bingoService' bean by the container. Note that the IBingoService interface itself has no such dependency on the IBingoRegistry class... the dependency is purely an implementation detail. The user (the Main.java program in this case) then uses the ApplicationContext as a glorified ServiceLocator to get an implementation of the IBingoService service bean.

    Is that a simple enough example... or did you really want something a little more hardcore (i.e. something that doesn't use toy classes)?

    Ciao
    .NET Guy

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="dateFormatSql" class="java.text.SimpleDateFormat">
    	   <constructor-arg><value>yyyy-MM-dd</value></constructor-arg>
    	</bean> 
    </beans>
    Code:
    package dateformat;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test &#123;
        public static void main&#40;String&#91;&#93; args&#41; &#123;
            ApplicationContext ctx = new ClassPathXmlApplicationContext&#40;"/dateformat/applicationContext.xml"&#41;;
            //XmlBeanFactory ctx = new XmlBeanFactory&#40;new ClassPathResource&#40;"applicationContext.xml", Test.class&#41;&#41;;
            SimpleDateFormat sdf = &#40;SimpleDateFormat&#41; ctx.getBean&#40;"dateFormatSql"&#41;;
            String formattedDate = sdf.format&#40;new Date&#40;&#41;&#41;;
            System.out.println&#40;"date='" + formattedDate + "'"&#41;;
        &#125;
    &#125;

  4. #4

    Default

    Thanks !

    Your example is just perfect, i'll adapt it to my own and try the other features ( message, event, multiple context) from it...

Similar Threads

  1. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  2. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  3. Replies: 6
    Last Post: May 8th, 2005, 11:09 AM
  4. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM
  5. Hierarchical application context in J2EE app
    By cmgharris in forum Container
    Replies: 2
    Last Post: Sep 13th, 2004, 04:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •