Spring- Bean的命名

 

id的命名约定:

1.遵循XML命名规范

2.由字母数字下划线组成

3.驼峰式,首个单词字母小写,第二个单词首字母要大写

样例项目的结构

xml应该放在src下面的conf中,如果不方法src下将会找不到conf文件

conf-definition.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/-beans-3.0.xsd">
        <!-- bean的六种命名方式 -->
        <!-- 配置全限定类名,唯一 -->
        < class="com.zc..chapter04.definition.HelloWorldImpl" />
        <!-- 指定id,唯一 -->
        <bean id="helloWorld" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        <!-- 指定name,唯一 -->
        <bean name="helloWorldByName" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        <!-- 指定id和name,唯一 -->
        <bean id="helloWorldById" name="helloWorldByName01" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        
        <!-- 指定多个name,唯一 -->
        <bean name="bean1;alias11;alias12;alias13" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        
        <bean id="bean2" name="alias21;alias22;alias23" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        
        
        <!-- 指定别名,唯一 -->
        <bean name="bean3" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        <alias name="bean3" alias="alias31"/>
        <alias name="bean3" alias="alias32"/>
        <alias name="bean3" alias="alias33"/>
       
</beans>

HelloWorld接口

1
2
3
4
5
6
7
8
9
10
11
package com.zc.spring.chapter04.definition;
public class HelloWorldImpl implements HelloWorld {
    @Override
    public void sayHello() {
        System.out.println("Hello World!");
    }
}  

Main运行类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.zc.spring.chapter04.definition;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    
    public static void main(String[] args) {
        
        
        
        sayHelloWorldByAlias();
    }
    
    public static void sayHelloWorldById() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
        HelloWorld helloWorld = beanFactory.getBean("helloWorld", HelloWorld.class);
        helloWorld.sayHello();
        
    }
    
    public static void  sayHelloWorldByClass() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
        HelloWorld helloWorld = beanFactory.getBean(HelloWorldImpl.class);
        helloWorld.sayHello();
    }
    public static void  sayHelloWorldByName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
        HelloWorld helloWorld = beanFactory.getBean("helloWorldByName", HelloWorld.class);
        helloWorld.sayHello();
    }
    
    public static void  sayHelloWorldByNameAndId() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
        HelloWorld helloWorld = beanFactory.getBean("helloWorldById", HelloWorld.class);
        helloWorld.sayHello();
        HelloWorld helloWorld01 = beanFactory.getBean("helloWorldByName01", HelloWorld.class);
        helloWorld01.sayHello();
    }
    
    public static void sayHelloWorldMultiName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
        HelloWorld helloWorld = beanFactory.getBean("bean1", HelloWorld.class);
        helloWorld.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  sayHelloWorldByAlias() {
        //配置文件加载以及IOC容器启动
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
        //通过别名获取bean实例
        HelloWorld bean3 = beanFactory.getBean("bean3", HelloWorld.class);
        //利用bean实例输出helloworld信息
        bean3.sayHello();
        HelloWorld bean31 = beanFactory.getBean("alias31",HelloWorld.class);
        bean31.sayHello();
        HelloWorld bean32 = beanFactory.getBean("alias32",HelloWorld.class);
        bean32.sayHello();
        HelloWorld bean33 = beanFactory.getBean("alias33",HelloWorld.class);
        bean33.sayHello();
    }
}

 

 

 

人已赞赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索