
uk.org.retep.kernel.annotations.PostInit Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*
*
*
* GNU GENERAL PUBLIC LICENSE - CLASSPATH EXCEPTION
*
*
*
* Linking this library statically or dynamically with other modules
* is making a combined work based on this library. Thus, the terms
* and conditions of the GNU General Public License cover the whole
* combination.
*
*
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module.
*
*
*
* An independent module is a module which is either not derived from or based
* on this library, or a module who's classes extend those within this library
* as part of the implementation of the library.
*
*
*
* If you modify this library, you may extend this exception to your version
* of the library, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
*/
package uk.org.retep.kernel.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* A method marked with this annotation will be called after the application
* has been started.
*
*
* This is the same as the @Init and @PostConstruct annotations except
* that when this method is run, all beans within the application have been
* deployed.
*
*
*
* The methods are run in the order that they were deployed.
*
*
*
* Only one method in the bean can be annotated by this annotation.
*
*
*
* For example: You have an application where you have plugin components that
* automatically register themselves to a plugin manager bean. Each plugin
* provides a Swing JComponent to display within a JFrame which is provided by
* the manager bean.
*
*
*
* First we'll create an interface defining a plugin.
*
*
*
* public interface Plugin {
* JComponent getComponent();
* }
*
*
*
* Now we'll define a plugin for this component. Here it has a dependency on the
* pluginManager bean and we annotate the init() method with @Init. The init()
* method then creates the component and registers the plugin with the PluginManager
* bean.
* We also disable lazy initialisation in the @Bean annotation because no
* other bean depends on our plugin - without this the plugin will never start.
*
*
*
* @Bean( name="aPlugin", lazyInit=false )
* public class SimplePlugin implements Plugin {
* private PluginManager pluginManager;
* private JPanel panel;
* private JTextField url;
* private JButton button;
*
* public PluginManager getPluginManager() {
* return pluginManager;
* }
*
* @Reference( "pluginManager" )
* public void setPluginManager( final PluginManager pluginManager ) {
* this.pluginManager = pluginManager;
* }
*
* @Init
* @DispatchThread( invocationType=InvocationType.INVOKE_AND_WAIT )
* public void init() {
* // create subcomponents here
* panel = new JPanel();
* panel.setLayout( new FlowLayout() );
* panel.add( new JLabel( "Url" ) );
* url = new JTextField( "http://retep.org/" );
* panel.add( url );
* button = new JButton( "Go" );
* panel.add( button );
*
* // register the plugin
* getPluginManager().registerPlugin( this );
* }
*
* public JComponent getComponent() {
* return panel;
* }
* }
*
*
*
* Now we have our PluginManager. Here it creates a JFrame, a JPanel to contain
* our plugins and a PostInit method to finally pack and display the JFrame.
*
*
*
* @Bean( name="pluginManager", lazyInit=false )
* public class PluginManager {
* private JFrame frame;
* private JPanel pluginPanel;
*
* @Init
* @DispatchThread( invocationType=InvocationType.INVOKE_AND_WAIT )
* public void init() {
* // create the frame for the application but don't show it
* frame = new JFrame( "My Application" );
* pluginPanel = new JPanel();
* pluginPanel.setLayout( new FlowLayout() );
* frame.getContentPane().add( pluginPanel, BorderLayout.NORTH );
* }
*
* @PostInit
* @DispatchThread( invocationType=InvocationType.INVOKE_LATER )
* public void makeVisible()
* {
* // Our application is now running so pack and display it to the user
* frame.pack();
* frame.setVisible( true );
* }
*
* @DispatchThread( invocationType=InvocationType.INVOKE_AND_WAIT )
* public void registerPlugin( Plugin plugin ) {
* // here just add it to the pluginPanel
* pluginPanel.add( plugin.getComponent() );
* }
* }
*
*
*
* Note: Because the above example uses Swing components, you must access those
* components from within the Swing dispatch thread (even when constructing them)
* because swing is single threaded.
* The @DispatchThread annotation is part of the retepTools library and ensures
* that the methods are invoked within the Swing thread rather than the thread
* that Spring is running under.
*
*
*
*
* @author peter
* @see Bean
* @see Init
* @see uk.org.retep.annotations.DispatchThread
*/
@Target( ElementType.METHOD )
@Retention( RetentionPolicy.RUNTIME )
public @interface PostInit
{
}