簡體   English   中英

的意義是什么 <property> Web MVC的[servlet-name] -servlet.xml中添加標簽?

[英]What is the meaning of <property> tag in [servlet-name]-servlet.xml of Spring Web MVC?

我在使用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的字段! 有人可以幫我嗎?

該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'
//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

說明:當我們要執行Setter注入時,我們使用屬性標記。在春季,當我們使用Setter注入時,我們將進行一些依賴注入,例如setter,constructor,interface,lookup方法注入。首先創建Dependent類對象,然后創建依賴類對象被建造

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM