栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring根据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配置文件写法如下:





p:userName="peach"

p:后是属性的变量名 后面是赋值

约束是xmlns:p="http://www.springframework.org/schema/p"

生成对象及属性值调用方法,代码如下:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143167.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号