Announcement Announcement Module
Collapse
No announcement yet.
Session bean not injecting into other class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Session bean not injecting into other class

    Hi,

    I am a spring beginner that was working fine but i did some refactoring and it seems to have stopped working. Basically the scenario is that i have a User Bean persisted by hibernate i set this to the variable that is populated by Spring but this value is not injected into other classes uing the session bean.

    So i call UserService.login below which sets the user variable, calling UserService.getUserDetails uses the correct user class.

    But when i call RatingService.addComment the User(UserInterface) object included is just a template method.

    In the spring+DWR doc it says for session beans i should use where the declared class implements an interface which
    HTML Code:
    <aop:scoped-proxy proxy-target-class="false"/>
    I am using DWR framework which converts java<->javascript, as far as i can tell, it is working fine.



    here is my application Context XML file snippet

    Code:
    	<!--
    		Instruct Spring to perform declarative transaction management
    		automatically on annotated classes.
    	-->
    	 <tx:annotation-driven mode="aspectj"/> 
    
    	<!--
    		Simply defining this bean will cause requests to owner names to be saved.
    		This aspect is defined in petclinic.jar's META-INF/aop.xml file.
    		Note that we can dependency inject this bean like any other bean.
    	-->
    	<!-- <bean class="org.springframework.samples.petclinic.aspects.UsageLogAspect" p:historySize="300"/> -->
    	<!--  DWR config -->
    	<dwr:configuration/>
    	<!--  DWR controller -->
    	<dwr:controller id="dwrController" debug="true" />
    
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    
    	<bean id="userBean"  class="com. robmunro.photo.data.User" scope="session" >	
    		<aop:scoped-proxy proxy-target-class="false"/>	
    	</bean>
    	
    	<bean id="photodaoProto" class="com. robmunro.photo.dao.PhotoDAO" scope="prototype">
    		 <constructor-arg ref="entityManagerFactory" index="0"/>
    	</bean>
    	
    	<bean id="photodao" class="com.robmunro.photo.dao.PhotoDAO" scope="request"><aop:scoped-proxy/></bean>
    		<!-- <constructor-arg><ref local="entityManagerFactory"/></constructor-arg> -->	
    	
    	<bean id="ratingdao" class="com. robmunro.photo.dao.RatingDAO" scope="request">	<aop:scoped-proxy/></bean>
    	
    	<bean id="userdao" class="com. robmunro.photo.dao.UserDAO" scope="request"><aop:scoped-proxy/></bean>
    	
    	<bean id="imageProcessor" class="com. robmunro.image.ImageProcessingThread">
    		<property name="photoDAO" ref="photodaoProto"/>
    	</bean>
    	
    	<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    	  <!-- wait 10 seconds before starting repeated execution -->
    	  <property name="delay" value="10000"/>
    	  <!-- run every 50 seconds -->
    	  <property name="period" value="60000"/>
    	  <property name="timerTask" ref="imageProcessor"/>
    	</bean>
    	
    	<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
    	  <property name="scheduledTimerTasks">
    	    <list>
    	      <ref bean="scheduledTask"/>
    	    </list>
    	  </property>
    	</bean>
    
    	<bean id="fileservice" class="com. robmunro.file.FileService" scope="request"><!-- scope="request" -->
    		<property name="photoDao" ref="photodao"/>
    		<property name="ratingDao" ref="ratingdao"/>
    		<dwr:remote javascript="RFileService">
    			<dwr:exclude method="getPhotoDao"/>
    			<dwr:exclude method="setPhotoDao"/>
    			<dwr:exclude method="getRatingDao"/>
    			<dwr:exclude method="setRatingDao"/>
    		   	<dwr:convert type="bean" class="com.robmunro.file.FileService$ExifTag"></dwr:convert>
    		   	<dwr:convert type="bean" class="com.robmunro.file.FileService$ImageData"></dwr:convert>
    		   	<dwr:convert type="bean" class="com.robmunro.photo.data.Photo"></dwr:convert>
    		   	<dwr:convert type="bean" class="com.robmunro.photo.data.Colour"></dwr:convert>
    		   	<dwr:convert type="bean" class="com.robmunro.photo.bean.SearchBean"></dwr:convert>
    		</dwr:remote>
    	</bean>
    	
    	<bean id="ratingservice" class="com. robmunro.rating.RatingService" scope="request" ><!-- scope="request" -->
    		<property name="user" ref="userBean"/>
    		<property name="photoDao" ref="photodao"/>
    		<property name="ratingDao" ref="ratingdao"/>
    		<property name="userDao" ref="userdao" />
    		<dwr:remote javascript="RatingService">
    			<dwr:exclude method="getPhotoDao"/>
    			<dwr:exclude method="setPhotoDao"/>
    			<dwr:exclude method="setUser"/>
    			<dwr:exclude method="getUser"/>
    			<dwr:exclude method="getRatingDao"/>
    			<dwr:exclude method="setRatingDao"/>
    			<dwr:exclude method="getUserDao"/>
    			<dwr:exclude method="setUserDao"/>
    			<dwr:convert type="bean" class="com. robmunro.photo.data.Photo"></dwr:convert>
    			<dwr:convert type="bean" class="com.r obmunro.photo.data.UserInterface"></dwr:convert>
    			<dwr:convert type="bean" class="com. robmunro.photo.data.User"></dwr:convert>
    			<dwr:convert type="bean" class="com. robmunro.photo.data.UserComment"></dwr:convert>
    			<dwr:convert type="bean" class="com. robmunro.photo.data.Comment"></dwr:convert>
    			<dwr:convert type="bean" class="com. robmunro.photo.bean.UserStats"></dwr:convert>
    		</dwr:remote>
    	</bean>
    	
    	<bean id="userservice" class="com. robmunro.user.UserService" scope="request"><!-- scope="request" -->
    		<property name="user" ref="userBean"/> 
    		<property name="userDao" ref="userdao"/>
    		<dwr:remote javascript="UserService">
    			<dwr:exclude method="getUserDao"/>
    			<dwr:exclude method="setUserDao"/>
    			<dwr:exclude method="setUser"/>
    			<dwr:exclude method="getUser"/>
    			<dwr:convert type="bean" class="com. robmunro.photo.bean.ValidationBean"></dwr:convert>
    			<dwr:convert type="bean" class="com. robmunro.photo.data.UserInterface"></dwr:convert>
    			<dwr:convert type="bean" class="com.r obmunro.photo.data.User"></dwr:convert>
    		</dwr:remote>
    	</bean>
    </beans>
    So the UserService has the login method and sets the bean in the session

    Code:
    public class UserService {
    	
    	private UserInterface user;
    	private UserDAO userDao; 
    	
    ...
    	
    	public ValidationBean login(String userName, String password) {
    		ValidationBean vb=new ValidationBean();
    		UserInterface checkUser = userDao.getByUserName(userName);
    		if (checkUser==null) {
    			vb.getMessages().add("No user by that name");
    			return vb;
    		}
    		if (checkUser.getPassword().equals(password)) {
    			vb.getMessages().add("Logged in");
    			vb.setUser(checkUser);
    			this.user = checkUser;
    			//this.userDao.setUser(checkUser);
    		} else {
    			vb.getMessages().add("Login unsuccessful");
    			this.user = new User();
    			//this.userDao.setUser(new User());
    		}
    		return vb;
    	}
    
    	public ValidationBean logout() {
    		this.user=new User();
    		//this.userDao.setUser(new User());
    		ValidationBean vb=new ValidationBean();
    		vb.getMessages().add("Logged out");
    		return vb;
    	}
    	
    	public ValidationBean getUserDetails() {
    		ValidationBean vb=new ValidationBean();
    		if (this.userDao.getSessionUser(this.user).getId()>0) {
    			vb.setUser(this.userDao.getSessionUser(this.user));
    		}else {
    			vb.getMessages().add("No user");
    		}
    		return vb;
    	}
    	...
    	public UserDAO getUserDao() {		
    			return userDao;	}
    	public void setUserDao(UserDAO userDao) {		this.userDao = userDao;	}
    
    	public UserInterface getUser() {		
    		return this.user;	
    	}
    	public void setUser(UserInterface user) {		
    		this.user = user;	
    	}
    	
    	
    }
    This RatingService uses the userBean that should be in the session after login. but the value for user is some sort of template object and data cannot be retreived from it.
    Code:
    
    public class RatingService {
    	
    	private PhotoDAO photoDao; 
    	private RatingDAO ratingDao; 
    	private UserDAO userDao; 
    	
    	private UserInterface user;
    	
    ...
    	public void addComment(  Photo p, String text) {
    		Photo localPhoto = checkForPhoto( p.getPath(),  p.getSource()) ;
    		//if (u==null || u.getId()==null) {
    		//	u=userDao.getDefaultUser();
    		//}
    		//userDao.dispUser("rating user get", this.user);
    		UserComment uc = new UserComment();
    		uc.setComment(new Comment());
    		uc.setUser(this.user);
    		uc.setPhoto(localPhoto);
    		uc.getComment().setComment(text);
    		ratingDao.saveUserComment(uc);
    	}
    	
    	public List<UserComment> getComments(Photo p) {
    		List checkPhotoList = photoDao.selectByPathAndSource(p.getPath(),p.getSource());
    		System.out.println(p.getPath()+":"+p.getSource());
    		if (checkPhotoList.size()>0) {
    			p = (Photo)checkPhotoList.get(0);
    			List<UserComment> commList =  ratingDao.getComments(p);
    			
    			return commList;
    		} else {
    			System.out.println("none");
    			return new ArrayList<UserComment>();
    		}
    	}
    	
    	
    	public UserInterface getUser() {		
    		return user;	
    	}
    	public void setUser(UserInterface user) {		
    		this.user = user;	
    	}
    	public PhotoDAO getPhotoDao() {	return photoDao;}
    	public void setPhotoDao(PhotoDAO photoDao) {	this.photoDao = photoDao;	}
    	public RatingDAO getRatingDao() {		return ratingDao;	}
    	public void setRatingDao(RatingDAO ratingDao) {		this.ratingDao = ratingDao;	}
    	public UserDAO getUserDao() {		return userDao;	}
    	public void setUserDao(UserDAO userDao) {		this.userDao = userDao;	}
    }

  • #2
    Why is everything at least request scoped????? Use a singleton instead of scoping everything for the request.

    I suggest you readup on what session scope does. You cannot simply assign the variable to another instance, that will not and will never going to work. The user bean you see is a proxied object, so need to set the properties onto that instead of reassining it to another object.

    Code:
    this.user = checkUser;
    So basically with a session scoped object NEVER do this.

    Comment


    • #3
      ahh ok. It was working for a while, i guess this was just a fluke.

      so i use BeanUtils.copyProperties to set the data into session variable. and make as much into singletons as possible.

      thanks vry much for your quick reply.

      Comment

      Working...
      X