Home  >  Spring Core

ApplicationContext Spring Example

By Arvind Rai, August 15, 2013
ApplicationContext is an interface and belongs to org.springframework.context package. ApplicationContext provides configuration for any spring application. ApplicationContext

1. Access application components
2. Loads resources
3. Publishes events to registered listeners
4. Resolves messages for internationalization (i18)
ApplicationContext can be used like
ApplicationContext context = new ClassPathXmlApplicationContext("spring-app.xml");
 
Find the example below to use ApplicationContext.

SpringDemo.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
    public static void main(String... args) {
    	ApplicationContext context = new ClassPathXmlApplicationContext("spring-app.xml");
    	Entitlement ent=(Entitlement) context.getBean("entitlement");
         System.out.println(ent.getName());
    } 
}
 


spring-app.xml
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <bean id="entitlement" class="com.concretepage.Entitlement">
	    <constructor-arg name="name" value="Entitlement"/>
	    <constructor-arg name="time" value="20"/>	           
    </bean>
</beans>
 


Entitlement.java
package com.concretepage;
public class Entitlement {
	private String name;
	private int time;
	public Entitlement(String name,int time){
		this.name=name;
		this.time=time;
	} 
	public String getName() {
		return name;
	}	
	public int getTime() {
		return time;
	}
}
 


Output
Entitlement
 


pom.xml
<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
      <optional>true</optional>
    </dependency>
    
    <dependency>
	  <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
	  <version>2.1_3</version>
	  <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.5.2-Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>3.6.0.Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>2.0.8</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>3.1.0.RELEASE</version>
	  <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp-all</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-aop</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    	
    <dependency>
	  <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-expression</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-asm</artifactId>
	  <version>3.1.0.RELEASE</version>
	 <scope>compile</scope>
    </dependency>	
    
    <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.17</version>
    </dependency>
    
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
    </dependency>
    
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-agent</artifactId> 
     <version>2.5.6</version> 
     <scope>provided</scope> 
    </dependency> 

   <dependency>
	 <groupId>org.aspectj</groupId>
	 <artifactId>aspectjweaver</artifactId>
	 <version>1.5.4</version>
   </dependency>
  </dependencies>
 
Download Source Code
applicationcontext-spring-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us