Spring根据XML配置文件 p名称空间注入属性的实例

时间:2021-05-20

要生成对象并通过名称空间注入属性的类代码如下:

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://.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根据XML配置文件 p名称空间注入属性的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章