Posts mit dem Label spring werden angezeigt. Alle Posts anzeigen
Posts mit dem Label spring werden angezeigt. Alle Posts anzeigen

Dienstag, 30. Juni 2009

Hint: Injecting static variable into Spring Beans

Several times I was wondering if it is possible to inject the value of a static variable into spring beans, now I stumbled over the solution:
<util:constant static-field="MyClass.MyStaticVar"/>


Mittwoch, 3. Dezember 2008

Annotation-driven transactions

Instead of programmatically implementing code or configuring Spring applicaton context xml files to manage transactions, it can simply be done by using Spring's transaction annotations. There's really not much to it. All you need to do is add the following to your Spring context:

<tx:annotation-driven />

Additionally ensure that the tx namespace is defined as per the xsi:schemaLocation

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd"
default-lazy-init="true" />


And now to the fun part! Add annotations to your code. I prefer to apply annotations to interfaces. That way all implementations of the interface are transactional.



...and Bob's your uncle!

Dienstag, 25. November 2008

Spring: Get Classpath Resources with Wildcards

With Spring's Resource abstraction it is easy to load resources from the classpath, but what if you need to load many files that follow a certain naming pattern? The following code accomplishes that with the org.springframework.core.io.support.PathMatchingResourcePatternResolver, it supports the same wildcards in the URL like the ApplicationContextLoader:

PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = patternResolver.getResources("classpath:/conf/aggregations/*_agg.xml");

Follower