I am planning to take my some of my values from db like.
The value will be stored like key value pair in DB(same like properties file).
Could you please let me whether spring has any support for storing key value pair in db like spring property holder?
You can implement your own by extending org.springframework.context.support.AbstractMessageSource and injecting your DAO or using your Hibernate domain class(es) to resolve the messages and then registering it as your messageSource bean in your application context.
Related
When using spring-data-redis caching annotations on a spring-data-jpa repository you can provide a #CacheConfig annotation with the cacheResolver property referencing a custom bean that implements CacheResolver. In this bean you can then amend the cache name that may have been specified in the #Cachable annotation. For instance we use this custom resolver to append the tenant name to the cache in our multi-tenant setup.
We now want to use Redis as a datastore in its own right, not just a cache. When specifying a POJO you want to be stored in Redis you are able to use the spring-data-redis #RedisHash annotation and provide a keyspace as the value property of that annotation. However, this is a constant value and does not seem to be able to be configured. It looks like the next version of spring-data-redis will allow SpEL expressions in the #RedisHash's value property, but it is not clear if that will provide all the functionality we need.
Is there a CacheResolver type class that can be configured for #RedisHashs to allow for custom key space resolving?
We are currently using EclipseLink as our JPA implementation for the new code. The old code did multitenancy with the solution description as this:
every customer (tenant) must have its own database - separate JDBC resources.
Information about selected tenant is carried across method invocations in ThreadLocal
TENANT variable, declared in com.aaa.Instance.java class.
JDBC resources are handled by com.aaa.TenantDataSource.
During initialization, TenantDataSource looks for DataSources registered
within java:comp/env/jdbc JNDI context and checks, if their names starts with 'abc_'.
References to DataSources are stored in internal map using tenant code from JNDI
resource name (DataSource named 'abc_xx' is registered as DataSource for 'xx' instance).
TenantDataSource examines value of Instance.TENANT variable when 'getConnection'
is invoked, for using appropriate DataSource.
TenantDataSource falls back to default DataSource if Instance.TENANT has null
value. This could be default behavior for single-tenant deployments.
Now we are using EclipseLink, I googled to see if I can implement multitenant as required. It seems EclipseLink can do:
* Single-table multi-tenancy
* table-per-tenant
* (VDP) multi-tenancy
not like Hibernate which supports database per tenant.
Could someone tell if my research is right?
Thanks
I have a MultiMap in Hazelcast with a List<SomeObject> for each String key and want to persist each list with something like
IExecutorService executorService = hzInstance.getExecutorService("commonExec");
//...
executorService.executeOnKeyOwner(new PersistTask(key), key);
but the PersistTask object need another object defined in the Spring context. If I understand correctly, the instance of PersistTask is created, then serialized and sent to the key owner and the executed there. How do I reference the local Spring context? I think I could try to access it statically but I feel filthy for doing that. Is there a better way? For the record, I'm using Hazelcast 3.5.3 and Spring 4.2.3. Thanks in advance.
Seems that Hazelcast provide some kind of dependency injector. Your object (in my case, PersistTask) must implement HazelcastInstanceAware and it will be injected the local Hazelcast instance after deserialization. You can add objects in a user context that can be retrieved from the Hazelcast instance. The Hazlcast blog has a nice example.
There is a better and more object-oriented solution: you can add the annotation #SpringAware to your class (in my case PersistTask) and it will be initialized using the Spring context after deserialization. All the Spring annotations and interfaces (#Autowired and InitializingBean for example) will be processed and the dependencies will be injected. For this to work, you must define a ManagedContext in the Hazelcast instance. The SpringManagedContext is what you need. This can be specified in the XML config like this:
<hz:hazelcast id="hzexample">
<hz:config>
<hz:spring-aware /> <!-- this line here !!! -->
<hz:instance-name>hzexample</hz:instance-name>
...
The example in the official blog is this but it is formatted badly.
I've a doubt about Spring session bean. Let me try to explain what I need and what I did. I need to store on a session variable (in that case a Bean) the user_id so, when I need to create some record on db I can keep track of who did what.
To do that, for first, I created a bean and, second, I modified my application context in that way:
<bean id="UserInfo" class="net.agdev.session.UserInfo" scope="session">
<aop:scoped-proxy/>
</bean>
I read that using this :
ContextLoader.getCurrentWebApplicationContext().getBean("UserInfo");
is ppossible to access to the bean, but it's not yet clear how to fill that bean..
I tryed to read on Spring documentation how to initalize the bean and how to recall on my Class controller but I didn't find anything.
Could you suggest where to find an example or a tutorial to do that?
many thanks!
Andrea
You mean how to get the user_id into the session bean? Depending on your application this should probably happen right after the user "logged in". Meaning, if you for instance have a login webflow or controller, set the user_id in your session bean within that webflow or controller.
So if I understood your context correctly this has only very little to do with Spring itself and mostly with your application :-)
If you want other aspects of your bean initialized for instance from operations on other services you could set an init-method on the bean definition as detailed here.
By aspect programming like AspectJ. You have to set some trigger, for example after an user does something you have to read your bean and fill it the operation info that have been performed by the user.
You can use #annotation to define trigger or you can do it by spring xml file. I think you have to use an application context bean and not a session bean.
Similar topics have been covered in other threads, but I couldn't find a definitive solution to my problem.
What we're trying to achieve is to design a web app which is able to:
read a datasource configuration at startup (an XML file containing multiple datasource definitions, which is placed outside the WAR file and it's not the application-context or hibernate configuration file)
create a session factory for each one of them (considering that each datasource is a database with a different schema)
switch at runtime to different datasources based on user input (users can select which datasource they want to use)
provide the correct dao object to manage user requests.
At the moment we have a DAO Manager object which is able to read the datasource configuration file and instantiate multiple session factories, saving them in a map. Each session factory is created with a configuration containing the proper hibernate mapping classes (different for each database schema). Moreover we have multiple DAO interfaces with their implementations, used to access "their database".
At this point we would need a way to get from the DAO Manager a specific DAO object, containing the right session factory attached, all based on the user request (basically a call from the above service containing the datasource id or a custom datasource object).
Ideally the service layer should use the DAO Manager to get a DAO object based on the datasource id (for instance), without worrying about it's actual implementation: the DAO Manager would take care of it, by creating the correct DAO object and injecting in it the right session factory, based on the datasource id.
My questions are:
Is this a good approach to follow?
How can I use Spring to dynamically inject in the DAO Manager multiple DAO implementations for each DAO interface?
Once the session factories are created, is there a way to let Spring handle them, as I would normally do with dependency injection inside the application-context.xml?
Would the 2nd Level Cache still work for each Session Factory?
Is this a good approach to follow?
It's probably the only possible approach. So, yes.
How can I use Spring to dynamically
inject in the DAO Manager multiple DAO
implementations for each DAO
interface?
Dynamically? I thought you wanted to do it at startup time. If so, just provide an accessor with a list or array:
public void setMyDaos(List<Mydao> daos){
this.daos = daos;
}
Once the session factories are
created, is there a way to let Spring
handle them, as I would normally do
with dependency injection inside the
application-context.xml?
This one's tough. I'd say you will probably have to store your sessionFactory bean in scope=session