기본 지원
우선 어떤 기본 이벤트가 지원되는지 살펴보자
프로그램 1(서비스)
우선 살펴보기 프로그램에서
ApplicationEventPublisher는 봄 항목이며 전송을 위해 주입되어야 합니다. ApplicationEventPublisherAware가 구현되어 있기 때문에 setApplicationEventPublisher 메소드가 자동으로 이를 호출하고 브로드캐스트 발신자를 가져옵니다
/** * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */public class EmailService implements ApplicationEventPublisherAware { private List<String> blackList; private ApplicationEventPublisher publisher; public void setBlackList(List<String> blackList) { this.blackList = blackList; } public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } /** * 具体广播类 * @param address * @param text */ public void sendEmail(String address, String text) { if (blackList.contains(address)) { BlackListEvent event = new BlackListEvent(this, address, text); publisher.publishEvent(event); return; } // send email... } }
프로그램 2(이벤트)
여기에서도 ApplicationEvent를 상속해야 하며, 방송을 수신할 때 얻을 수 있도록 필요한 매개변수 등을 직접 구현할 수 있습니다. 또한 소스
/** * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */public class BlackListEvent extends ApplicationEvent { private String address; private String test; public BlackListEvent(Object source, String address, String test) { super(source); this.address = address; this.test = test; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTest() { return test; } public void setTest(String test) { this.test = test; } }
프로그램 3(수신기)
스프링을 사용할 때 여전히 해당 사양 세트를 따라야 합니다. 그런 다음 수신기는 ApplicationListener 인터페이스를 구현해야 합니다. 일반 브로드캐스트를 수신하면 onApplicationEvent 인터페이스가 전달됩니다
물론 spring은 매우 사려 깊고 반드시 ApplicationListener 클래스를 구현하지는 않습니다. 주석 @EventListener
/** * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */public class BlackListNotifier implements ApplicationListener<BlackListEvent> { private String notificationAddress; public void setNotificationAddress(String notificationAddress) { this.notificationAddress = notificationAddress; } @EventListener public void onApplicationEvent(BlackListEvent event) { // notify appropriate parties via notificationAddress... System.out.println("onApplicationEvent, some thing I receive:" + event.getAddress() + ",text:" + event.getTest()); } @EventListener(condition = "#event.test == 'foo'") public void onApplicationCustomerEvent(BlackListEvent event) { System.out.println("onApplicationCustomerEvent,some thing I receive:" + event.getAddress() + ",text:" + event.getTest()); // notify appropriate parties via notificationAddress... } @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class}) public void handleContextStart() { System.out.println("-------------handleContextStart"); } /** * 参数可以给BlackListEvent 可以不给 */ @EventListener(classes = {BlackListEvent.class}) public void handleBlackListEvent() { System.out.println("-------------handleBlackListEvent"); } }
@EventListener
이 주석을 사용하는 방법을 분석해 보겠습니다. 위 프로그램과 마찬가지로 인터페이스 구현 외에도 조건을 통해 SpEL 표현식을 사용할 수 있습니다. , 조건이 충족되었을 때만 실행됩니다. 이벤트 객체가 트리거될 때 이 클래스가 실행됩니다. 프로그램 4(config bean)여기서는 주로/** * 配置 * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */@Configurationpublic class AppConfig { @Bean public EmailService emailService() { EmailService s = new EmailService(); List<String> emails = new ArrayList<>(3); emails.add("known.spammer@example.org"); emails.add("known.hacker@example.org"); emails.add("john.doe@example.org"); s.setBlackList(emails); return s; } @Bean public BlackListNotifier notifier() { BlackListNotifier notifier = new BlackListNotifier(); notifier.setNotificationAddress("blacklist@example.org"); return notifier; } }
을 수락하기 위해 일부 서비스를 등록하고 브로드캐스트 Bean을 수락합니다.

이 기사에서는 Java 프로젝트 관리, 구축 자동화 및 종속성 해상도에 Maven 및 Gradle을 사용하여 접근 방식과 최적화 전략을 비교합니다.

이 기사에서는 Maven 및 Gradle과 같은 도구를 사용하여 적절한 버전 및 종속성 관리로 사용자 정의 Java 라이브러리 (JAR Files)를 작성하고 사용하는 것에 대해 설명합니다.

이 기사는 카페인 및 구아바 캐시를 사용하여 자바에서 다단계 캐싱을 구현하여 응용 프로그램 성능을 향상시키는 것에 대해 설명합니다. 구성 및 퇴거 정책 관리 Best Pra와 함께 설정, 통합 및 성능 이점을 다룹니다.

이 기사는 캐싱 및 게으른 하중과 같은 고급 기능을 사용하여 객체 관계 매핑에 JPA를 사용하는 것에 대해 설명합니다. 잠재적 인 함정을 강조하면서 성능을 최적화하기위한 설정, 엔티티 매핑 및 모범 사례를 다룹니다. [159 문자]

Java의 클래스 로딩에는 부트 스트랩, 확장 및 응용 프로그램 클래스 로더가있는 계층 적 시스템을 사용하여 클래스로드, 링크 및 초기화 클래스가 포함됩니다. 학부모 위임 모델은 핵심 클래스가 먼저로드되어 사용자 정의 클래스 LOA에 영향을 미치도록합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

Dreamweaver Mac版
시각적 웹 개발 도구
