Spring 基於註解的配置
Spring @Required 註釋
@Required 註釋應用於 bean 屬性的 setter 方法,它表明受影響的 bean 屬性在配置時必須放在 XML 配置文件中,否則容器就會拋出一個 BeanInitializationException 異常
springconfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for student bean -->
<bean id="student" class="student">
<property name="name" value="Zara"></property>
<property name="age" value="11"></property>
</bean>
</beans>
student.java
import org.springframework.beans.factory.annotation.Required;
import org.springframework.test.annotation.Repeat;
@SuppressWarnings("deprecation")
public class student {
private String name;
private Integer age;
@Required
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Required
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
}
Spring @Autowired 註釋
@Autowired 註釋對在哪裏和如何完成自動連接提供了更多的細微的控制。
屬性中的 @Autowired
springconfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="textEditor">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="spellChecker">
</bean>
</beans>
textEditor.java
import org.springframework.beans.factory.annotation.Autowired;
public class textEditor {
@Autowired
private spellChecker spellChecker;
private String name;
public spellChecker getChecker() {
return spellChecker;
}
public void callcheck(){
System.out.println(this.getClass().getName());
this.spellChecker.check();
}
public String getName() {
return name;
}
}
Setter 方法中的 @Autowired
springconfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="textEditor">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="spellChecker">
</bean>
</beans>
textEdirot.java
import org.springframework.beans.factory.annotation.Autowired;
public class textEditor {
spellChecker checker;
public textEditor(){
}
@Autowired
public void setChecker(spellChecker checker) {
this.checker = checker;
}
public spellChecker getChecker() {
return checker;
}
public void spllcheck(){
this.checker.checkSpelling();
}
}
構造函數中的 @Autowired
textEditor.java
import org.springframework.beans.factory.annotation.Autowired;
public class textEditor {
spellChecker checker;
@Autowired
public textEditor(spellChecker checker){
this.checker = checker;
}
public void setChecker(spellChecker checker) {
this.checker = checker;
}
public spellChecker getChecker() {
return checker;
}
public void spllcheck(){
this.checker.checkSpelling();
}
}
Spring @Qualifier 註釋
可能會有這樣一種情況,當你創建多個具有相同類型的 bean 時,並且想要用一個屬性只爲它們其中的一個進行裝配,在這種情況下,你可以使用 @Qualifier 註釋和 @Autowired 註釋通過指定哪一個真正的 bean 將會被裝配來消除混亂。
springconfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="profile" class="profile"></bean>
<bean id="student1" class="student">
<property name="name" value="zhw"></property>
<property name="age" value="11"></property>
</bean>
<bean id="student2" class="student">
<property name="name" value="hong"></property>
<property name="age" value="22"></property>
</bean>
</beans>
profile.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class profile {
@Autowired
@Qualifier("student1")
private student stu;
public profile(){
System.out.println("inside profile");
}
public void printAge(){
System.out.println("Age"+stu.getAge());
}
public void printName(){
System.out.println("name"+stu.getName());
}
}
Spring 中的事件處理
當加載 beans 時,ApplicationContext 發佈某些類型的事件。例如,當上下文啓動時,ContextStartedEvent 發佈,當上下文停止時,ContextStoppedEvent 發佈。通過 ApplicationEvent 類和 ApplicationListener 接口來提供在 ApplicationContext 中處理事件。如果一個 bean 實現 ApplicationListener,那麼每次 ApplicationEvent 被髮布到 ApplicationContext 上,那個 bean 會被通知。
springconfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="profile" class="profile" init-method="init" destroy-method="del">
<property name="name" value="zhw"></property>
</bean>
<bean id="startevent" class="startevent"></bean>
<bean id="stopevent" class="stopevent"></bean>
</beans>
startevent.java
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class startevent implements ApplicationListener<ContextStartedEvent> {
@Override
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("start event receive");
}
}
stopevent.java
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
public class stopevent implements ApplicationListener<ContextStoppedEvent> {
@Override
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("receive stop event");
}
}
main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
context.start();
profile profile = (profile) context.getBean("profile");
profile.printname();
context.stop();
}
}
Spring 中的自定義事件
1.繼承ApplicationEvent自定義事件
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source){
super(source);
}
public String toString(){
return "my custom event";
}
}
2.實現ApplicationEventPublisherAware接口
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class customeventpublish implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
public void publish(){
CustomEvent ce = new CustomEvent(this);
publisher.publishEvent(ce);
}
}
3.實現ApplicationListener接口,監聽自定義發送的事件
import org.springframework.context.ApplicationListener;
public class customeventhandler implements ApplicationListener<CustomEvent> {
@Override
public void onApplicationEvent(CustomEvent event) {
System.out.println(event.toString());
}
}
測試