1.基於xml的配置
2.基於java類的配置
@Configuration 的註解類表示這個類可以使用 Spring IoC 容器作爲 bean 定義的來源。
@Bean 註解告訴 Spring,一個帶有 @Bean 的註解方法將返回一個對象,該對象應該被註冊爲在 Spring 應用程序上下文中的 bean。
1。bean類
package dc.xx;
public class Helloword {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
2。配置類
package dc.xx;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
@Configuration
@Import(Helloword.class) //注入一個 Bean
public class HelloworldConfig {
@Bean
@Scope("prototype") //單例
public Helloword helloword(){
return new Helloword();
}
}
3.測試
package dc.xx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Testjava {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloworldConfig.class);
Helloword helloWorld = ctx.getBean(Helloword.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
Spring內置事件。
序號 | Spring 內置事件 & 描述 |
---|---|
1 |
ContextRefreshedEvent ApplicationContext 被初始化或刷新時,該事件被髮布。這也可以在 ConfigurableApplicationContext 接口中使用 refresh() 方法來發生。 |
2 |
ContextStartedEvent 當使用 ConfigurableApplicationContext 接口中的 start() 方法啓動 ApplicationContext 時,該事件被髮布。你可以調查你的數據庫,或者你可以在接受到這個事件後重啓任何停止的應用程序。 |
3 |
ContextStoppedEvent 當使用 ConfigurableApplicationContext 接口中的 stop() 方法停止 ApplicationContext 時,發佈這個事件。你可以在接受到這個事件後做必要的清理的工作。 |
4 |
ContextClosedEvent 當使用 ConfigurableApplicationContext 接口中的 close() 方法關閉 ApplicationContext 時,該事件被髮布。一個已關閉的上下文到達生命週期末端;它不能被刷新或重啓。 |
5 |
RequestHandledEvent 這是一個 web-specific 事件,告訴所有 bean HTTP 請求已經被服務。 |
監聽上下文
1.被監聽了內容。
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
2.監聽開始,即上文
public class CStartEventHandler implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
3.監聽結束,即下文
public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
4.配置文件
<?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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
<bean id="cStartEventHandler"
class="com.tutorialspoint.CStartEventHandler"/>
<bean id="cStopEventHandler"
class="com.tutorialspoint.CStopEventHandler"/>
</beans>
5.測試類
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
// Let us raise a start event.
context.start();
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
// Let us raise a stop event.
context.stop();
}
}