Java/Spring/ApplicationContext

Материал из Java эксперт
Перейти к: навигация, поиск

ApplicationContext Aware

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="contextAware" class="ContextAwareDemo"/>

</beans>

File: Main.java
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "context.xml");
    ContextAwareDemo demo = (ContextAwareDemo) ctx.getBean("contextAware");
    demo.displayAppContext();
  }
}
class ContextAwareDemo implements ApplicationContextAware {
  private ApplicationContext ctx;
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    ctx = applicationContext;
  }
  public static void main(String[] args) {
  }
  public void displayAppContext() {
    System.out.println(ctx);
  }
}





ContextApplication Event

File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="publisher" class="EventPublisherDemo"/>
    <bean class="MessageEventListener"/>
</beans>

File: Main.java
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    EventPublisherDemo pub = (EventPublisherDemo) ctx.getBean("publisher");
    pub.report("A");
    pub.report("B");
  }
}
class EventPublisherDemo implements ApplicationContextAware {
  private ApplicationContext ctx;
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.ctx = applicationContext;
  }
  public void report(String message) {
    ctx.publishEvent(new MessageEvent(this, message));
  }
}
class MessageEvent extends ApplicationEvent {
  private String message;
  public MessageEvent(Object source, String message) {
    super(source);
    this.message = message;
  }
  public String getMessage() {
    return message;
  }
}
class MessageEventListener implements ApplicationListener {
  public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof MessageEvent) {
      MessageEvent messageEvent = (MessageEvent) event;
      System.out.println("Received " + messageEvent.getMessage());
    }
  }
}





Create ApplicationContext with ClassPathXmlApplicationContext

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="errorReporter" class="ErrorReporter"/>
</beans>

File: Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "context.xml");
    ErrorReporter reporter = (ErrorReporter) ctx.getBean("errorReporter");
    reporter.evaluate(1);
  }
}
class ErrorReporter {
  public void evaluate(int value) {
      System.out.println(value);
  }
}





ResourceLoader Demo

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

File: Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
    Resource res1 = ctx.getResource("file:///tmp");
    System.out.println(res1.getClass());
    Resource res2 = ctx.getResource("classpath:com/yourName/Complex.class");
    System.out.println(res2.getURL());
    Resource res3 = ctx.getResource("http://www.google.ru");
    System.out.println(res3.getFile());
  }
}