博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring开发_使用p名称空间配置属性
阅读量:6292 次
发布时间:2019-06-22

本文共 5504 字,大约阅读时间需要 18 分钟。

项目结构:

/spring_1400_p名称空间/src/com/b510/app/test/SpringTest.java

1 package com.b510.app.test;  2  3 import org.springframework.context.ApplicationContext;  4 import org.springframework.context.support.ClassPathXmlApplicationContext;  5  6 import com.b510.service.AnimalService;  7  8 public class SpringTest {
9 public static void main(String[] args) {
10 ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml"); 11 // 获取id为animaleServiceOfDog的Bean 12 AnimalService animalServiceOfDog = (AnimalService) act 13 .getBean("animaleServiceOfDog"); 14 animalServiceOfDog.getInfo(); 15 // 获取id为animaleServiceOfCat的Bean 16 AnimalService animalServiceOfCat = (AnimalService) act 17 .getBean("animaleServiceOfCat"); 18 animalServiceOfCat.getInfo(); 19 } 20 }

/spring_1400_p名称空间/src/com/b510/service/AnimalService.java

1 package com.b510.service;  2  3 /**  4  * 动物抽象类  5  *  6  * @author Hongten  7  *  8  */  9 public interface AnimalService {
10 11 /** 12 * 获取相关信息 13 */ 14 public abstract void getInfo(); 15 16 }

/spring_1400_p名称空间/src/com/b510/service/MeatService.java

1 package com.b510.service;  2  3 /**  4  * 定义抽象类MeatService肉类  5  *  6  * @author Hongten  7  *  8  */  9 public interface MeatService {
10 11 /** 12 * 定义方法whatMeat 13 * 14 * @return 返回一个字符串 15 */ 16 public abstract String whatMeat(); 17 }

/spring_1400_p名称空间/src/com/b510/service/impl/CatServiceBean.java

1 package com.b510.service.impl;  2  3 import com.b510.service.AnimalService;  4 import com.b510.service.MeatService;  5  6 /**  7  * 定义猫类实现AnimaleService抽象类  8  *  9  * @author Hongten 10  * 11  */ 12 public class CatServiceBean implements AnimalService {
13 private int age; 14 private MeatService meatService; 15 16 public int getAge() {
17 return age; 18 } 19 20 public void setAge(int age) {
21 this.age = age; 22 } 23 24 @Override 25 public void getInfo() {
26 System.out.println("我是猫,我的年龄是:"+age+",我很喜欢吃"+meatService.whatMeat()); 27 } 28 29 public MeatService getMeatService() {
30 return meatService; 31 } 32 33 public void setMeatService(MeatService meatService) {
34 this.meatService = meatService; 35 } 36 37 38 }

/spring_1400_p名称空间/src/com/b510/service/impl/DogServiceBean.java

1 package com.b510.service.impl;  2  3 import com.b510.service.AnimalService;  4 import com.b510.service.MeatService;  5  6 /**  7  * 定义DogServiceBean类  8  *  9  * @author Hongten 10  * 11  */ 12 public class DogServiceBean implements AnimalService {
13 private int age; 14 private MeatService meatService; 15 16 public MeatService getMeatService() {
17 return meatService; 18 } 19 20 public void setMeatService(MeatService meatService) {
21 this.meatService = meatService; 22 } 23 24 public int getAge() {
25 return age; 26 } 27 28 public void setAge(int age) {
29 this.age = age; 30 } 31 32 @Override 33 public void getInfo() {
34 System.out.println("我是狗,我的年龄是:" + age + ",我很喜欢吃" 35 + meatService.whatMeat()); 36 } 37 38 }

/spring_1400_p名称空间/src/com/b510/service/impl/FishServiceBean.java

1 package com.b510.service.impl;  2  3 import com.b510.service.MeatService;  4  5 /**  6  * 定义鱼肉实现MeatService抽象类  7  *  8  * @author Hongten  9  * 10  */ 11 public class FishServiceBean implements MeatService {
12 13 @Override 14 public String whatMeat() {
15 return "鱼肉"; 16 } 17 18 }

/spring_1400_p名称空间/src/com/b510/service/impl/PorkServiceBean.java

1 package com.b510.service.impl;  2  3 import com.b510.service.MeatService;  4  5 /**  6  * 定义猪肉实现MeatService抽象类  7  *  8  * @author Hongten  9  * 10  */ 11 public class PorkServiceBean implements MeatService {
12 13 @Override 14 public String whatMeat() {
15 return "猪肉"; 16 } 17 18 }

/spring_1400_p名称空间/src/beans.xml

1 
2
3
7
8
9
11
13

使用p名称空间没有标准的XML格式灵活,如果某个Bean的属性名称是以"-ref"结尾的,那么采用p名称空间定义是就会出现错误,采用标准的XML格式是

不会出现这样的错误滴!!!

运行结果:

 

1 2012-3-12 12:58:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh  2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Mon Mar 12 12:58:51 CST 2012]; root of context hierarchy  3 2012-3-12 12:58:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  4 信息: Loading XML bean definitions from class path resource [beans.xml]  5 2012-3-12 12:58:54 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory  6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@111a775  7 2012-3-12 12:58:54 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  8 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@111a775: defining beans [meatServiceOfFish,meatServiceOfPork,animaleServiceOfDog,animaleServiceOfCat]; root of factory hierarchy  9 我是狗,我的年龄是:12,我很喜欢吃猪肉 10 我是猫,我的年龄是:3,我很喜欢吃鱼肉

转载地址:http://usdta.baihongyu.com/

你可能感兴趣的文章
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>
springmvc Could not write content: No serializer
查看>>
Python系语言发展综述
查看>>
新手 开博
查看>>
借助开源工具高效完成Java应用的运行分析
查看>>
163 yum
查看>>
第三章:Shiro的配置——深入浅出学Shiro细粒度权限开发框架
查看>>
80后创业的经验谈(转,朴实但实用!推荐)
查看>>
让Windows图片查看器和windows资源管理器显示WebP格式
查看>>
我的友情链接
查看>>
vim使用点滴
查看>>
embedded linux学习中几个需要明确的概念
查看>>
mysql常用语法
查看>>
Morris ajax
查看>>
【Docker学习笔记(四)】通过Nginx镜像快速搭建静态网站
查看>>
ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
查看>>
<转>云主机配置OpenStack使用spice的方法
查看>>
java jvm GC 各个区内存参数设置
查看>>
[使用帮助] PHPCMS V9内容模块PC标签调用说明
查看>>