Announcement Announcement Module
Collapse
No announcement yet.
How To Application Context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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;

      Comment


      • #4
        Thanks !

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

        Comment

        Working...
        X