版权声明: https://blog.csdn.net/qq_38386316/article/details/88779500
1. EventHanding in Spring
- Circle.java
package org.zcs.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
@Component
public class Circle implements Shape, ApplicationEventPublisherAware {
@Autowired
private MessageSource messageSource;
public MessageSource getMessageSource() {
return messageSource;
}
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
private Point center;
private ApplicationEventPublisher publisher;
public Point getCenter() {
return center;
}
@Resource
public void setCenter(Point center) {
this.center = center;
}
@PostConstruct
public void initlize() {
System.out.println("initlizing a circle ");
}
@PreDestroy
public void destroy() {
System.out.println("destroy a circle");
}
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Drawing Circle:");
System.out.println(messageSource.getMessage("drawing.circle", null, "Defalut Drawing circle", null));
System.out.println(messageSource.getMessage("drawing.point", new Object[] {center.getX(), center.getY()}, "Defalut Drawing point", null));
DrawEvent drawEvent = new DrawEvent(this);
publisher.publishEvent(drawEvent);
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
// TODO Auto-generated method stub
this.publisher = publisher;
}
}
- DrawEvent.java
package org.zcs.spring;
import org.springframework.context.ApplicationEvent;
public class DrawEvent extends ApplicationEvent {
public DrawEvent(Object source) {
super(source);
// TODO Auto-generated constructor stub
}
public String toString() {
return "Draw Event occured";
}
}
- MyEventListener.java
package org.zcs.spring;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyEventListener implements ApplicationListener{
@Override
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
System.out.println(event.toString());
}
}
- DrawingApp.java
package org.zcs.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
//BeanFactory factory = new FileSystemXmlApplicationContext("src/spring.xml");
//Triangle triangle = new Triangle();
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Shape circle = (Shape)context.getBean("circle");
circle.draw();
System.out.println(context.getMessage("greeting", null, "Default Greeting", null));
}
}
2.学习记录。
- 学习了spring中的监听操作。学习了一遍,有点儿懵逼哇。
- 先记录着,再看看…