Spring根据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 控制反转 SpringSpring根据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包不要缺少

方法二使用有参构造方法

以上这篇Spring根据XML配置文件注入属性的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Spring如何在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 <beans> 根元素,可包含多个<bean>元素,一个<bean>即一个Bean的配置. <bean> 一个<bean>即一个Bean对象.原来是new出来该类的一个对象,Spring中是一个<bean>创建一个对象. <bean na

  • SpringBoot通过yml和xml文件配置日志输出方法

    SpringBoot中默认使用Logback进行日志输出,可以同时使用SpringBoot框架的配置文件application.yml或是通过logback的配置文件logback.xml进行配置. 通过application.yml配置 <?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--定义日志文件的存储地址 勿在 Lo

  • 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配置文件写法如下: &

  • 详解spring applicationContext.xml 配置文件

    applicationContext.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http

  • Spring标准的xml文件头实例分析

    这篇文章主要介绍了Spring标准的xml文件头实例分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2

  • spring mvc 读取xml文件数据库配置参数的方法

    本文主要介绍怎么通过属性注入与构造器注入实现把我们项目中要用到的数据库参数放到xml文件里面去,方便部署. spring mvc 4.2.6项目 SQL Server 2008数据库 本文介绍的主要使用ApplicationContext以及其实现类实现.主要用到的是ClassPathXmlApplicationContext. ClassPathXmlApplicationContext:从类路径ClassPath中寻找指定的XML配置文件,找到并装载 完成ApplicationContext

  • Spring 配置文件XML头部文件模板实例详解

    普通spring配置文件模板: <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.s

  • Spring主配置文件(applicationContext.xml) 导入约束详解

    eclipse导入Spring配置文件约束  Windows-Preference-XML-XMLCatalog 点 Add 选File System 下spring的解压包下的schema文件夹,选beans,然后选择spring对应的版本的xsd文件 选择指定xsd文件,再Key的路径后面添加"/spring-beans-4.2.xsd"点ok 创建applicationContext.xml   写根元素 <beans></beans> Add导入XSI,

  • Spring根据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框架中

  • 基于Spring boot @Value 注解注入属性值的操作方法

    本文主要介绍Spring @Value 注解注入属性值的使用方法的分析,文章通过示例代码非常详细地介绍,对于每个人的学习或工作都有一定的参考学习价值 在使用spring框架的项目中,@Value是经常使用的注解之一.其功能是将与配置文件中的键对应的值分配给其带注解的属性.在日常使用中,我们常用的功能相对简单.本文使您系统地了解@Value的用法. @Value注入形式 根据注入的内容来源,@ Value属性注入功能可以分为两种:通过配置文件进行属性注入和通过非配置文件进行属性注入. 非配置文件注

  • Spring中xml配置文件的基础使用方式详解

    目录 1. xml配置文件的读取 1.1 通过类路径读取配置文件 1.2 通过文件系统绝对路径读取配置文件 1.3使用BeanFactory接口读取配置文件 2.带参构造对象的创建(constructor-arg标签) 3.使用另一个类中的方法创建对象,并放到Spring容器中 4.调用另一个类中的静态方法创建对象,并放到Spring容器中 5.对象的生命周期 6.单例多例的测试 1. xml配置文件的读取 目录结构 applicationContext.xml配置文件 <?xml versio

  • java实现Spring在XML配置java类的方法

    1. 创建自己的bean文件:beans.xml <?xml version="1.0" encoding="UTF-8"?> <busi-beans> <beans> <bean id="SysHelloImpl" type="com.cxm.test.SysHello"> <desc>test</desc> <impl-class>com.

  • jQuery实现遍历XML节点和属性的方法示例

    本文实例讲述了jQuery实现遍历XML节点和属性的方法.分享给大家供大家参考,具体如下: 用jquery遍历xml网上已经有很多, 但是看了好多文章, 对于不指定属性名称的遍历方法却没有 研究了一下, 好像jquery没有attributes. 还是要借助于原生态的JS 以下是JS代码 <script type="text/javascript" src="js/jquery.min.js"></script> <div id=&qu

  • Spring运行时手动注入bean的方法实例

    有时候,会有这样一个需求,在程序运行时动态生成的对象,需要注入到Spring容器中进行管理. 下面是获取Bean以及注入Bean的工具类 import org.springframework.beans.BeansException; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinit

  • 详解Spring通过@Value注解注入属性的几种方式

    场景 假如有以下属性文件dev.properties, 需要注入下面的tag tag=123 通过PropertyPlaceholderConfigurer <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="dev.properties" /&

  • Eclipse引用XSD实现XML配置文件提示标签的方法

    对于SpringMVC的下载包,XSD文件存放在压缩包里的Schema文件夹里,而且各功能的XSD存放在各自文件夹内,把最新版本的XSD文件取出. Eclipse软件,依次选择Window--Preferences,弹出Preferences窗体,左侧,展开XML -- XML Catalog,对User Specified Entries,选择Add... 找到XSD文件存放位置,并填上 要注意的是:KeyType选择Schema location,并且Key值的后面要加上/xxxxx-x.x

随机推荐