Announcement Announcement Module
Collapse
No announcement yet.
Extending base classes with DI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extending base classes with DI

    I am trying to create a base library with controllers and entities that can be extended for several sites and am not sure if I am doing this correctly as I am new to Spring and DI, etc. Any help would be appreciated.

    Base Controller

    Code:
    @Controller
    public class MyAccountController {
    	public User user;
    
        @Inject
    	@Named("User")
    	public void setUser(User user) {
    		this.user = user;
    	}
    
        public MyAccountController () {}
    	
    	public MyAccountController (User user) {
    		this.user = user;
    	}
    
        public void preProcessSignup(Model model, HttpServletRequest request, HttpSession session, User user) {}
    	
    	@RequestMapping(value = "/processSignup.do", method = { RequestMethod.GET, RequestMethod.POST })
    	public String doProcessSignup(Model model, HttpServletRequest request, HttpSession session) {
    		user = new User();
    		
    		preProcessSignup(model, request, session, user);
    	
    		user.setEmail("[email protected]");
    		user.setUsername("username");
    		user.setPassword("password");
    
    		userDAO.saveUser(user);
    
    		return "signup";
    	}
    }
    Sub Controller

    Code:
    @Controller
    public class SubMyAccountController extends MyAccountController {
    	public SubMyAccountController() {
    		super();
    	}
    
    	public SubMyAccountController(User user) {
    		super(user);
    	}
    
    	@Inject
    	@Named("User")
    	public void setUser(User user) {
    		this.user = user;
    	}
    
    	@Override
    	public void preProcessSignup(Model model, HttpServletRequest request, HttpSession session, User user) {
    		user.setRegistrationDate("12/27/2012");
    	}
    Base Class

    Code:
    @Entity
    public class User implements Serializable {
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	protected String id;
    
    	protected String username;
    	protected String email;
    	protected String password;
    	
    	//getters and setters, etc.
    }
    Sub Class

    Code:
    @Entity
    public class SubUser extends User {
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	public String id;
    	
    	private String registrationDate;
    
    	//getters and setters, etc.
    }
    Application Context
    Code:
    <bean id="User" class="a.b.c.SubUser" />
    I am using the preProcessSignup method to allow for overriding and setting the extended properties for the subUser object.
    However, it is not casting the User object to the subUser and does not recognize the registrationDate property.

    What am I doing wrong? Is there a better way to do this? I just want to have base classes / controllers that hold all functionality and data common to these sites. Then extend those classes with pre- and post- methods to implement and differences.
    This is probably a basic question, but I am struggling with it.

    Thanks for any help or suggestions
Working...
X