1

我使用 Spring Web MVC 在我的动态 Web 项目的 [servlet-name]-servlet.xml 中获得了以下 bean 清除?我已经阅读了很多文件,但仍然无法理解拥有这些属性标签的目的是什么?

<bean name="abcController" parent="defController" 
    class="abcController">
    <constructor-arg ref="staticService" />
    <property name="commandClass" value="abcCommand" />
    <property name="property2" value="search" />
    <property name="property3" value="true" />
    <property name="formView" value="/someValue" />


</bean>

我知道该属性可以是 abcController 类中的一个字段,但是在 abcController 类中没有一个名为 formView 的字段!有人可以帮我吗?

4

2 回答 2

1

该 xml 文件用于创建字段,而无需在文件本身中编码这些字段。

 // This is used to Start the ApplicationContext Container and to get the Bean of AbcCotroller

ApplicationContext context = 
new ClassPathXmlApplicationContext("[servlet-name]-servlet.xml");

abcController obj = (abcController) context.getBean("abcController");

您可以稍后在代码中使用 bean:

obj.getFormView(); //this will return '/somevalue'
于 2012-09-28T09:49:42.997 回答
0
//Bean.java
 public class SampleBean{
 private String message;
 public void setMessage(String message){
     this.message=message;  //setter injection            

        }
  public void ShowMessage(){
  system.out.println("Message"+message);
       }
  }

  //Main.java
   Class Main{
     public Static Void Main(String args[]){

//启动ApplicationContext容器

         ApplicationContext applicationcontext = new  ClassPathXmlApplicationCOntext("Spring.xml");
  //To Read the SampleBean
        Object o=applicationcontext .getBean("SampleBean");
       SampleBean sampleBean=(SampleBean)o;
 //Invoke the Method
            sampleBean.ShowMessage();
         }
     }
//Spring.Xml

// 您需要配置一些 Spring 和 Xml 所需的更多命名空间

    <bean id="sampleBean" class="SampleBean">
     <property name="message" value="this is the  use of property Tag"/>
   </bean>

   //output :This is the use or Property Tag

explanation: when we want to perform Setter Injection we go for the Property Tag In spring we have some dependency injections like setter,constructor,interface,lookup method injection when we use the Setter Injection first Dependent class object is create and next the dependency class object is created

于 2016-06-01T10:24:17.563 回答