Results 1 to 7 of 7
Thread: Spring IoC
- 07-08-2011, 08:33 AM #1
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
Spring IoC
Hi all, i develop here an app with spring,jpa,struts2, and i use spring to inject my service objects into struts actions using IoC. As i said before i also use jpa (jpa entities), and i do not know if in order to use a entity into a service class i have to inject this entity into service via IoC also, or i should use new keyword. I know that the new keyword is "bad" and should be avoided. Which is the best practice ?
Thank you.
- 07-08-2011, 09:39 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Why? Where did you pick that idea up from?Java Code:I know that the new keyword is "bad" and should be avoided.
Injecting in a JPA entity doesn't make much sense. Isn't that simply your model onto a database row?
Surely the service should pick that up itself? Possibly via an injected DAO, which does the database accessing and returns the entities.Last edited by Tolls; 07-08-2011 at 09:39 AM. Reason: typo
- 07-08-2011, 09:48 AM #3
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
hi Tolls,
the ideea was from the loose coupling vs tight coupling in OO world.
and, i do not want to inject something into an entity, but inject a Entity instance into a "Service" class. Just like i inject service classes into struts2 actions.
Also i do not use an effective DAO layer, all my database operations are performed from service classes. I did that because this is not such a complex app that require a separate class for that.
- 07-08-2011, 10:01 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Without knowing what it is your system does (or its general design) it's hard to say, but the idea of injecting an entity into a service seems wrong.
An JPA entity is some data from the database (usually). It's going to be different each time a service is run, so why inject it?
How are you going to inject it? In the Spring files?
- 07-08-2011, 10:16 AM #5
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
i just insert an "empty" object, in order to be able to use object from the service class.
ie:
applicationContext.xml
and then in MyServiceImpl i have :Java Code:<bean id="myService" class="com.vs.common.services.MyServiceImpl"> <property name="emService" ref="emailMessageService"/> <property name="eAddress" ref="emailAddress"/> <property name="eMessage" ref="emailMessage"/> </bean>
before saving this info into the database i need to send a notification email.Java Code:@Transactional public class MyServiceImpl extends GenericVsService implements iMyService { private iEmailMessageService emService; private EmailMessage eMessage; //this is a entity private EmailAddress eAddress; // this also // setters and getters..... public Boolean save(String xxx){ try { eAddress.setEmail("adrian.videanu@yahoo.com"); eMessage.setSubject("testSubj"); eMessage.setPriority(1); eMessage.setBody("crocodilu"); eMessage.setEmailAddress(eAddress); emService.sendEmail(eMessage); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } //...... other code here... return true; }
so, here in myService in need these 2 objects : emailMessage and emailAddress in order to be able to send a mail via EmailMessageService. The values of properties for these 2 objects will be determined from xxx String.
the other way that i see thing here is
which way do you think is the best approach... or both are bad :) ?Java Code:public Boolean save(String xxx){ try { emailAddress = new EmailAddress(); emailAddress.setEmail("adrian.videanu@yahoo.com"); emailMessage eMess = new EmailMessage(); eMess.setSubject("testSubj"); eMess.setPriority(1); eMess.setBody("crocodilu"); eMess.setEmailAddress(eAddress); emService.sendEmail(eMess); //.....................Last edited by videanuadrian; 07-08-2011 at 10:29 AM.
- 07-08-2011, 10:33 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
That's nonsense I'm afraid.
Those single entities will be overwritten time and again in a multi-threaded environment, resulting in corruption of the data.
Even if you made your services non-singleton (ie prototype) it makes no sense. I have no idea where you have got the idea that "new" is somehow a bad thing.
In any case, wouldn't JPA be thoroughly confused by these shared entities being overwritten?
IoC is not about injecting entities (the things you are doing work on). It's about injecting services and the like that do the work on the entities.
So no...do not do this.
- 07-08-2011, 10:42 AM #7
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
ok, i`ll continue use IoC for injecting services into struts actions (or injecting services into other services), and use "new" inside services when i need to create a new entity.
thanks a lot for the patience that you had with me and my problems :)Last edited by videanuadrian; 07-08-2011 at 10:45 AM.
Similar Threads
-
Spring IoC
By videanuadrian in forum New To JavaReplies: 4Last Post: 07-08-2011, 10:52 AM -
possible overlapping jars in spring annotated mvc/spring security project
By savantics in forum Web FrameworksReplies: 1Last Post: 12-27-2010, 05:21 PM -
spring mvc
By jpnr99 in forum New To JavaReplies: 4Last Post: 10-14-2010, 09:58 AM -
EJB 3.0 VS Spring
By nanaji in forum Advanced JavaReplies: 4Last Post: 01-28-2009, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks