Spring container operations

Source: Internet
Author: User

Spring container operations
I. The spring event has the following two members. 1. ApplicationEvent, container event, published by container 2. ApplicationListener listener, which can be held by any listener Bean in the container (1) first a spring container event: package cn. study. basic; import org. springframework. context. applicationEvent; public class EmailEvent extends ApplicationEvent {private String address; private String text; public EmailEvent (Object source) {super (source);} public EmailEvent (Object source, String address, string text) {super (source); t His. address = address; this. text = text;} public String getAddress () {return address;} public void setAddress (String address) {this. address = address;} public String getText () {return text;} public void setText (String text) {this. text = text ;}}( 2) Compile the container listener code: package cn. study. basic; import org. springframework. context. applicationListener; public class EmailListener implements ApplicationLis Tener <EmailEvent >{@ Override public void onApplicationEvent (EmailEvent arg0) {System. out. println (arg0 instanceof EmailEvent); if (arg0 instanceof EmailEvent) {EmailEvent ee = (EmailEvent) arg0; System. out. println ("address:" + ee. getAddress ();} else {System. out. println ("container:" + arg0) ;}}( 3), bean. add the following configuration to the xml file: 1 <bean class = "cn. study. basic. emailListener "> </bean> (4). Test Method package cn. stud Y. basic. test; import org. junit. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import cn. study. basic. emailEvent; public class TestAMain {@ Test public void testApp () throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext ("bean. xml "); EmailEvent emailEvent = new EmailEvent (" object "," ad Dress "," test "); context. publishEvent (emailEvent) ;}} run the code. The execution result is as follows: trueaddress: address 2. bean obtains the spring container during web development, spring containers are usually configured using declarative methods. Developers only need. configure the corresponding Listener in xml to initialize the Spring container at startup. However, in some special cases, the Bean in the container needs to actively access the Spring container in two ways. (1) to implement the BeanFactoryAware interface, you must implement the following methods while implementing the interface. Public void setBeanFactory (BeanFactory arg0) throws BeansException {} (2), implements the ApplicationContextAware interface, and needs to implement the following method @ Overridepublic void setApplicationContext (ApplicationContext arg0) the following uses the second method to implement the following small example of throws BeansException {}: package cn. study. basic; import org. springframework. beans. beansException; import org. springframework. context. applicationContext; import org. springframework. context. applicationContextAware; public class GetApContext implements ApplicationContextAware {private ApplicationContext ctx; @ Override public void setApplicationContext (ApplicationContext arg0) throws BeansException {this. ctx = arg0;} public ApplicationContext getContext () {return ctx ;}} also needs to be in bean. configure the bean in the xml file, as shown in the following 1 <bean class = "cn. study. basic. getApContext "> </bean> Test method: package cn. study. basic. test; import org. junit. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import cn. study. basic. getApContext; public class TestContext {@ Test public void testContext () throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext ("bean. xml "); GetApContext ctx = (GetApContext) context. getBean ("spContext"); System. out. println (ctx. getContext (); System. out. println (context = ctx. getContext () ;}} test results: ---> org. springframework. context. support. classPathXmlApplicationContext @ 2c11b4c2: startup date [Fri Oct 03 14:04:27 CST 2014]; root of context hierarchytrue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.