📄 resin-ioc.xtp
字号:
<li><code>@SessionScoped</code> creates a new instance for eachHTTP session, reusing the instance for the same session.</li><li><code>@ApplicationScoped</code> uses a single instance in eachweb-app. For web applications, this will have the same lifecycle as<code>@Singleton</code>.</li><li><code>@Singleton</code> uses a single instance in thecontainer. For web applications, this will have the same lifecycle as<code>@ApplicationScoped</code>.</li></ul><p>An example scoped resource might be a <code>Calculator</code> object whichis used only for a single instance. It might fill the arguments whileprocessing a form and then calculate the result. The<code>@RequestScoped</code> makes sure scripts receive the same instanceeach time it's called.</p><example title="Example: @RequestScoped Calculator">import javax.webbeans.RequestScoped;import javax.webbeans.Component;@RequestScoped@Componentpublic class Calculator { private int _a; private int _b; public void setA(int a) { _a = a; } public void setB(int b) { _b = b; } public int getSum() { return _a + _b; }}</example><p>You could also register the same calculator using XML:</p><example title="Example: resin-web.xml Calculator"><web-app xmlns="http://caucho.com/ns/resin"> <component class="example.Calculator" scope="request"/></web-app></example></s2><s2 title="@New Component Discovery"><p>The <code>@New</code> annotation automatically registers the targetclass with the WebBeans registry and tells Resin to create a new instanceof the bean, even if the bean has a singleton scope definition.Since <code>@New</code> replaces <code>@In</code>, it can be used anywhere<code>@In</code> can be used.</p><example title="Example: MyServlet.java">import javax.webbeans.New;public class MyServlet extends GenericServlet { @New Movie _movie; ...}</example><p>The <code>Movie</code> is identical to the movie class definedabove, but doesn't need the <code>@Component</code> annotation anddoesn't need the <code>META-INF/web-beans.xml</code> marker file.In many cases, the <code>@New</code> annotation can replace the Java<code>new</code> operator. If your application startsusing <code>@New</code> consistently, it can add injection capabilities toa growing share of your code, letting you simplify and refactor incrementally.</p><example title="Example: Movie.java">public class Movie { private String _title; public String getTitle() { return _title; } public void setTitle(String title) { _title = title; } @PostConstruct public void init() { ... }}</example></s2><s2 title="Lifecycle: @PostConstruct and @PreDestroy"><p>If your service needs to initialize itself after being configured, itcan annotation an initialization method with <code>@PostConstruct</code>.After Resin creates, injects, and configures your component, it will call any<code>@PostConstruct</code> methods. Long-lived services or services thatneed to register themselves with other services will typically need touse the <code>@PostConstruct</code> annotation.</p><p>At the end of a component's lifetime, you might need to close someresources, e.g. closing a socket or delisting from a timer service. Resinwill call any component method marked with <code>@PreDestroy</code>before it destroys the method.</p><p>For example, a <code>TimerService</code> may want to schedule a eventevery 2 seconds. The <code>@PostConstruct</code> method will start the timerand the <code>@PreDestroy</code> method will stop the timer.</p><example title="Example: Timer Service">import javax.annotation.PostConstruct;import javax.webbeans.In;import javax.webbeans.Component;import java.util.concurrent.ScheduledExecutorService;import com.caucho.webbeans.Singleton;@Component@Singletonpublic class TimerService { @In ScheduledExecutorService _timer; @PostConstruct public void init() { _timerFuture = _timer.scheduleAtFixedRate(this, 0, 2, TimeUnit.SECONDS); } ... @PreDestroy public void close() { _timerFuture.cancel(false); }}</example></s2></s1><s1 title="XML configuration"><p>You can register your components and services with Resin using theresin.xml or resin-web.xml files. Since the WebBeans registry is integratedwith Resin, your services be treated as first-class components along withthe Resin resources. Although most components will not need XML, thereare a few advantages for the small number of services which do use XML.</p><p>The XML-configuration lets you customize your application for aparticular environment, e.g. setting configuration parameters. For example,Resin's <code><database></code> needs to select a database driver andconfigure the URL, user and password of the database as well as configuringconnection pooling parameters. Some application services will also needconfiguration.</p><p>In addition, the XML-configuration documents the servicesyou've enabled. For heavyweight services, this documentation is critical,while lightweight components do not need this extra housekeeping overhead.</p><s2 title="bean and component registration"><p>The <code><bean></code> and <code><component></code> tags registerapplication classes with Resin. The two tags are identical except for theexpected lifecycle: <code><bean></code> configures singleton services while<code><component></code> configures component template classes.In other words, the default scope of a <code><bean></code>is <code>@Singleton</code> while the default scopeof a <code><component></code> is <code>@Dependent</code>.A <code><component></code> will create a new instance each time it'sinjected or referenced, while a <code><bean></code> always returnsthe same singleton instance.</p><example title="Example: bean and component resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.MyService"/> <component class="example.MyService"/></web-app></example><p>The <code><bean></code> and <code><component></code> tags have anumber of optional attributes:</p><deftable title="bean and component attributes"><tr> <th>Attribute</th> <th>Description</th></tr><tr> <td>binding</td> <td>the <code>@BindingType</code> annotations for custom bean matching</td></tr><tr> <td>class</td> <td>the Java class of the bean or component (required)</td></tr><tr> <td>init</td> <td>optional configuration/initialization block, using bean-style assignment</td></tr><tr> <td>name</td> <td>the instance name, used for <code>@Named</code> and script integration</td></tr><tr> <td>scope</td> <td>specifies scope of the instances: request, conversation, session, application, or singleton</td></tr></deftable></s2><s2 title="Bean property configuration"><p>Resin's XML configuration uses the standard JavaBeans patterns toconfigure properties. Resin uses the same mechanism for all of its ownconfiguration parsing, including every JavaEE configuration file, theresin-web.xml and the resin.xml itself. So your application will haveall the configuration flexibility it needs.</p><p>Since the component beans can use WebBeansinjections, injected components are typically not configured inthe resin-web.conf, avoiding the need for tags like <code><ref></code>.</p><example title="Example: Hello.java">public class Hello { private String _greeting = "default"; public void setGreeting(String greeting) { _greeting = greeting; } public String getGreeting() { return _greeting; }}</example><p>The basic example sets a <code>greeting</code> property of a hello, worldbean. In this example, we're configuring a singleton, but the<code><init></code> works for dependent components as well. Resin willapply the configuration to the instance as part of the creation process.</p><example title="Example: resin-web.xml configuring a singleton"><web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.Hello"> <init> <greeting>Hello, World</greeting> </init> </bean></web-app></example><p>Resin's configuration uses 5 basic bean patterns, extending the JavaBeansconventions. It can configure literal values like string and integers aswell as configuring other beans. Any component bean configured by Resinhas full access to <code>@In</code> injection as well asthe standard <code>@PostConstruct</code> annotations. Sub-beans arenot automatically registered with WebBeans, i.e. they act like theservlet configuration.</p><p>(Currently the patterns are name-based like JavaBeans, since Resinwas designed before annotations. We may add configuration annotationsin the future.</p><example title="Example: Bean configuration patterns"> public void setFoo(String data); public void setFoo(Movie data); public void addFoo(Movie data); public Movie createFoo(); public void setText(String data);</example><ol><li><code>setFoo(String)</code> configures a standard JavaBeans-styleliteral.</li><li><code>setFoo(Movie)</code> creates a new instance of <code>Movie</code> and recursively configures it.</li><li><code>addFoo(Movie)</code> also creates a new instance of <code>Movie</code> and recursively configures it. <code>addFoo</code> is an easy way ofconfiguring lists.</li><li><code>Movie createFoo()</code> lets the bean createthe <code>Movie</code> instance. Many beans canuse <code>createFoo</code> with inner classes tohandle complex configuration.</li><li><code>setText</code> is called with the text contents ofthe XML. Value-style beans will use this. (somewhat rare).</li></ol><p>As mentioned above, Resin uses these 5 patterns to handle all of theJavaEE configuration files. In particular, the <code>createFoo</code>pattern returning inner classes is very handy for some complicatedconfiguration cases, and for cases where a sub-tag needs information aboutthe parent.</p><example title="Example: sub-bean configuration example"><web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.Theater"> <init> <name>Balboa</name> <movie title="The Princess Bride"/> <movie title="The Maltese Falcon"/> </init> </bean></web-app></example><p>In this example, the <code>Theater</code> classes usesan inner <code>Movie</code> class to illustrate the use ofthe <code>create</code> pattern.</p><example title="Example: Theater.java">public class Theater { String _name; ArrayList<Movie> _movies = new ArrayList<Movie>(); public void setName(String name) { _name = name; } public Movie createMovie() { return new Movie(this); } public void addMovie(Movie movie) { _movies.add(movie); } public static class Movie { private Theater _theater; private String _title; Movie(Theater theater) { _theater = theater; } public void setTitle(String title) { _title = title; } }}</example></s2><s2 title="Base configuration: string conversions"><p>Resin-IoC provides a number of built-in string conversion types as wellas supporting JavaBeans <code>PropertyEditor</code> and custom converters.</p><deftable title="Built-in String Converters"><tr> <th>Type</th> <th>Description</th></tr><tr> <td>boolean, Boolean</td> <td>Java boolean</td></tr><tr> <td>byte, Byte</td> <td>Java byte</td></tr><tr> <td>short, Short</td> <td>Java short</td></tr><tr> <td>int, Integer</td> <td>Java integer</td></tr><tr> <td>long, Long</td> <td>Java long</td></tr><tr> <td>float, Float</td> <td>Java float</td></tr><tr> <td>double, Double</td> <td>Java double</td></tr><tr> <td>char, Character</td> <td>Java char</td></tr><tr> <td>String[]</td> <td>String array separated by commas</td></tr><tr> <td>Class</td> <td>Java classes</td></tr><tr> <td>Path</td> <td>Resin VFS Paths</td></tr><tr> <td>File</td> <td>java.io.File</td></tr><tr> <td>URL</td> <td>java.net.URL</td></tr><tr> <td>Pattern</td> <td>java.util.regex.Pattern</td></tr><tr> <td>Locale</td> <td>java.util.Locale</td></tr><tr> <td>Date</td> <td>java.util.Date</td></tr><tr> <td>Properties</td> <td>java.util.Properties</td></tr><tr> <td>RawString</td> <td>com.caucho.config.type.RawString</td></tr></deftable><s3 title="enumerations"><p>Enumerations are automatically converted from theirstring representation.</p></s3><s3 title="String constructor"><p>Resin-IoC will automatically convert a string to an object if theobject has a single String argument constructor.</p><example title="Example: MyBean with constructor">public class MyBean { public MyBean(String value) { ... }}</example></s3><s3 title="valueOf"><p>For classes which implement a static <code>valueOf(String)</code> method,Resin will automatically convert to the given type usingthe <code>valueOf</code> method.</p><example title="Example: MyBean with valueOf">public class MyBean { ... public static MyBean valueOf(String text) { MyBean bean = new MyBean(); bean.setTextValue(text); bean.init(); return bean; }}</example></s3><s3 title="setValue"><p>For objects with a <code>setValue</code> or <code>addText</code> methodand a zero-argument constructor, Resin-IoC will convert using thefollowing steps:</p><ol><li>Create the object</li><li>Inject any dependencies</li><li>Call <code>setValue</code> or <code>setText</code> with the string</li><li>Call any @PostConstruct</li><li>Return the configured bean</li></ol></s3></s2><s2 title="Compound types"><s3 title="list">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -