spring Bean的命名
来源:互联网 发布:移动4g数据卡确认欠费 编辑:程序博客网 时间:2024/05/22 16:47
每个Bean可以有一个或多个Id,我们把第一个id成为标识符,其余id叫做别名,这些id在ioc容器中必须唯一,bean id的命名约定如下。
创建一个测试类:Main
运行该方法,可以得到
指定id,唯一
修改测试类:Main
同样运行的结果为:
指定name,唯一
修改测试类:Main
指定别名,唯一
bean id的命名方式:
配置全限定类名,唯一
示例:
定一个helloworld接口
定义一个spring的配置文件text.xml
package com.zto;public interface HelloWorld { public void sayHello();}
实现该接口
package com.zto.impl;import com.zto.HelloWorld;public class HelloWorldImpl implements HelloWorld { @Override public void sayHello() { System.out.println("Hello World!!"); }}
定义一个spring的配置文件text.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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><bean class="com.zto.impl.HelloWorldImpl" ></bean></beans>
package com.zto;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath*:test.xml"); HelloWorld hello = beanFactory.getBean(HelloWorld.class); hello.sayHello(); }}
运行该方法,可以得到
2016-09-05 21:54:52 main INFO support.ClassPathXmlApplicationContext:[578]:Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@694e1548: startup date [Mon Sep 05 21:54:52 CST 2016]; root of context hierarchy2016-09-05 21:54:52 main INFO xml.XmlBeanDefinitionReader:[317]:Loading XML bean definitions from URL [file:/E:/workspace1/subscribecore-parents/subscribecore/wechat/target/classes/test.xml]Hello World!!
指定id,唯一
示例代码
java代码不变,修改text.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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" /></beans>
package com.zto;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { /*BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello = beanFactory.getBean(HelloWorld.class); hello.sayHello();*/ syaHelloWroldById(); } public static void syaHelloWroldById() { BeanFactory bean = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =bean.getBean("helloworld",HelloWorld.class); hello.sayHello(); }}
同样运行的结果为:
2016-09-05 21:54:52 main INFO support.ClassPathXmlApplicationContext:[578]:Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@694e1548: startup date [Mon Sep 05 21:54:52 CST 2016]; root of context hierarchy2016-09-05 21:54:52 main INFO xml.XmlBeanDefinitionReader:[317]:Loading XML bean definitions from URL [file:/E:/workspace1/subscribecore-parents/subscribecore/wechat/target/classes/test.xml]Hello World!!
指定name,唯一
同上,只修改配置文件
<?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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" /> <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" /></beans>
修改测试类:Main
package com.zto;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { /*BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello = beanFactory.getBean(HelloWorld.class); hello.sayHello();*/ /*syaHelloWroldById();*/ syaHelloWroldByName(); } public static void syaHelloWroldById() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByName() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class); hello.sayHello(); }}
指定id和name,唯一
<?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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" /> <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworldById" name="helloworldByName01" class="com.zto.impl.HelloWorldImpl" /></beans>
package com.zto;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { /*BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello = beanFactory.getBean(HelloWorld.class); hello.sayHello();*/ /*syaHelloWroldById();*/ /*syaHelloWroldByName();*/ syaHelloWroldByNameAndId(); } public static void syaHelloWroldById() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByName() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByNameAndId() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworldById",HelloWorld.class); hello.sayHello(); HelloWorld hello2=beanFactory.getBean("helloworldByName01",HelloWorld.class); hello.sayHello(); }}
指定多个name,唯一
<?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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" /> <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworldById" name="helloworldByName01" class="com.zto.impl.HelloWorldImpl" /> <bean name="bean1;alias11;alias12;alias13" class="com.zto.impl.HelloWorldImpl" /> <bean id="bean2" name="alias21;alias22;alias 23" class="com.zto.impl.HelloWorldImpl" /></beans>
package com.zto;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { /*BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello = beanFactory.getBean(HelloWorld.class); hello.sayHello();*/ /*syaHelloWroldById();*/ /*syaHelloWroldByName();*/ /*syaHelloWroldByNameAndId();*/ syaHelloWroldByMultiName(); } public static void syaHelloWroldById() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByName() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByNameAndId() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworldById",HelloWorld.class); hello.sayHello(); HelloWorld hello2=beanFactory.getBean("helloworldByName01",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByMultiName() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld bean1= beanFactory.getBean("bean1",HelloWorld.class); bean1.sayHello(); HelloWorld bean11= beanFactory.getBean("alias11",HelloWorld.class); bean11.sayHello(); HelloWorld bean12= beanFactory.getBean("alias12",HelloWorld.class); bean12.sayHello(); HelloWorld bean13= beanFactory.getBean("alias13",HelloWorld.class); bean13.sayHello(); HelloWorld bean2= beanFactory.getBean("bean2",HelloWorld.class); bean2.sayHello(); HelloWorld bean21= beanFactory.getBean("alias21",HelloWorld.class); bean21.sayHello(); HelloWorld bean22= beanFactory.getBean("alias22",HelloWorld.class); bean22.sayHello(); HelloWorld bean23= beanFactory.getBean("alias23",HelloWorld.class); bean23.sayHello(); }}
指定别名,唯一
<?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:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" /> <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" /> <bean id="helloworldById" name="helloworldByName01" class="com.zto.impl.HelloWorldImpl" /> <bean name="bean1;alias11;alias12;alias13" class="com.zto.impl.HelloWorldImpl" /> <bean id="bean2" name="alias21;alias22;alias 23" class="com.zto.impl.HelloWorldImpl" /> <bean id="bean3" class="com.zto.impl.HelloWorldImpl" /> <alias name="bean3" alias="alias31" /> <alias name="bean3" alias="alias32" /></beans>
package com.zto;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { /*BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello = beanFactory.getBean(HelloWorld.class); hello.sayHello();*/ /*syaHelloWroldById();*/ /*syaHelloWroldByName();*/ /*syaHelloWroldByNameAndId();*/ /*syaHelloWroldByMultiName();*/ syaHelloWroldByAlias(); } public static void syaHelloWroldById() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByName() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByNameAndId() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld hello =beanFactory.getBean("helloworldById",HelloWorld.class); hello.sayHello(); HelloWorld hello2=beanFactory.getBean("helloworldByName01",HelloWorld.class); hello.sayHello(); } public static void syaHelloWroldByMultiName() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld bean1= beanFactory.getBean("bean1",HelloWorld.class); bean1.sayHello(); HelloWorld bean11= beanFactory.getBean("alias11",HelloWorld.class); bean11.sayHello(); HelloWorld bean12= beanFactory.getBean("alias12",HelloWorld.class); bean12.sayHello(); HelloWorld bean13= beanFactory.getBean("alias13",HelloWorld.class); bean13.sayHello(); HelloWorld bean2= beanFactory.getBean("bean2",HelloWorld.class); bean2.sayHello(); HelloWorld bean21= beanFactory.getBean("alias21",HelloWorld.class); bean21.sayHello(); HelloWorld bean22= beanFactory.getBean("alias22",HelloWorld.class); bean22.sayHello(); HelloWorld bean23= beanFactory.getBean("alias23",HelloWorld.class); bean23.sayHello(); } public static void syaHelloWroldByAlias() { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "classpath*:test.xml"); HelloWorld bean3= beanFactory.getBean("bean3",HelloWorld.class); bean3.sayHello(); HelloWorld alias31= beanFactory.getBean("alias31",HelloWorld.class); alias31.sayHello(); HelloWorld alias32= beanFactory.getBean("alias32",HelloWorld.class); alias32.sayHello(); }}
bean id 的命名约定
1、遵循XML命名规范
2、由字母、数字、下划线组成
3、驼峰式,首个单词的字母大写
bean id 的命名约定
1、遵循XML命名规范
2、由字母、数字、下划线组成
3、驼峰式,首个单词的字母大写
0 0
- spring Bean的命名
- spring Bean 的命名
- Spring 命名bean的错误
- Spring中Bean的命名
- Spring的内部bean与空间命名
- 自定义Spring注解bean的命名策略
- Spring Bean的命名(学习笔记)
- Spring学习(十三)Spring Bean 的命名方式介绍
- Spring 资源文件与命名bean的错误
- Spring XML配置文件结构及bean的命名
- Spring中Bean的命名 (id 和name)
- Rhyme/Spring Bean的命名 1.3.1. Naming beans
- (4)Bean的命名
- Spring中Bean的命名问题及ref和idref之间的区别
- Spring中Bean的命名问题及ref和idref之间的区别
- <context:component-scan>的name-generator属性 自定义Spring Bean注解命名规则
- [JAVA][Spring]Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
- [JAVA][Spring]Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
- js中的包装对象
- 高德地图MapView和其他SurfaceView在同一个Activity中重叠显示透明问题
- 重写ImageView画圆
- Element translate must be declared
- C#控制台基础 检测一个string数组是否包含指定的string字符串
- spring Bean的命名
- C++链表
- Expected resource of type id
- 《TCP/IP详解 卷1:协议》 读书笔记 第22章 TCP的坚持定时器
- nullable、__nullable、_Nullable 究竟有什么区别呢?
- 单线程爬虫之“中国领导干部资料库”
- 复用Httpurlconnection
- CCF 201604-2 得了60分
- 超越股神的贝佐斯:“我可是在做一件大事”