zoukankan      html  css  js  c++  java
  • 命名空间p方式的属性注入

    ---------------------siwuxie095

       

       

       

       

       

       

       

    命名空间 p 方式的属性注入

       

       

    命名空间 p 方式的属性注入是 Spring 2.x 版本后提供的方式

       

       

    1、编写一个普通类

       

    Book.java:

       

    package com.siwuxie095.property;

       

    public class Book {

    private String bookName;

    public void setBookName(String bookName) {

    this.bookName = bookName;

    }

    public void print() {

    System.out.println("Book"+bookName);

    }

    }

       

       

       

    2、在配置文件中注入属性

       

    applicationContext.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">

    <!--

    beans 标签中引入 p 命名空间:

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

    -->

    <!-- 命名空间 p 方式的属性注入 -->

    <bean id="book" class="com.siwuxie095.property.Book" p:bookName="十万个为什么"></bean>

       

    </beans>

       

       

       

    3、编写一个测试类

       

    TestProperty.java:

       

    package com.siwuxie095.property;

       

    import org.junit.Test;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

       

    public class TestProperty {

    /**

    * 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 jar 包)

    *

    * 选中方法名,右键->Run As->JUint Test

    */

    @Test

    public void testProperty() {

    // (1) 加载 Spring 的核心配置文件

    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

    // (2) 得到核心配置文件中创建的对象(获取 Bean 实例)

    Book book=(Book) context.getBean("book");

    book.print();

    }

    }

       

       

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    mysql命令集锦
    linux 删除文件名带括号的文件
    linux下的cron定时任务
    struts2文件下载的实现
    贴一贴自己写的文件监控代码python
    Service Unavailable on IIS6 Win2003 x64
    'style.cssText' is null or not an object
    "the current fsmo could not be contacted" when change rid role
    远程激活程序
    新浪图片病毒
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/7402733.html
Copyright © 2011-2022 走看看