Nov 08 2012

Learning Spring, part VI

Category: TechnicalIuliana @ 15:44

This won’t be a post  about a problem or a question, but about an observation.

When I took the spring Core course in Belgrade this June, in the Chapter about data access the jdbcTemplate instance was created like this:
Java code:

//random DAO class
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

Xml configuration:

<bean id="dataSource" class="..." />

After that I read Spring in Action, then the Spring reference and everywhere when given an example on how to use jdbcTemplate, the instance was created and injected like that.

And I am confused. If jdbcTemplate instance is thread-safe once configured, is recommended to not create one for each use and is stateless (does not maintain any conversational state) why don’t we just create it as a singleton bean and use it as such?
Sample of my code:

//random DAO class
private JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

Xml configuration:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
   <constructor-arg ref="dataSource" />
</bean>

Tags: ,


Nov 04 2009

Decorator/Wrapper Pattern(The part that you will not forget)

Category: English posts,TechnicalIuliana @ 18:15


After I wrote this post about the decorator pattern I kept thinking of an example of this design pattern that you will not forget that easily. Usually the typical UML Diagram for a medium complexity decorator pattern example looks like this:

And the implementation you cand find all over the internet uses the exact class/inteface names and it does not connect with anything real. And this is the main problem. To be able to remember something you have to be able to connect it to something real and present in your life. So I came up with this example.(UML Diagram created in Magic Draw 16.6, demo version)
Continue reading “Decorator/Wrapper Pattern(The part that you will not forget)”

Tags:


Oct 29 2009

Decorator/Wrapper Pattern

Category: English posts,TechnicalIuliana @ 1:07

This week I went to another java interview. Yes, another one, because I still have no job. Don’t ask me how that happened because I don’t know. Maybe I just did not find a company that I would love to work at, enough to keep my mouth shut and not piss off the guys at HR. Besides all the simple questions, that I usually fuck up because I do not pay enough attention, at this interview, I was asked to describe “The Decorator Design Pattern”(which is actually a structural pattern by some authors). And of course I did not write one word, because even if lately I read a book about design patterns I did not remember which one was “The Decorator Pattern” and what was it about. Usually when I program I design my own classes, interfaces hierarchy and database, because except the two titan projects I worked at while being part of a medium team, my applications are small, simple and useful only to me and a few of my friends. So like I said, I design and implement everything and I don’t care that some big hot-shot developers have bagged and tagged some programming techniques and wrote books about it.

But sometimes when you are asked about these programming techniques at interviews you feel frustrated that you did not learn your programming theory part.This is how I felt Monday morning. So on my way home I decided to start a series of posts on this blog about all “bagged and tagged” programming techniques also called “Design Patterns”. Maybe writing about them will make me remember them and not feel frustrated at an interview ever again.

Continue reading “Decorator/Wrapper Pattern”

Tags: , ,