Announcement Announcement Module
Collapse
No announcement yet.
Why I can't use IOC in my class?! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why I can't use IOC in my class?!

    Hi everyone:

    I want to inject a interface to my class and use it get a dataList. This interface is :
    Code:
    public interface ForumDAO {
    	public List getForums();
    
    }
    And the implement class is:
    Code:
    public class ForumDAOImpl extends HibernateDaoSupport implements ForumDAO {
        public List getForums(){
        	return this.getHibernateTemplate().find("from Forum");
        }
    }
    I want to use it in my ForumProxy class like this:
    Code:
    public class ForumProxy {
    	private static Log log = LogFactory.getLog(ForumProxy.class);
    	
    	private ForumDAO fdao;
    
    	public void setFdao(ForumDAO fDAO) {
    		this.fdao = fDAO;
    	}
    
    	public ForumDAO getFdao() {
    		return this.fdao;
    	}
    
    	public  Map getCommonListFromParam(String action) {
    
    		log.info("get forumDAO? "+this.getFdao());
    		List list=this.getFdao().getForums();
                                    Map map=new HashMap();
                                    map.put("data",list);
    		return map;
    	}
    }
    in my springapp config file ,I write :
    Code:
    <beans>
    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <!-- results in a setDriverClassName&#40;String&#41; call -->
            <property name="driverClassName">
                <value>org.gjt.mm.mysql.Driver</value>
            </property>
            <property name="url">
                <value>jdbc&#58;mysql&#58;//127.0.0.1&#58;3306/Hibernate</value>
            </property>
            <property name="username">
                <value>root</value>
            </property>
    		<property name="password">
                <value></value>
            </property>
        </bean>
    
    <bean id="MySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
       <property name="mappingResources">
          <list>
            <value>lyo/hotmail/site/bean/Article.hbm.xml</value>
            <value>lyo/hotmail/site/bean/Forum.hbm.xml</value>
          </list>
       </property>
       <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.query.substitutions">true=1 false=0</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.use_outer_join">false</prop>
          </props>
       </property>
       <property name="dataSource"><ref bean="MyDataSource"/></property>
    </bean>
    <bean id="fproxy" class="lyo.hotmail.site.util.ForumProxy">
       <property name="fdao">
          <ref bean="formDao"/>
       </property>
    </bean>
    <bean id="viewSpringForumController" class="lyo.hotmail.site.action.ViewForumcontroller">
    
       </bean>
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/oldForum.spring">viewSpringForumController</prop>
                </props>
            </property>
       
        </bean>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property name="prefix"><value>/WEB-INF/forum/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    <bean id="formDao" class="lyo.hotmail.site.service.ForumDAOImpl">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
    </bean> 
     
    </beans>
    But I get null in my ForumProxy class.The code:log.info("get forumDAO? "+this.getFdao());
    will Print :
    Code:
    get forumDAO? null
    And later throws a NullPointerException in "this.getFdao().getForums();"
    Why I can't use my interface in this proxy class? Get Null in it?

    Could some one help me?
    Thks!

  • #2
    You should inject the proxy in your controller as well, have you done this?

    Code:
    <bean id="viewForumController" class="ViewSpringForumController">
      <property name="forumProxy"><ref bean="fproxy"/></property>
    </bean>
    along with a setForumProxy() method of course.

    Comment


    • #3
      : ) Thks

      You are right ,I change the code to :
      Code:
      public final class ViewForumcontroller implements Controller &#123;
      	private Log log = LogFactory.getLog&#40;this.getClass&#40;&#41;&#41;;
      	private ForumProxy forumProxy;
      
      	public ModelAndView handleRequest&#40;HttpServletRequest arg0,
      			HttpServletResponse arg1&#41; throws Exception &#123;
      		String action = arg0.getParameter&#40;"action"&#41;;
      //ForumProxy forumProxy=new ForumProxy&#40;&#41;;<---- It will report error if I uncommit this line
      		Map map=forumProxy.getCommonListFromParam&#40;action&#41;;
      
      		return new ModelAndView&#40;&#40;String&#41;map.get&#40;"url"&#41;,"datalist",&#40;List&#41;map.get&#40;"data"&#41;&#41;;
      	&#125;
          
      
      	public ForumProxy getForumProxy&#40;&#41; &#123;
      		return forumProxy;
      	&#125;
      
      	public void setForumProxy&#40;ForumProxy forumProxy&#41; &#123;
      		this.forumProxy = forumProxy;
      	&#125;
      &#125;
      add <ref > element of course. But Why ? I want to use the ForumProxy class as a helper class (common class). It is necessary to inspect it to my controller servlet? I means I want to use it as this:
      Code:
      public final class ViewForumcontroller implements Controller &#123;
      	private Log log = LogFactory.getLog&#40;this.getClass&#40;&#41;&#41;;
      
      	public ModelAndView handleRequest&#40;HttpServletRequest arg0,
      			HttpServletResponse arg1&#41; throws Exception &#123;
      		String action = arg0.getParameter&#40;"action"&#41;;
      		ForumProxy forumProxy=new ForumProxy&#40;&#41;;
      		Map map=forumProxy.getCommonListFromParam&#40;action&#41;;
      
      		return new ModelAndView&#40;&#40;String&#41;map.get&#40;"url"&#41;,"datalist",&#40;List&#41;map.get&#40;"data"&#41;&#41;;
      	&#125;
          
      &#125;
      But latter don't work.I will get Null.

      Why can't I do it in this way? Please tell me ,Thks! :?:

      Comment


      • #4
        You can't just create a new object and expect it to be fully wired up with the dependencies you've mentioned in the application context. This is not what Spring's trying to do. To do something like you want, Spring would have to intercept all calls to constructors of objects (through something like AspectJ or other hooks) and that's exactly the thing Spring does not do.

        The reasons:

        - You'd have to tweak your VM or load custom stuff. Classloading issues will often be the result of this
        - Classes wouldn't be portable anymore to other lightweight containers. Running without a (Spring) container wouldn't be possible anymore (no testing without container, etcetera)
        - you have to specify the implementation in your calling code. In fact you're not injecting dependencies anymore, but instead creating them yourself... Results in tighter coupling.

        Comment


        • #5
          : )

          Thanks in advanced!

          Comment

          Working...
          X