Results 1 to 7 of 7

Thread: Spring IoC

  1. #1
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Java Code:
    I know that the new keyword is "bad" and should be avoided.
    Why? Where did you pick that idea up from?

    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

  3. #3
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default

    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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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?

  5. #5
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default

    i just insert an "empty" object, in order to be able to use object from the service class.
    ie:
    applicationContext.xml
    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>
    and then in MyServiceImpl i have :
    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;
    }
    before saving this info into the database i need to send a notification email.

    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
    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);
                         //.....................
    which way do you think is the best approach... or both are bad :) ?
    Last edited by videanuadrian; 07-08-2011 at 10:29 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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.

  7. #7
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default

    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

  1. Spring IoC
    By videanuadrian in forum New To Java
    Replies: 4
    Last Post: 07-08-2011, 10:52 AM
  2. Replies: 1
    Last Post: 12-27-2010, 05:21 PM
  3. spring mvc
    By jpnr99 in forum New To Java
    Replies: 4
    Last Post: 10-14-2010, 09:58 AM
  4. EJB 3.0 VS Spring
    By nanaji in forum Advanced Java
    Replies: 4
    Last Post: 01-28-2009, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •