Spring的Ioc依赖注入:在类中添加了一个私有属性, 然后没有get&set方法, 在另一个方法中直接使用赋值, 并不需要实例化一个新的该对象, 貌似是调用内存中存在的对象, 如果不存在则创建(new)一个, 是这样的吗?  这部分概念不是很熟悉, 新人求教
private FormService formService;public TaskFormData getTaskFormData(String taskId) {        formService = processEngine.getFormService();        return formService.getTaskFormData(taskId);    }

解决方案 »

  1.   

    spring中的IoC机制是这样的:通过反射原理,利用Class类中的newInstance()方法类实例化对象。
        比如说现在有一个UserAction类和一个UserService类,在UserAction类中有一个private UserService userService的属性,这个属性是要创建它的setter方法的。另外在spring的配置文件applicationContext.xml中也要配置这两个类之间的关联关系,例如:
        <bean id="userDAO" class="com.softeem.crm.dao.impl.UserDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
        <bean id="userService" class="com.softeem.crm.service.impl.UserServiceImpl">
         <property name="userDAO" ref="userDAO"></property>
        </bean>
        
        <bean id="userAction" class="com.softeem.action.UserAction">
         <property name="userService" ref="userService"></property>
        </bean>
    将他们之间的关联关系配置完成之后,在来看反射中的实现(反射中的代码是不用自己写的)在反射中,首先要读取applicationContext.xml配置文件中的信息,通过forName()(其他方法也可以的)加载UserAction类(根据从配置文件中获取的完整的类路径来加载),获取class对象,利用Class类中的getDeclaredFields() 获取到userService属性,根据属性名称类拼接setter方法,获取属性的类型(忘记什么方法了),通过newInstance()方法实例化userService,获取setUserService()方法的对象, 来执行Method类中的invoke()方法来对UserAction类中userService属性进行setter方法赋值。
      

  2.   

    没在配置文件配或者使用注解,这个就和普通的类一样了,没有get&set方法,当然也不会自动注入
      

  3.   

    Struts2的依赖注入没有getter setter方法也可以,使用了字节码操作