Tag Archives: spring

@Autowired

after thinking a lot about DIP, it was kinda clear, that we need to refactor some stuff… ok not really the thinking part, more the discusting part… a big beans.xml file sucks… and analysing my beanx.xml showed, that one small component is used everywhere and a lot of injects only where this one class…

first, something about a concept… in my opinion there are two ways for dependency injection… the one where a config file injects classes to others and the one where the class, wishes to get an injection… secondly is @Resource, @Autowired…

and in my project, i thought the one class, which is used almost everywhere should be get by @Autowired… and actually it was an static class, where the key-value configuration out of the db is loaded… so simply refactored it…

and the main problem was: tell spring, that it should autowire!

<context:annotation-config />
<context:component-scan base-package=“[class name]” />

after adding this few lines to the beans.xml, it actually found the [class name] in time and could inject it 🙂

crap config!

mule with spring-beans and autowire…

… doesnt work 🙁

i really think (now partly thought) spring is cool… i used a spring-bean configfile, initialised all the entities and @Autowired them.. pretty cool… reflection on the entities to find and load them… and i think its really readable in java code… whenever you see an @Autowired… it will be created someware completely different and reflected to the position…

but autowiring with mule dont work… or i couldnt figure out how… mule runs its own context and the springcontext is somewhere in there… and the springcontext behaves different… crap… so new solution:

beans.xml

<bean id=”A” class=”ch.michio.spring.A”/>

<bean id=”B” class=”ch.michio.spring.B”>
     <property name=”a” ref=”A”/>
</bean>

so in class B there was an:

@Autowired
private A a;

 now there is a setter:

private A a;
public setA(A a){
     this.a = a;
}

So during initialisation

  1. an A object is created
  2. a B object is created
  3. a in spring bean container found A is set to B

my main problem with this solution is the readability… you need to read the java files AND the beans.xml… bah… 🙁

weaving: spring and eclipselink

What is weaving?

Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime. (http://static.springsource.org/spring/docs/2.5.x/reference/aop.html)

So weaving is used to load objects and manipulate them… so… why? and why did this concern me?

ok in my project we use spring with eclipselink and the idea was to create the entities in spring and just @Autowire them for usage…  but it needed a InstumentalLoadTimeWeaver to do so… and this Weaver needs to run in a special java-agent (spring-agent)… which is really crap on a server… so it really concerned me!

Solution? EclipseLink allows the configuration weaving=false (link)… which just simply disables the dynamic weaving, so no weaver is used and no java-agent is used… hurray…

but… what are the side-effects?

… to weave all applicable class files at build time so that you can deliver pre-woven class files. Consider this option to weave all applicable class files at build time so that you can deliver prewoven class files. By doing so, you can improve application performance by eliminating the runtime weaving step required by dynamic weaving… (http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Weaving)

so its this a solution? i think so 🙂

spring

in my project there are a lot of technologies involved and as a .net-developer everything is new… yeah… its java again 🙂 ok jpa, mule, spring, maven, pojos… and i finally understood the main concept of spring :)… or lets say the part with beans… ok i didnt understand, when (timewise) springbeans ware created… but i think this is kinda less important… ok its starts to be important, when something goes wrong, but can do…

 <bean id=”[objectId]” class=”[className]”>
<constructor-arg>
<value>[value]</value>
</constructor-arg>
</bean>

With this declaration an object of typ className will be generated with the name objectId. The constructor is filled with set values… the big question is: where the hell is this object? Actually i dont know, but it can be get in java with:

@Autowired
[className] [objectId];

done.

mule with spring and testing without mule…

mule is able to import spring beans… as expected 🙂 but to test those… its kinda hard… 🙁

first create an own bean config file separated from the mule-config.xml and include it in mule-config.xml with following tags:

<spring:beans>
<spring:import resource=”beans.xml”/>
</spring:beans>

simple 🙂 now the beans.xml is a simple springbeans config file… with <beans> as root tag and <bean>’s inside. easy too…

and now use following annotation in the unittest:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={“classpath:beans.xml”})

this should now load the bean correctly during test…