Spring根據(jù)XML配置文件注入屬性的方法
方法一使用setter方法
package com.swift;
public class Book {
private String bookName;
public void setBook(String bookName) {
this.bookName = bookName;
}
@Override
public String toString() {
return "Book [book=" + bookName + "]";
}
}
在Spring框架中,假定Servlet類中不能直接生成Book類的對象,并注入String bookName的屬性值
而需要通過配置文件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" 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="book" class="com.swift.Book"> <property name="bookName" value="三體——黑暗森林"></property> </bean> </beans>
Servlet類代碼:
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("/book")
public class BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public BookServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().append("Served at: ").append(request.getContextPath());
@SuppressWarnings("resource")
//就是下邊這幾句了
ApplicationContext context=new ClassPathXmlApplicationContext("a.xml");
Book book=(Book) context.getBean("book");
String bookInfo=book.fun();
response.getWriter().println();
response.getWriter().append(bookInfo);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
注意
beans 、context、core 和expression核心jar包
以及commons-logging 和log4j兩個jar包不要缺少
方法二使用有參構(gòu)造方法
以上這篇Spring根據(jù)XML配置文件注入屬性的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中判斷字段真實長度的實例(中文2個字符,英文1個字符)
下面小編就為大家?guī)硪黄猨ava中判斷字段真實長度的實例(中文2個字符,英文1個字符)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
Jenkins 關(guān)閉和重啟詳細介紹及實現(xiàn)
這篇文章主要介紹了Jenkins的關(guān)閉、重啟的相關(guān)資料,用jar -jar jenkins.war來啟動jenkins服務(wù)器,那么我們?nèi)绾侮P(guān)閉或者重啟jenkins服務(wù)器呢,這里就給出實現(xiàn)的方法,需要的朋友可以參考下2016-11-11
淺談MyBatis-Plus學(xué)習(xí)之Oracle的主鍵Sequence設(shè)置的方法
這篇文章主要介紹了淺談MyBatis-Plus學(xué)習(xí)之Oracle的主鍵Sequence設(shè)置的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
idea2020導(dǎo)入spring5.1的源碼詳細教程
這篇文章主要介紹了idea2020導(dǎo)入spring5.1的源碼的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題
本文主要介紹了詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
java web實現(xiàn)網(wǎng)上手機銷售系統(tǒng)
這篇文章主要為大家詳細介紹了java web實現(xiàn)網(wǎng)上手機銷售系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

