每个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 hierarchy
2016-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 hierarchy
2016-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、驼峰式,首个单词的字母大写