Spring 基于注解的配置(5)

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());
    }
}

测试
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章