Quoting Niclas Hedhman <[email protected]>:
Are you talking about LifeCycle or Initializable??
They are handled slightly differently. First of all, LifeCycle methods
are only called upon creation/deletion of the Entity, not the instance
representing the entity in memory. Also, I vaguely recall that for
non-Entities, LifeCycle has no meaning.
I also recall that unlike other methods, LifeCycle methods are called
on ALL mixins of the Entity (need to verify this, since *I* normally
limit LifeCycle to a single Mixin.).
On the other hand, Initializable is called (if defined) when a Mixin
(that implements it) is instantiated.
Does that help??
The test-case is attached. When you run it, only
[main] INFO
org.qi4j.api.entity.LifecycleMixinTest$SomeEntityMixinWithLCAndProperty -
create called.
will get printed. The test "doTestWithLCAndProperty" will pass, but
the test "doTestWithJustLC" will fail.
/*
* Copyright (c) 2010, Stanislav Muhametsin. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.qi4j.api.entity;
import org.junit.Test;
import org.qi4j.api.common.QualifiedName;
import org.qi4j.api.injection.scope.State;
import org.qi4j.api.injection.scope.This;
import org.qi4j.api.mixin.Mixins;
import org.qi4j.api.property.Property;
import org.qi4j.api.property.StateHolder;
import org.qi4j.api.unitofwork.UnitOfWork;
import org.qi4j.bootstrap.AssemblyException;
import org.qi4j.bootstrap.ModuleAssembly;
import org.qi4j.entitystore.memory.MemoryEntityStoreService;
import org.qi4j.spi.uuid.UuidIdentityGeneratorService;
import org.qi4j.test.AbstractQi4jTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Stanislav Muhametsin
*/
public class LifecycleMixinTest extends AbstractQi4jTest
{
public interface SomeEntity
{
public Property<String> someProperty();
}
@Mixins(SomeEntityMixinWithJustLC.class)
public interface SomeEntityWithJustLC
extends SomeEntity, EntityComposite
{
}
@Mixins(SomeEntityMixinWithLCAndProperty.class)
public interface SomeEntityWithLCAndProperty
extends SomeEntity, EntityComposite
{
}
public static abstract class SomeEntityMixinWithJustLC
implements SomeEntity, Lifecycle
{
public static final Logger LOGGER = LoggerFactory.getLogger(
SomeEntityMixinWithJustLC.class );
@This
private SomeEntity _me;
@Override
public void create()
throws LifecycleException
{
LOGGER.info( "create called." );
this._me.someProperty().set( "Some value" );
}
@Override
public void remove()
throws LifecycleException
{
}
}
public static abstract class SomeEntityMixinWithLCAndProperty
implements SomeEntity, Lifecycle
{
public static final Logger LOGGER = LoggerFactory.getLogger(
SomeEntityMixinWithLCAndProperty.class );
@State
private StateHolder _state;
@This
private SomeEntity _me;
@Override
public void create()
throws LifecycleException
{
LOGGER.info( "create called." );
this._me.someProperty().set( "Some value" );
}
@Override
public void remove()
throws LifecycleException
{
}
@Override
public Property<String> someProperty()
{
return this._state.getProperty( QualifiedName.fromClass(
SomeEntity.class, "someProperty" ) );
}
}
@Override
public void assemble( ModuleAssembly module )
throws AssemblyException
{
module.addServices( MemoryEntityStoreService.class,
UuidIdentityGeneratorService.class );
module.addEntities( SomeEntityWithJustLC.class,
SomeEntityWithLCAndProperty.class );
}
@Test
public void doTestWithJustLC()
{
UnitOfWork uow = this.unitOfWorkFactory.newUnitOfWork();
try
{
EntityBuilder<SomeEntityWithJustLC> builder = uow.newEntityBuilder(
SomeEntityWithJustLC.class );
builder.newInstance();
}
finally
{
uow.discard();
}
}
@Test
public void doTestWithLCAndProperty()
{
UnitOfWork uow = this.unitOfWorkFactory.newUnitOfWork();
try
{
EntityBuilder<SomeEntityWithLCAndProperty> builder = uow
.newEntityBuilder( SomeEntityWithLCAndProperty.class );
builder.newInstance();
}
finally
{
uow.discard();
}
}
}
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev