Announcement Announcement Module
Collapse
No announcement yet.
[ CANNOT create a bean after making it as another class ] HELP !!! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [ CANNOT create a bean after making it as another class ] HELP !!!

    The following Approach I code works fine.

    (Approach I). : SimplePublicUserShareManager class
    Code:
    @Service
    public class SimplePublicUserShareManager implements PublicUserShareManager {
    	@Autowired private UserDao userDao; 
    	@Autowired private EmailManager emailManager;
    
    	@Override
    	public void emailTest (PrivateShare privateShare, ...) {
    
    		final EmailUser4PublicShare emailUser = new EmailUser4PublicShare();
    
    		User user = userDao.selectUser(privateShare.getUserSessionId());
    		emailUser.setOwnerFirstName(user.getFirstName());
    		emailUser.setOwnerLastName(user.getLastName());
    		
                    ...
    		
    		emailManager.sendEmail4PublicShare(emailUser, emailArrayList, emailMessage);
    Approach II. : SimplePublicUserShareManager class and EmailUtil class

    BUT after I create another class for replacing above red lines of code, it doesn't work and NullPointerException was thrown.


    Code:
    @Service
    public class SimplePublicUserShareManager implements PublicUserShareManager {
    	@Autowired private EmailManager emailManager;
    
    	@Override
    	public void emailTest (PrivateShare privateShare, ...) {
    
    		
    		EmailUtil eu = new EmailUtil();
    		final EmailUser4PublicShare emailUser = eu.createEmailModel(privateShare, ...);		
                    ...
    		
    		emailManager.sendEmail4PublicShare(emailUser, emailArrayList, emailMessage);
    Code:
    public class EmailUtil {
    	@Autowired private UserDao userDao;	
    	
            public final EmailUser4PublicShare createEmailModel(PrivateShare privateShare, ...) {
    		final EmailUser4PublicShare emailUser = new EmailUser4PublicShare();
    		
    		User user = userDao.selectUser(privateShare.getUserSessionId());
                    // This line is the problem!!! userDao cannot be generated over here.
    		
    		emailUser.setOwnerFirstName(user.getFirstName());
    		emailUser.setOwnerLastName(user.getLastName());
    		emailUser.setOwnerId(user.getId());
    		
    		return emailUser;
    		
    	}
    I really don't know why Approach II doesn't work.
    I also tried a generate userDao in EmailUtil manully as follows (Approach III), but this also doesn't work

    (Approach III).
    Thus EmailUtil cannot create userDao in EmailUtil.java.
    Why?????? Could someone please help?


    Approach III.
    Code:
    public class EmailUtil {
    	
       public final EmailUser4PublicShare createEmailModel(PrivateShare privateShare, ...) {
    		final EmailUser4PublicShare emailUser = new EmailUser4PublicShare();
    		
                    JdbcUserDao userDao = new JdbcUserDao();
                    // even cannot manually generate userDao instead of using autowiring    
    
    		User user = userDao.selectUser(privateShare.getUserSessionId());
                    // userDao cannot be generated over here.
    		
    		emailUser.setOwnerFirstName(user.getFirstName());
    		emailUser.setOwnerLastName(user.getLastName());
    		emailUser.setOwnerId(user.getId());
    		
    		return emailUser;
    		
    	}
    Why Approach II and Approach III are not working? Anyone knows?

  • #2
    I found the reason after googling for a while. So I answer to myself to share this to others who are beginners like me.
    POJO (EmailUtil) cannot create Spring bean (UserDao).
    So there are two options to make this work.
    1. make EmailUtil as a Spring bean OR
    2. Need to implelment ApplicationContextAware in the class SimplePublicUserShareManager, and
    get a spring bean from EmailUtil : for more detail, go to this link http://sujitpal.blogspot.com/2007/03...gacy-code.html

    Hope this help you guys.

    Comment

    Working...
    X