(i) Spring container related operations

Source: Internet
Author: User

One, spring events

The spring event has the following two members.

1,applicationevent, container events, released by the container

2,Applicationlistener Listener, can be any listener bean in the container as

(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); this.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) write the Container Listener code:

Package Cn.study.basic;import Org.springframework.context.applicationlistener;public class EmailListener implements applicationlistener<emailevent> {@Overridepublic 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), add the following configuration to the Bean.xml file:

<bean class= "Cn.study.basic.EmailListener" ></bean>

(4), 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.emailevent;public Class Testamain {@Testpublic void testApp () throws Exception {ApplicationContext context = new Classpathxmlapplicationcontext ( "Bean.xml"); EmailEvent emailevent = new EmailEvent ("Object", "Address", "test"); Context.publishevent (emailevent);}}

Run the code, and the execution results are as follows:

True
Address:address

Second, the bean gets the spring container

In the process of web development, spring containers are typically generated using declarative method configuration, Developers only need to configure the appropriate listener in Web. Xml to initialize the spring container at boot time, but in some special cases, the bean in the container needs to actively access the spring container, as in the next two ways.

(1), the implementation of the Beanfactoryaware interface, in the implementation of the interface, the following methods must be implemented.

public void Setbeanfactory (Beanfactory arg0) throws Beansexception {}

(2), implement Applicationcontextaware interface, and need to implement the following methods

        @Overridepublic void Setapplicationcontext (ApplicationContext arg0) throws Beansexception {}

The following small example is implemented using the second method, which implements the interface first, with the following code:

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; @Overridepublic void Setapplicationcontext (ApplicationContext arg0) throws beansexception {this.ctx = arg0;} Public ApplicationContext GetContext () {return CTX;}}

You also need to configure the Bean in the Bean.xml file, as follows

<bean class= "Cn.study.basic.GetApContext" ></bean>

The test method is as follows:

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 {@Testpublic void TestContext () throws Exception {ApplicationContext context = new Classpathxmlapplicat Ioncontext ("Bean.xml"); Getapcontext CTX = (getapcontext) context.getbean ("SpContext"); System.out.println (Ctx.getcontext ()); System.out.println (Context==ctx.getcontext ());}}

The test results are as follows:

--->org[email protected]2c11b4c2:startup date [Fri Oct 14:04:27 CST 2014]; Root of context Hierarchy
True

(i) Spring container related operations

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.