您的位置:首页 > 编程语言 > Java开发

Spring 初探(五)(Spring Bean 自动装配与自定义监听事件)

2016-12-21 17:24 381 查看
现在通行的使用bean的方法是不通过xml进行配置,而仅仅采用java

内置的类指明bean及相应的依赖关系,下面展开叙述。

@Configuration可以声明某个类是作为配置类,在这样的类中

通过@Bean进行修饰的方法可以返回相应的bean类

通过由AnnotationConfigApplicationContext初始化的Application的getBean

方法(以类名.class为参数)即可得到相应bean实例。

(其找到相应bean类的方式可以相应地理解为byType)

可以在最后实现文件中加载多个configure类,相应参照网站(register refresh)

同样地可以对bean实现依赖注入的功能。(即bean间的依赖)

这可以通过构造函数的方式进行,@Bean函数的构造返回与类的初始化方式相一致。

使用@Import(ConfigClass.class)对具有@Configuration的配置类进行修饰

可以使得在该配置类中使用ConfigureClass中定义的bean作为依赖。

在xml bean中可以规定的init-method及destory-method在@Bean的配置形式中有相应的

版本,即@Bean(initMethod = "*", destoryMethod = "*")

类似地有scope的相应操作,如在@Bean下加修饰@Scope("prototype")

可以将每一个返回新的bean实体。

Spring中的事件处理基本上类似于signal的意义(熟悉scrapy或者一些基于事件驱动

进行传递信息框架的人很熟悉)

如:

监听 ContextStartedEvent -> implements ApplicationListener<ContextStartedEvent>

-> Override onApplicationEvent(ContextStartedEvent event)

相应地有对ContextStoppedEvent的处理。

下面看一个实现了利用@Bean进行自动配置及进行事件处理的例子。

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Value;

/**
* Created by admin on 2016/12/21.
*/
public class HelloWorld {
@Value("Hello World!")
private String message;

public  void setMessage(String message)
{
this.message = message;
}

public void getMessage(){
System.out.println("Your mesage : " + message);
}

}

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Value;

/**
* Created by admin on 2016/12/21.
*/
public class HelloWorld {
@Value("Hello World!")
private String message;

public  void setMessage(String message)
{
this.message = message;
}

public void getMessage(){
System.out.println("Your mesage : " + message);
}

}

package com.tutorialspoint;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;

/**
* Created by admin on 2016/12/21.
*/
public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{

public void onApplicationEvent(ContextStoppedEvent event){
System.out.println("ContextStopped Received");
}

}

package com.tutorialspoint;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;

/**
* Created by admin on 2016/12/21.
*/
public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{

public void onApplicationEvent(ContextStoppedEvent event){
System.out.println("ContextStopped Received");
}

}

package com.tutorialspoint;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;

/**
* Created by admin on 2016/12/21.
*/
public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{

public void onApplicationEvent(ContextStoppedEvent event){
System.out.println("ContextStopped Received");
}

}


下面对自定义事件展开简要介绍,

自定义事件需要定义继承org.springframework.context.ApplicationEvent

的事件类,由于基类没有提供默认构造函数,在继承时必须重载基于source

的构造函数,source参数指定了事件的publisher,一般在此publisher中

的某个函数中对此事件进行初始化,并调用此publisher的一个方法

实现事件的publish。

值得注意的是,下面例子中的publish的细节是调用一个ApplicationEventPublisher接口

的实现类相应的publishEvent函数。

(这里使用了java的一个特性,即java能够对接口及其实现类实施基类与派生类的多态

关系,基本上就是虚基类的作用)

事件处理的部分已经在前面提到了(onApplicationEvent)

下面是示例代码:

package com.tutorialspoint;

import org.springframework.context.ApplicationEvent;

/**
* Created by admin on 2016/12/21.
*/
public class CustomEvent extends ApplicationEvent {

public CustomEvent(Object source){
super(source);
}

public String toString(){
return "My Custom Event";
}

}

package com.tutorialspoint;

import org.springframework.context.ApplicationEvent;

/**
* Created by admin on 2016/12/21.
*/
public class CustomEvent extends ApplicationEvent {

public CustomEvent(Object source){
super(source);
}

public String toString(){
return "My Custom Event";
}

}

package com.tutorialspoint;

import org.springframework.context.ApplicationListener;

/**
* Created by admin on 2016/12/21.
*/
public class CustomEventHandler implements ApplicationListener<CustomEvent> {

public void onApplicationEvent(CustomEvent event){
System.out.println(event.toString());
}

}

package com.tutorialspoint;

import org.springframework.context.annotation.*;

/**
* Created by admin on 2016/12/21.
*/
@Configuration
public class TestConfigure {

@Bean
public CustomEventHandler customEventHandler(){
return new CustomEventHandler();
}

@Bean
public CustomEventPublisher customEventPublisher(){
return new CustomEventPublisher();
}

}

package com.tutorialspoint;

import org.springframework.context.annotation.*;
import org.springframework.context.ConfigurableApplicationContext;

/**
* Created by admin on 2016/12/21.
*/
public class MainApp {
public static void main(String [] args){

ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(TestConfigure.class);
CustomEventPublisher cvp = (CustomEventPublisher) context.getBean(CustomEventPublisher.class);
cvp.publish();
cvp.publish();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息