Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >spring开发_使用p名称空间配置属性

spring开发_使用p名称空间配置属性

作者头像
Hongten
发布于 2018-09-13 08:59:09
发布于 2018-09-13 08:59:09
47800
代码可运行
举报
文章被收录于专栏:HongtenHongten
运行总次数:0
代码可运行

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112563.html

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 <?xml version="1.0" encoding="GBK"?>
 2     <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
 3 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 7     <bean id="meatServiceOfFish" class="com.b510.service.impl.FishServiceBean"></bean>
 8     <bean id="meatServiceOfPork" class="com.b510.service.impl.PorkServiceBean"></bean>
 9     <bean id="animaleServiceOfDog" class="com.b510.service.impl.DogServiceBean"
10         p:age="12" p:meatService-ref="meatServiceOfPork" />
11     <bean id="animaleServiceOfCat" class="com.b510.service.impl.CatServiceBean"
12         p:age="3" p:meatService-ref="meatServiceOfFish" />
13 </beans>

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

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

运行结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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,我很喜欢吃鱼肉
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-03-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
spring开发_BeanFactoryPostProcessor_容器后处理器
http://www.cnblogs.com/hongten/gallery/image/112611.html
Hongten
2018/09/13
3200
spring开发_BeanPostProcessor_Bean后处理器
http://www.cnblogs.com/hongten/gallery/image/112581.html
Hongten
2018/09/13
3150
spring开发_Annotation_注解
http://www.cnblogs.com/hongten/gallery/image/112614.html
Hongten
2018/09/13
3910
spring开发_spring构造注入Bean
http://www.cnblogs.com/hongten/gallery/image/112399.html
Hongten
2018/09/13
4730
spring开发_spring环境搭建
http://www.cnblogs.com/hongten/gallery/image/112322.html
Hongten
2018/09/13
4500
spring开发_Annotation_AOP_Before增强处理
http://www.cnblogs.com/hongten/gallery/image/112691.html
Hongten
2018/09/13
4320
spring开发_注入其他Bean的方法返回值_MethodInvokingFactoryBean
http://www.cnblogs.com/hongten/gallery/image/112562.html
Hongten
2018/09/13
1.3K0
spring开发_JDBC操作MySQL数据库
http://www.cnblogs.com/hongten/gallery/image/112450.html
Hongten
2018/09/13
2.5K0
spring开发_Spring_DataSource
http://www.cnblogs.com/hongten/gallery/image/112745.html
Hongten
2018/09/13
4910
spring开发_spring+hibernate
http://www.cnblogs.com/hongten/gallery/image/112469.html
Hongten
2018/09/13
5560
spring开发_spring中Bean的作用域_singleton_prototype
http://www.cnblogs.com/hongten/gallery/image/112385.html
Hongten
2018/09/13
4820
spring开发_Spring+Hibernate_HibernateDaoSupport
http://www.cnblogs.com/hongten/archive/2012/03/15/java_spring_hibernate_hibernateTemplate.html
Hongten
2018/09/13
4330
spring开发_Spring+Struts2
http://www.cnblogs.com/hongten/gallery/image/112920.html
Hongten
2018/09/13
5310
spring开发_Spring+Hibernate_HibernateTemplate
http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html
Hongten
2018/09/13
5010
spring开发_JDBC操作MySQL数据库_使用xml配置事务管理
http://www.cnblogs.com/hongten/archive/2012/03/09/java_spring_jdbc.html
Hongten
2018/09/13
9410
spring+hibernate+JQuery开发_电子相册_源码
=============================================================
Hongten
2018/09/13
4.8K0
spring+hibernate+JQuery开发_电子相册_源码
Spring IOC基于XML管理Bean(二)
注意:在使用 <context:property-placeholder> 元素加载外包配置文件功能前,首先需要在 XML 配置的一级标签 <beans> 中添加 context 相关的约束。
鱼找水需要时间
2023/06/11
4920
Spring IOC基于XML管理Bean(二)
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件
创建一个maven工程,导入Spring需要的依赖,为了方便测试,我还导入了一个Junit测试包
我是一棵卷心菜
2022/01/18
1.4K0
Spring--框架学习
为了后期方便各州测试,在/src下新建test测试目录,在其中新建java文件夹,里面写测试代码
花猪
2022/02/22
1.3K0
Spring--框架学习
SpringIOC
IOC:控制反转的底层原理就是:工厂模式+反射+配置文件 DI:依赖注入就是通过配置文件设置属性值
用户3112896
2019/09/26
4030
推荐阅读
相关推荐
spring开发_BeanFactoryPostProcessor_容器后处理器
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验