Spring实战之使用c:命名空间简化配置操作示例

本文实例讲述了Spring使用c命名空间简化配置操作。分享给大家供大家参考,具体如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:c="http://www.springframework.org/schema/c"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
   <!-- 配置chinese实例,其实现类是Chinese -->
   <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
      c:axe-ref="steelAxe" c:age="29"/>
   <!-- 配置chinese实例,其实现类是Chinese -->
   <bean id="chinese2" class="org.crazyit.app.service.impl.Chinese"
      c:_0-ref="stoneAxe" c:_1="29"/>
   <!-- 配置stoneAxe实例,其实现类是StoneAxe -->
   <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
   <!-- 配置steelAxe实例,其实现类是SteelAxe -->
   <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
</beans>

二 接口

Axe

package org.crazyit.app.service;
public interface Axe
{
   // Axe接口里有个砍的方法
   public String chop();
}

Person

package org.crazyit.app.service;
public interface Person
{
   // 定义一个使用斧子的方法
   public void useAxe();
}

三 实现

Chinese

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class Chinese implements Person
{
   private Axe axe;
   private int age;
   public Chinese(){ }
   // axe的setter方法
   public void setAxe(Axe axe)
   {
      this.axe = axe;
   }
   // age的setter方法
   public void setAge(int age)
   {
      this.age = age;
   }
   // 实现Person接口的useAxe()方法
   public void useAxe()
   {
      System.out.println(axe.chop());
      System.out.println("age成员变量的值:" + age);
   }
}

StoneAxe

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class StoneAxe implements Axe
{
   public String chop()
   {
      return "石斧砍柴好慢";
   }
}

SteelAxe

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class SteelAxe implements Axe
{
   public String chop()
   {
      return "钢斧砍柴真快";
   }
}

四 测试类

package lee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.crazyit.app.service.*;
public class BeanTest
{
  public static void main(String[] args)throws Exception
  {
    // 创建Spring容器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    // 获取chinese 实例
    Person p = ctx.getBean("chinese" , Person.class);
    // 调用useAxe()方法
    p.useAxe();
    Person p1 = ctx.getBean("chinese2" , Person.class);
    // 调用useAxe()方法
    p1.useAxe();
  }
}

五 运行

钢斧砍柴真快
age成员变量的值:29
石斧砍柴好慢
age成员变量的值:29

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

(0)

相关推荐

  • SpringBoot获取yml和properties配置文件的内容

    (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @ConfigurationProperties 注解 --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --> <dependency> <groupId>org.springframework.boot</groupId>

  • Spring Boot配置元数据方法教程

    这篇文章主要介绍了Spring Boot配置元数据方法教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 前言 在编写 Spring Boot 应用程序时,将配置属性映射到 Java bean上是非常有用的.但是,记录这些属性的最好方法是什么呢? 在本教程中,我们将探讨 Spring Boot Configuration Processor和 关联的 JSON 元数据文件,该 JSON 文档记录每个属性的含义.约束等. 配置元数据 作为开发人员

  • Spring Task定时任务的配置和使用详解

    记录下Spring自带的定时任务用法. spring中使用定时任务 基于xml配置文件使用定时任务 首先配置spring开启定时任务 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/

  • 通过Spring Boot配置动态数据源访问多个数据库的实现代码

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中也描述了):只适用于数据库数量不多且固定的情况.针对数据库动态增加的情况无能为力. 下面讲的方案能支持数据库动态增删,数量不限. 数据库环境准备 下面一Mysql为例,先在本地建3个数据库用于测试.需要说明的是本方案不限数据库数量,支持不同的数据库部署在不同的服务器上.如图所示db_project_001.d

  • 详解SpringBoot配置连接池

    内置的连接池 目前spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池. 数据库连接可以使用DataSource池进行自动配置. 由于Tomcat数据源连接池的性能和并发,在tomcat可用时,我们总是优先使用它. 如果HikariCP可用,我们将使用它. 如果Commons DBCP可用,我们将使用它,但在生产环境不推荐使用它. 最后,如果Commons DBCP2可用,我们将使用它. 以上的几种连接池,可以通过在配置application文

  • springboot如何读取配置文件(application.yml)中的属性值

    在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId>

  • spring boot Logging的配置以及使用详解

    前言:该篇文章基本上是翻译的官方文档! spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配置.如果使用了spring boot的Starters,那么默认会使用Logback用于记录日志. 一.Log format spring boot中默认的日志输出格式如下: 2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.ca

  • 详解Spring Boot加载properties和yml配置文件

    一.系统启动后注入配置 package com.example.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframewo

  • Spring实战之使用p:命名空间简化配置操作示例

    本文实例讲述了Spring实战之使用p:命名空间简化配置操作.分享给大家供大家参考,具体如下: 一 配置 <?xml version="1.0" encoding="GBK"?> <!-- 指定Spring配置文件的根元素和Schema 并导入p:命名空间的元素 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="

  • 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 复制代码 代码如下: <bean id="configproperties"          class="org.springframework.beans.factory.config.PropertiesFactoryBean">          <property name="location" value="classpath:jdbc.properties"/>

  • SpringMVC自定义类型转换器实现解析

    这篇文章主要介绍了SpringMVC自定义类型转换器实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 页面录入的字符串:2019/12/05可以映射到实体的日期属性上,但是如果是录入2019-12-05就会报错400 bad request,想要以2019-12-05日期格式的方式映射到实体的日期属性上,需要自定义类型转换器,主要步骤如下: 1. 自定义类实现Convertro<S,T>接口 2.Springmvc.xml中配置Conv

随机推荐