Spring根據(jù)XML配置文件 p名稱空間注入屬性的實例
要生成對象并通過名稱空間注入屬性的類 代碼如下:
package com.swift;
public class User {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
public String fun() {
return "User's fun is ready."+this.userName;
}
}
XML配置文件寫法如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- IoC 控制反轉(zhuǎn) SpringSpring根據(jù)XML配置文件生成對象 --> <bean id="user" class="com.swift.User" p:userName="peach"></bean> </beans>
p:userName="peach"
p:后是屬性的變量名 后面是賦值
約束是xmlns:p="http://www.springframework.org/schema/p"
生成對象及屬性值調(diào)用方法,代碼如下:
package com.swift;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@WebServlet("/test")
public class TestIOC extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestIOC() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
@SuppressWarnings("resource")
//就是下邊這幾句了
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=(User) context.getBean("user");
String userInfo=user.fun();
response.getWriter().println();
response.getWriter().append(userInfo);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
以上這篇Spring根據(jù)XML配置文件 p名稱空間注入屬性的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java的Hibernate框架中Criteria查詢使用的實例講解
這篇文章主要介紹了Java的Hibernate框架中Criteria查詢使用的實例講解,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01
Spring?框架的?MethodInterceptor?簡介及示例代碼
MethodInterceptor接口定義了一個方法Object?intercept(Object?obj,?Method?method,?Object[]?args,?MethodProxy?proxy)?,該方法在代理對象的方法被調(diào)用時被觸發(fā),這篇文章主要介紹了Spring?框架的?MethodInterceptor?簡介及示例代碼,需要的朋友可以參考下2023-09-09
基于Java8并行流(parallelStream)的注意點
這篇文章主要介紹了Java8并行流(parallelStream)的注意點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
小議Java的源文件的聲明規(guī)則以及編程風(fēng)格
這篇文章主要介紹了小議Java的源文件的聲明規(guī)則以及編程風(fēng)格,僅給Java初學(xué)者作一個簡單的示范,需要的朋友可以參考下2015-09-09
SpringBoot JPA懶加載失效的解決方案(親測有效)
這篇文章主要介紹了SpringBoot JPA懶加載失效的解決方案(親測有效),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
MyBatis insert語句返回主鍵和selectKey標(biāo)簽方式
這篇文章主要介紹了MyBatis insert語句返回主鍵和selectKey標(biāo)簽方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java實現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式
這篇文章主要介紹了Java實現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

