### Initialize HashMap with Arguments
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Demonstrates how to initialize a HashMap component in Seasar2. It shows the use of 'initMethod' with both explicit arguments and OGNL expressions to populate the map. This is useful for setting initial values or performing setup actions.
```xml
"aaa"
111
#self.put("aaa", 111)
#out.println("Hello")
```
--------------------------------
### Manual DI Setup with GreetingMain (Java)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
Demonstrates a basic Dependency Injection setup in Java where the main class directly instantiates and wires dependent objects. This approach requires code modification for configuration changes.
```Java
package examples.di.main;
import examples.di.Greeting;
import examples.di.impl.GreetingClientImpl;
import examples.di.impl.GreetingImpl;
public class GreetingMain {
public static void main(String\[\] args) {
Greeting greeting = new GreetingImpl();
GreetingClientImpl greetingClient = new GreetingClientImpl();
greetingClient.setGreeting(greeting);
greetingClient.execute();
}
}
```
--------------------------------
### Main Application Class for Seasar2 EJB3 Example in Java
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/ejb3.html
The main class to run the Seasar2 EJB3 example. It initializes the Seasar container using SingletonS2ContainerFactory and looks up the GreetingClient to execute it.
```java
package examples.ejb3.main;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
import examples.ejb3.GreetingClient;
public class GreetingMain {
private static final String PATH = "examples/ejb3/dicon/GreetingMain.dicon";
public static void main(String\[\] args) throws Exception {
SingletonS2ContainerFactory.setConfigPath(PATH);
SingletonS2ContainerFactory.init();
try {
doMain(args);
} finally {
SingletonS2ContainerFactory.destroy();
}
}
public static void doMain(String\[\] args) throws Exception {
Context ctx = new InitialContext();
GreetingClient greetingClient = (GreetingClient)
ctx.lookup("greetingClient");
greetingClient.execute();
}
}
```
--------------------------------
### Seasar2 InitMethod Annotation Examples
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
Demonstrates the @InitMethod annotation in Seasar2, used to specify initialization methods for components. Includes examples for Tiger and Backport175 annotation syntax, as well as the constant-based approach for defining initialization methods.
```java
public class Xxx {
...
@InitMethod
public void init() {
...
}
}
```
```java
public class Xxx {
...
/**
* @org.seasar.framework.container.annotation.backport175.InitMethod
*/
public void init() {
...
}
}
```
```java
public static final String INIT_METHOD = "init";
```
--------------------------------
### Seasar2 DI Initialization (Java)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Java code to initialize the Seasar2 container and retrieve components. It uses S2ContainerFactory to create the container from a DICON file and then gets a specific component by its name.
```java
package examples.di.main;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
import examples.di.GreetingClient;
public class GreetingMain2 {
private static final String PATH =
"examples/di/dicon/GreetingMain2.dicon";
public static void main(String[] args) {
S2Container container =
S2ContainerFactory.create(PATH);
container.init();
GreetingClient greetingClient = (GreetingClient)
container.getComponent("greetingClient");
greetingClient.execute();
}
}
```
--------------------------------
### Select Multiple Employee Records as Map List using Seasar2 JDBC
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/s2jdbc_base.html
This example shows how to fetch multiple employee records, returned as a list of maps, using Seasar2's BasicSelectHandler and MapListResultSetHandler. The configuration defines the SQL query to select all columns from the 'emp' table. The client code initializes the Seasar2 container, gets the handler, executes the query with no parameters, and then iterates through the list of maps, printing each one.
```xml
"SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno FROM emp"
```
```java
package examples.jdbc;
import java.util.List;
import org.seasar.extension.jdbc.SelectHandler;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class SelectMapListClient {
private static final String PATH =
"examples/jdbc/SelectMapList.dicon";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
container.init();
try {
SelectHandler handler = (SelectHandler)
container.getComponent("selectMapListHandler");
List result = (List) handler.execute(null);
for (int i = 0; i < result.size(); ++i) {
System.out.println(result.get(i));
}
} finally {
container.destroy();
}
}
}
```
--------------------------------
### Seasar2 AOP Integration (Java)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Java code demonstrating the execution of an application configured with AOP using Seasar2. The main method is similar to the pure DI example, but the output will include AOP trace logs due to the configuration in the DICON file.
```java
package examples.di.main;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
import examples.di.GreetingClient;
public class GreetingMain3 {
private static final String PATH =
"examples/di/dicon/GreetingMain3.dicon";
public static void main(String[] args) {
S2Container container =
S2ContainerFactory.create(PATH);
GreetingClient greetingClient = (GreetingClient)
container.getComponent("greetingClient");
greetingClient.execute();
}
}
```
--------------------------------
### S2DBCP Usage Example
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/dbcp.html
Example code demonstrating how to use S2DBCP with a typical DAO and Service pattern in Java.
```APIDOC
## S2DBCP Usage Example
This section provides example Java code for using S2DBCP within an application, illustrating a common DAO and Service implementation.
### `EmployeeDao.java`
```java
package examples.dbcp;
import java.sql.SQLException;
public interface EmployeeDao {
public String getEmployeeName(int empno) throws SQLException;
}
```
### `EmployeeDaoImpl.java`
```java
package examples.dbcp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
public class EmployeeDaoImpl implements EmployeeDao {
private DataSource dataSource_;
public EmployeeDaoImpl(DataSource dataSource) {
dataSource_ = dataSource;
}
public String getEmployeeName(int empno) throws SQLException {
String ename = null;
Connection con = dataSource_.getConnection();
try {
PreparedStatement ps = con.prepareStatement(
"SELECT ename FROM emp WHERE empno = ?");
try {
ps.setInt(1, empno);
ResultSet rs = ps.executeQuery();
try {
if (rs.next()) {
ename = rs.getString("ename");
}
} finally {
rs.close();
}
} finally {
ps.close();
}
} finally {
con.close();
}
return ename;
}
}
```
### `EmployeeService.java`
```java
package examples.dbcp;
import java.sql.SQLException;
public interface EmployeeService {
public String getEmployeeName(int empno) throws SQLException;
}
```
```
--------------------------------
### Seasar2 Meta-data Configuration Example
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
This XML snippet illustrates how to define meta-data within the Seasar2 DICON configuration. The `` tag is used to associate arbitrary key-value pairs with components or the container itself, providing extensibility.
```xml
111
```
--------------------------------
### Seasar2 DICON Configuration for AOP and Component Loading
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
This example demonstrates loading a Seasar2 container from a DICON file and retrieving components like List and Date. It implicitly utilizes AOP configurations defined elsewhere (e.g., in aop.dicon) to apply interceptors to the components.
```java
private static final String PATH =
"examples/dicon/Aop.dicon";
S2Container container = S2ContainerFactory.create(PATH);
List list = (List) container.getComponent(List.class);
list.size();
Date date = (Date) container.getComponent(Date.class);
date.getTime();
date.hashCode();
date.toString();
```
--------------------------------
### Initialize Seasar2 Test Setup
Source: https://github.com/seasarorg/seasar2/blob/master/s2jdbc-gen/src/test/resources/org/seasar/extension/jdbc/gen/internal/generator/GenerateSqlFileTestTest_NoSqlFile.txt
Sets up the Seasar2 testing environment by including the 's2jdbc.dicon' configuration file. This is a standard prerequisite for tests utilizing Seasar2's JDBC features.
```java
protected void setUp() throws Exception {
super.setUp();
include("s2jdbc.dicon");
}
```
--------------------------------
### Seasar2 Component Annotation (Tiger)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Example of using Tiger annotations for Seasar2 component definition. This approach is suitable for Java 5 and later environments, allowing inline configuration of component properties like name, instance type, and auto-binding.
```java
@Component(name="xxx", instance=InstanceType.PROTOTYPE,
autoBinding=AutoBindingType.PROPERTY)
public class Xxx {
...
}
```
--------------------------------
### Seasar2 Binding Annotation Examples
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
Demonstrates the usage of the @Binding annotation for property injection in Seasar2. Includes examples for Tiger, Backport175, and constant-based configurations, showcasing different ways to specify binding types and values.
```java
@Binding("aaa2")
public void setAaa(String aaa) {
...
}
@Binding(bindingType=BindingType.NONE)
public void setBbb(String bbb) {
...
}
@Binding
public void setCcc(String ccc) {
...
}
```
```java
/**
* @org.seasar.framework.container.annotation.backport175.Binding("aaa2")
*/
public void setAaa(String aaa) {
...
}
/**
* @org.seasar.framework.container.annotation.backport175.Binding(bindingType="none")
*/
public void setBbb(String bbb) {
...
}
/**
* @org.seasar.framework.container.annotation.backport175.Binding
*/
public void setCcc(String ccc) {
...
}
```
```java
public static final String aaa_BINDING = "aaa2";
public static final String bbb_BINDING = "bindingType=none";
public static final String ccc_BINDING = null;
public void setAaa(String aaa) {
...
}
public void setBbb(String bbb) {
...
}
public void setCcc(String ccc) {
...
}
```
--------------------------------
### InitMethod Annotation (Tiger and Constant)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/DIContainer.html
Illustrates the @InitMethod annotation for specifying initialization methods in Seasar2 components, as an alternative to the initMethod tag. This example shows the Tiger annotation and its constant counterpart.
```java
public class Xxx {
// ...
@InitMethod
public void init() {
// ...
}
}
```
```java
public static final String INIT_METHOD = "init";
```
--------------------------------
### Retrieve Components from S2Container
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/DIContainer.html
These Java code examples illustrate how to retrieve components from an S2Container. You can get components by their class or by their registered name. The examples show direct retrieval and retrieval using SingletonS2Container for convenience, especially with S2Tiger and Java 5+. Note the potential for `TooManyRegistrationRuntimeException` if multiple components match the key.
```java
Hoge hoge = (Hoge) container.getComponent(Hoge.class);
Hoge hoge = SingletonS2Container.getComponent(Hoge.class);
Hoge hoge = (Hoge) container.getComponent("hoge");
Hoge hoge = SingletonS2Container.getComponent("hoge");
```
--------------------------------
### Stateless Annotation Example in Java
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/ejb3.html
Demonstrates the usage of the @Stateless annotation in Java to define a Stateless SessionBean. It shows how to specify a name or rely on auto-naming.
```java
@Stateless(name="xxx")
public class Xxx {
...
}
```
--------------------------------
### Java Client for Seasar Transaction Example
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/tx.html
A main class to demonstrate how to bootstrap the Seasar container and retrieve a component configured for transaction management. It then invokes the 'foo' method on the Hoge component.
```java
package examples.tx;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class HogeClient {
private static final String PATH =
"examples/tx/HogeClient.dicon";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
Hoge hoge = (Hoge) container.getComponent(Hoge.class);
hoge.foo();
}
}
```
--------------------------------
### Seasar2 FileSystemComponentAutoRegister Configuration
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
This Java code snippet demonstrates configuring `FileSystemComponentAutoRegister` in Seasar2. It shows how to set instance definitions, auto-binding modes, and define patterns for classes to be automatically registered as components, including patterns to ignore.
```java
// Example of setting instanceDef and autoBindingDef (actual values depend on context)
// instanceDef: @org.seasar.framework.container.deployer.InstanceDefFactory@REQUEST
// autoBindingDef: @org.seasar.framework.container.assembler.AutoBindingDefFactory@NONE
// Example of adding class patterns
// autoRegister.addClassPattern("org.example.service", ".*Service");
// autoRegister.addIgnoreClassPattern("org.example.test", ".*Test");
```
--------------------------------
### Seasar2 DICON Configuration Example
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
This DICON snippet demonstrates component definition within a namespace, including including other DICON files and defining components with arguments. It illustrates how namespaces differentiate components with identical names.
```xml
aaa
foo.aaa
```
--------------------------------
### S2Container DI Setup with GreetingMain2 (Java)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
Illustrates how to use S2Container to manage dependencies based on a DICON configuration file. The application reads the configuration to create and inject components, demonstrating externalized DI.
```Java
package examples.di.main;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
import examples.di.GreetingClient;
public class GreetingMain2 {
private static final String PATH =
"examples/di/dicon/GreetingMain2.dicon";
public static void main(String\[\] args) {
S2Container container =
S2ContainerFactory.create(PATH);
GreetingClient greetingClient = (GreetingClient)
container.getComponent("greetingClient");
greetingClient.execute();
}
}
```
--------------------------------
### Seasar2 Component Configuration (String)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Shows how to configure Seasar2 components using string constants, which is a method compatible with older JDK versions. This approach defines component properties as a comma-separated string, offering a concise configuration option.
```java
public static final String COMPONENT =
"name = xxx, instance = prototype, autoBinding = property";
```
--------------------------------
### Java: AOP without dicon file - Example with PointcutImpl, AspectImpl, and AopProxy
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/aop.html
This example demonstrates how to apply AOP without using dicon configuration files in Seasar2. It involves creating a PointcutImpl to specify target methods, an AspectImpl to bind an interceptor and pointcut, and an AopProxy to create the enhanced proxy object.
```java
import org.seasar.framework.aop.Aspect;
import org.seasar.framework.aop.Pointcut;
import org.seasar.framework.aop.impl.AspectImpl;
import org.seasar.framework.aop.impl.PointcutImpl;
import org.seasar.framework.aop.proxy.AopProxy;
import org.seasar.framework.aop.interceptors.TraceInterceptor;
import java.util.Date;
public class AopExample {
public static void main(String[] args) {
// Define a Pointcut that targets the 'getTime' method
Pointcut pointcut = new PointcutImpl(new String[]{"getTime"});
// Create an Aspect using TraceInterceptor and the defined Pointcut
Aspect aspect = new AspectImpl(new TraceInterceptor(), pointcut);
// Create an AopProxy for the Date class with the defined Aspect
AopProxy aopProxy = new AopProxy(Date.class, new Aspect[]{aspect});
// Get the enhanced proxy object
Date proxy = (Date) aopProxy.create();
// Call the target method on the proxy
System.out.println(proxy.getTime());
}
}
```
--------------------------------
### Java POJO Implementation for Transaction Example
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/tx.html
Provides a basic implementation of the Hoge interface. This class contains the business logic that will be executed within a transaction context.
```java
package examples.tx;
public class HogeImpl implements Hoge {
public void foo() {
System.out.println("foo");
}
}
```
--------------------------------
### Seasar2 Hotswap Main Application
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
The main class to run the Seasar2 hotswap example. It configures the container, retrieves the Greeting component, demonstrates its usage, and allows for interaction to trigger code modification and re-evaluation.
```java
package examples.hotswap.main;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
import examples.hotswap.Greeting;
public class GreetingMain {
private static final String CONFIGURE_PATH =
"examples/hotswap/dicon/s2container.dicon";
private static final String PATH =
"examples/hotswap/dicon/hotswap.dicon";
public static void main(String[] args) throws Exception {
S2ContainerFactory.configure(CONFIGURE_PATH);
S2Container container = S2ContainerFactory.create(PATH);
System.out.println("hotswapMode:" + container.isHotswapMode());
container.init();
try {
Greeting greeting = (Greeting) container
.getComponent(Greeting.class);
System.out.println(greeting.greet());
System.out.println("Let's modify GreetingImpl, then press ENTER.");
System.in.read();
System.out.println("after modify");
System.out.println(greeting.greet());
} finally {
container.destroy();
}
}
}
```
--------------------------------
### Creating a Mock Interface with MockInterceptor
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/testtech.html
This example demonstrates how to create a mock interface using MockInterceptor in Seasar. It shows setting specific return values for methods like `greeting()` and `echo()` and then creating a proxy object of the `Hello` interface. The code also illustrates how this can be configured within a Seasar component definition.
```java
MockInterceptor mi = new MockInterceptor();
mi.setReturnValue("greeting", "Hello");
mi.setReturnValue("echo", "Hoge");
Hello hello = mi.createProxy(Hello.class);
```
```xml
"greeting"
"Hello"
"echo"
"Hoge"
```
--------------------------------
### Multiple Includes and Component Definition - XML
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
An example showing multiple S2Container definition files being included, followed by a direct component definition within the same file.
```XML
```
--------------------------------
### Seasar2 DI Configuration (DICON)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Defines components and their dependencies using Seasar2's DICON XML format. This file specifies which Java classes implement components and how they are wired together, such as setting properties on one component from another.
```xml
greeting
```
--------------------------------
### Seasar2 Component Annotation (backport175)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Illustrates Seasar2 component definition using backport175 annotations for compatibility with JDK 1.4. This method requires the backport175 library and uses a specific annotation format for configuring component metadata.
```java
/**
* @org.seasar.framework.container.annotation.backport175.Component(
* name = "xxx",
* instance = "prototype",
* autoBinding = "property")
*/
public class Xxx {
...
}
```
--------------------------------
### Java Test Setup and FindAll with Seasar2 JDBC
Source: https://github.com/seasarorg/seasar2/blob/master/s2jdbc-gen/src/test/resources/org/seasar/extension/jdbc/gen/internal/generator/GenerateEntityTestTest_NoId.txt
This Java code demonstrates setting up a test environment using S2TestCase and the 's2jdbc.dicon' configuration. It also includes a test method to retrieve all records of a Ddd entity using jdbcManager.
```java
package org.seasar.extension.jdbc.gen.internal.generator;
import javax.annotation.Generated;
import org.seasar.extension.jdbc.JdbcManager;
import org.seasar.extension.unit.S2TestCase;
/**
* {@link Ddd}のテストクラスです。
*
*/
@Generated(value = {"S2JDBC-Gen test-0.0.1", "org.seasar.extension.jdbc.gen.internal.model.EntityTestModelFactoryImpl"}, date = "2009/04/01 13:12:11")
public class DddTest extends S2TestCase {
private JdbcManager jdbcManager;
/**
* 事前処理をします。
*
* @throws Exception
*/
@Override
protected void setUp() throws Exception {
super.setUp();
include("s2jdbc.dicon");
}
/**
* 全件取得をテストします。
*
* @throws Exception
*/
public void testFindAll() throws Exception {
jdbcManager.from(Ddd.class).getResultList();
}
}
```
--------------------------------
### Java Unit Test for AaaService
Source: https://github.com/seasarorg/seasar2/blob/master/s2jdbc-gen/src/test/resources/org/seasar/extension/jdbc/gen/internal/generator/GenerateServiceTestTest.txt
This Java code defines a unit test class for AaaService using S2TestCase from Seasar. It includes a setUp method to include application configurations and a testAvailable method to verify service instantiation.
```java
package hoge.service;
import javax.annotation.Generated;
import org.seasar.extension.unit.S2TestCase;
/**
* {@link AaaService}のテストクラスです。
*
*/
@Generated(value = {"S2JDBC-Gen test-0.0.1", "org.seasar.extension.jdbc.gen.internal.model.ServiceTestModelFactoryImpl"}, date = "2009/04/01 13:12:11")
public class AaaServiceTest extends S2TestCase {
private AaaService aaaService;
/**
* 事前処理をします。
*
* @throws Exception
*/
@Override
protected void setUp() throws Exception {
super.setUp();
include("app.dicon");
}
/**
* {@link #aaaService}が利用可能であることをテストします。
*
* @throws Exception
*/
public void testAvailable() throws Exception {
assertNotNull(aaaService);
}
}
```
--------------------------------
### Configure ComponentAutoRegister with Class Pattern and Reference
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
This example shows how to configure a generic ComponentAutoRegister to add a specific class as a reference and define a pattern for auto-registering classes within a package.
```xml
@aaa.bbb.ccc.ddd.HogeImpl@class
"aaa.bbb"
".*Impl"
```
--------------------------------
### Simple Component Definition in S2Container XML
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
A minimal example of defining a component within an S2Container's .dicon file. It specifies a component named 'hoge' and links it to the implementation class 'examples.dicon.HogeImpl'. This demonstrates how to register a specific component for dependency injection.
```xml
```
--------------------------------
### Java POJO Interface for Transaction Example
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/tx.html
Defines a simple Java interface for demonstrating transaction management. This interface has a single method 'foo' that will be managed by the transaction aspects.
```java
package examples.tx;
public interface Hoge {
public void foo();
}
```
--------------------------------
### Configure ComponentAutoRegister in Seasar2
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Configure ComponentAutoRegister for file system component auto-registration. This class allows specifying instance and binding types, and defining custom naming strategies. It also supports adding class patterns to include or ignore during registration.
```java
org.seasar.framework.container.autoregister.ComponentAutoRegister
```
--------------------------------
### Define Greeting Interface in Java
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/ejb3.html
Defines the Greeting interface, which declares a method to return a greeting string. This is a fundamental part of the example's communication flow.
```java
package examples.ejb3;
public interface Greeting {
String greet();
}
```
--------------------------------
### OGNL Expression Examples
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/DIContainer.html
Demonstrates various ways to use OGNL expressions for configuring components in Seasar2. This includes string and character literals, numeric and boolean values, constructor calls, static method invocations, class references, and component method calls.
```ognl
"hoge"
```
```ognl
'a'
```
```ognl
123
```
```ognl
true
```
```ognl
new java.util.Date(0)
```
```ognl
@java.lang.Math@max(1, 2)
```
```ognl
@java.lang.String@class
```
```ognl
hoge.toString()
```
--------------------------------
### Configure FileSystemComponentAutoRegister (XML)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
This example shows the XML configuration for FileSystemComponentAutoRegister in Seasar2. It sets properties like instanceDef, autoBindingDef, and autoNaming, and uses addClassPattern and addIgnoreClassPattern methods to control which classes are auto-registered.
```xml
@org.seasar.framework.container.deployer.InstanceDefFactory@REQUEST
@org.seasar.framework.container.assembler.AutoBindingDefFactory@NONE
@org.seasar.framework.container.autoregister.ExampleAutoNaming@
"com.example.myapp.service"
"[A-Z]*.class"
"com.example.myapp.service.impl"
"_$$_javassist_.*"
```
--------------------------------
### Configure JarComponentAutoRegister in Seasar2
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Configure JarComponentAutoRegister to automatically register components from specified JAR files. It allows filtering JARs by name, setting a reference class for path resolution, and defining instance and binding types. You can also specify custom naming strategies.
```java
org.seasar.framework.container.autoregister.JarComponentAutoRegister
```
--------------------------------
### Configure JarComponentAutoRegister (XML)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/DIContainer.html
This XML configuration illustrates the setup for JarComponentAutoRegister in Seasar2. It specifies jar file names, reference classes, instance definitions, auto-binding definitions, and auto-naming components. It also includes methods for adding and ignoring class patterns within JAR files.
```xml
"myapp-dao*"
@org.aopalliance.intercept.MethodInterceptor@class
@org.seasar.framework.container.deployer.InstanceDefFactory@PROTOTYPE
@org.seasar.framework.container.assembler.AutoBindingDefFactory@AUTO
@org.seasar.framework.container.autoregister.ExampleAutoNaming@
"org.seasar.example.dao"
"%sDao.class"
"org.seasar.example.dao.impl"
"%sImpl.class"
```
--------------------------------
### S2JUnit4 Sample Configuration (EmployeeDaoImplTest.dicon)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/S2JUnit4.html
This XML configuration file sets up the S2JUnit4 testing environment for EmployeeDaoImpl. It includes necessary components like javaee5.dicon and configures the EmployeeDaoImpl with a BasicSelectHandler for database queries. It demonstrates how to define SQL statements and map results to Employee beans.
```xml
"SELECT e.empno, e.ename, e.deptno, d.dname FROM emp e, dept d
WHERE e.empno = ? AND e.deptno = d.deptno"
@examples.s2junit4.Employee@class
```
--------------------------------
### Execute Single SQL Update with Seasar2 JDBC
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/s2jdbc_base.html
This Java code demonstrates how to perform a single SQL UPDATE statement using Seasar2's `UpdateHandler`. It initializes the Seasar2 container, retrieves an `UpdateHandler` component, and executes an update with provided parameters. Ensure the `Update.dicon` configuration file is present and correctly set up.
```java
package examples.jdbc;
import org.seasar.extension.jdbc.UpdateHandler;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class UpdateClient {
private static final String PATH = "examples/jdbc/Update.dicon";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
container.init();
try {
UpdateHandler handler =
(UpdateHandler) container.getComponent("updateHandler");
int result =
handler.execute(new Object[] { "SCOTT", new Integer(7788)});
System.out.println(result);
} finally {
container.destroy();
}
}
}
```
--------------------------------
### Configure BasicSelectHandler with BeanListResultSetHandler
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/s2jdbc.html
This DICON configuration defines a BasicSelectHandler component for executing SQL queries. It specifies the SQL statement to run and configures it to use a BeanListResultSetHandler to map the results to a list of Employee objects. This is a common setup for fetching multiple records into Java beans.
```xml
"SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno FROM emp"
@examples.jdbc.Employee@class
```
--------------------------------
### Configure TraceInterceptor for ArrayList and Date in Seasar2
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
This snippet shows how to configure the TraceInterceptor for `java.util.ArrayList` and `java.util.Date` components within a Seasar2 DICON file. The interceptor is applied to all methods of ArrayList and specific methods (`getTime`, `hashCode`) of Date, demonstrating AOP configuration.
```xml
traceInterceptor
0
traceInterceptor
```
--------------------------------
### DelegateInterceptor: Method Delegation Example
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/aop.html
Demonstrates how DelegateInterceptor can be configured to delegate method invocations to another component, optionally mapping specific method names. It requires setting a target component and can map original method names to target method names.
```Java
DelegateInterceptor delegate = new DelegateInterceptor();
Object foo = /* obtain foo component */;
delegate.setTarget(foo);
delegate.addMethodNameMap("bar", "bar2");
```
--------------------------------
### Perform Batch SQL Updates with Seasar2 JDBC
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/s2jdbc_base.html
This Java code shows how to execute multiple SQL UPDATE statements in a batch using Seasar2's `BatchHandler`. It configures the container to use `BasicBatchHandler`, prepares a list of parameter arrays for each update, and executes them. This is efficient for updating multiple rows with similar SQL statements.
```java
package examples.jdbc;
import java.util.ArrayList;
import java.util.List;
import org.seasar.extension.jdbc.BatchHandler;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class BatchClient {
private static final String PATH = "examples/jdbc/Batch.dicon";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
container.init();
try {
BatchHandler handler =
(BatchHandler) container.getComponent("batchHandler");
List argsList = new ArrayList();
argsList.add(new Object[] { "SMITH", new Integer(7369)});
argsList.add(new Object[] { "SCOTT", new Integer(7788)});
int result = handler.execute(argsList);
System.out.println(result);
} finally {
container.destroy();
}
}
}
```
--------------------------------
### Configure InterType for Property Interception in Seasar2
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
This DICON configuration demonstrates the use of `interType` to apply a property-based interceptor (`aop.propertyInterType`) to a custom `examples.Hoge` component. It shows how to include other DICON files and define custom component behaviors.
```xml
aop.propertyInterType
```
--------------------------------
### Select Single Employee Bean using Seasar2 JDBC
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/s2jdbc_base.html
This snippet demonstrates how to configure and use Seasar2's BasicSelectHandler with BeanResultSetHandler to fetch a single employee record as a Java Bean. It requires the 'j2ee.dicon' and defines the SQL query and the target Employee class for mapping. The client code initializes the Seasar2 container, retrieves the handler, executes the query with a parameter, and prints the resulting Employee object.
```xml
"SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno
FROM emp WHERE empno = ?"
@examples.jdbc.Employee@class
```
```java
package examples.jdbc;
import org.seasar.extension.jdbc.SelectHandler;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class SelectBeanClient {
private static final String PATH =
"examples/jdbc/SelectBean.dicon";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
container.init();
try {
SelectHandler handler =
(SelectHandler) container.getComponent("selectBeanHandler");
Employee result = (Employee) handler.execute(
new Object[]{new Integer(7788)});
System.out.println(result);
} finally {
container.destroy();
}
}
}
```
--------------------------------
### Implement QualifiedAutoNaming in Seasar2
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
Implement the QualifiedAutoNaming interface for more sophisticated component naming. It allows removing package prefixes and class suffixes, and applying custom replacement rules. Unlike DefaultAutoNaming, it retains the capitalization of the first letter of the generated component name.
```java
org.seasar.framework.container.autoregister.QualifiedAutoNaming
```
--------------------------------
### Java Unit Test for Employee DAO with Seasar2
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/S2Unit.html
This Java code snippet demonstrates a unit test for an Employee DAO implementation using Seasar2's S2TestCase. It includes test setup, transactional test execution, and data verification against an Excel file. The test relies on Seasar2's dependency injection and testing utilities.
```java
package test.examples.unit;
import org.seasar.extension.dataset.DataSet;
import org.seasar.extension.dataset.DataTable;
import org.seasar.extension.dataset.impl.DataTableImpl;
import org.seasar.extension.unit.S2TestCase;
import examples.unit.Employee;
import examples.unit.EmployeeDao;
public class EmployeeDaoImplTest extends S2TestCase {
private EmployeeDao dao_;
public EmployeeDaoImplTest(String arg0) {
super(arg0);
}
public void setUp() {
include("j2ee.dicon");
include("examples/unit/EmployeeDao.dicon");
}
public void testGetEmployeeTx() throws Exception {
readXlsWriteDb("getEmployeePrepare.xls");
Employee emp = dao_.getEmployee(9900);
DataTable table = new DataTableImpl("emp");
table.setupColumns(Employee.class);
table.copyFrom(emp);
DataSet dataSet = readXls("getEmployeeResult.xls");
assertEquals("1", dataSet.getTable("emp"), table);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(EmployeeDaoImplTest.class);
}
}
```
--------------------------------
### Select Single Map with Seasar2 JDBC
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/s2jdbc.html
This Java example demonstrates fetching a single record from the database, represented as a Map. It uses S2Container to get a SelectHandler configured with a specific SQL query and parameters, then prints the resulting map.
```java
package examples.jdbc;
import java.util.Map;
import org.seasar.extension.jdbc.SelectHandler;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class SelectMapClient {
private static final String PATH =
"examples/jdbc/SelectMap.dicon";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
container.init();
try {
SelectHandler handler =
(SelectHandler) container.getComponent("selectMapHandler");
Map result = (Map) handler.execute(
new Object[]{new Integer(7788)});
System.out.println(result);
} finally {
container.destroy();
}
}
}
```
--------------------------------
### Seasar2 DI Configuration with AOP (DICON)
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
An enhanced Seasar2 DICON file that includes AOP configuration. It uses the 'include' tag to import AOP definitions and the 'aspect' tag within component definitions to apply interceptors, such as a trace interceptor.
```xml
aop.traceInterceptor
greeting
aop.traceInterceptor
```
--------------------------------
### Continue Processing After Exception with ThrowsInterceptor
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/aop.html
This example demonstrates how to use ThrowsInterceptor to ensure processing continues to the end of a method even when exceptions are thrown. It involves defining a class that throws exceptions, an interceptor that extends ThrowsInterceptor, a Dicon file for component configuration, and a client class to test the setup.
```java
package examples.aop.throwsinterceptor;
public class Checker {
public void check(String str) {
if (str != null) {
System.out.println(str);
} else {
throw new NullPointerException("null");
}
}
}
```
```java
package examples.aop.throwsinterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.seasar.framework.aop.interceptors.ThrowsInterceptor;
public class HandleThrowableInterceptor extends ThrowsInterceptor {
public void handleThrowable(Throwable t, MethodInvocation invocation)
throws Throwable {
}
}
```
```java
package examples.aop.throwsinterceptor;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
public class AopCheckerClient {
private static String PATH = "examples/aop/throwsinterceptor/Checker.dicon";
public static void main(String[] args) {
S2Container container = S2ContainerFactory.create(PATH);
Checker checker = (Checker) container.getComponent(Checker.class);
checker.check("foo");
checker.check(null);
checker.check("hoge");
}
}
```
--------------------------------
### Seasar2 Request Auto-binding Filter Configuration
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/resources/zh/DIContainer.html
This web.xml configuration shows how to set up the `S2ContainerFilter` in a Java web application using Seasar2. This filter enables automatic binding of request-related objects (HttpServletRequest, HttpServletResponse, HttpSession, ServletContext) to components.
```xml
s2filter
org.seasar.framework.container.filter.S2ContainerFilter
s2filter
/*
```
--------------------------------
### Configure S2ContainerServlet in web.xml
Source: https://github.com/seasarorg/seasar2/blob/master/seasar2/src/site/ja/resources/DIContainer.html
This XML snippet demonstrates how to configure the S2ContainerServlet in the web.xml file. It specifies the servlet class, initialization parameters like configPath and debug, and servlet mapping for URL patterns. The `load-on-startup` tag ensures the servlet is initialized early.
```xml
s2servlet
org.seasar.framework.container.servlet.S2ContainerServlet
configPath
app.dicon
debug
false
1
s2servlet
/s2servlet
```