- 遵循XML命名規範
- 由字母,數字,下劃線組成
- 駝峯式,首個單詞字母小寫,從第二個單詞開始首字母大寫。
首先在項目中創建包definition,接下來在包中創建HelloWorld接口:
public interface HelloWorld {
public void sayHello();
}
public class HelloWorldImpl implements HelloWorld{
public void sayHello() {
System.out.println("Hello World");
}
}
接下來我們在配置文件中爲HelloWorldImpl進行Bean的命名:
<bean class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
我們在程序入口Mian.java來加載配置文件以及運行示例。
public static void sayHelloWorldByClass(){
//使用FileSystemXmlApplicationContext加載配置文件信息
BeanFactory beanFactory=
new FileSystemXmlApplicationContext("src/conf/conf-definition.xml");
//獲取bean實例
HelloWorld helloWorld=beanFactory.getBean(HelloWorldImpl.class);
helloWorld.sayHello();
}
- 完成配置文件的加載以及Spring IoC容器的啓動
- 從容器中獲得HelloWorldImpl實現類的實例
- 輸出問候信息
<bean id="HelloWorldById" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
修改Main程序入口,新建方法來調用bean
public static void sayHelloWorldById(){
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-definition.xml");
HelloWorld helloWorld=factory.getBean("HelloWorldById",HelloWorldImpl.class);
helloWorld.sayHello();
}
<bean name="HelloWorldByName" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
修改Main程序入口,新建方法來調用beanpublic static void sayHelloWorldByName(){
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-definition.xml");
HelloWorld helloWorld=factory.getBean("HelloWorldByName",HelloWorldImpl.class);
helloWorld.sayHello();
}
<bean id="HelloWorldById01" name="HelloWorldByName01"
class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
修改Main程序入口,新建方法來調用bean
public static void sayHelloWorldByNameAndId(){
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-definition.xml");
HelloWorld helloWorld01=factory.getBean("HelloWorldById01",HelloWorldImpl.class);
HelloWorld helloWorld02=factory.getBean("HelloWorldByName01",HelloWorldImpl.class);
helloWorld01.sayHello();
helloWorld02.sayHello();
}
<bean name="bean1;alias01;alias02;alias03"
class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
<bean id="bean2" name="alias11;alias12;alias13"
class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
public static void sayHelloWorldByMutilName(){
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-definition.xml");
HelloWorld helloWorld1=factory.getBean("bean1",HelloWorldImpl.class);
HelloWorld helloWorld01=factory.getBean("alias01",HelloWorldImpl.class);
HelloWorld helloWorld02=factory.getBean("alias02",HelloWorldImpl.class);
HelloWorld helloWorld03=factory.getBean("alias03",HelloWorldImpl.class);
helloWorld1.sayHello();
helloWorld01.sayHello();
helloWorld02.sayHello();
helloWorld03.sayHello();
HelloWorld helloWorld2=factory.getBean("bean2",HelloWorldImpl.class);
HelloWorld helloWorld11=factory.getBean("alias11",HelloWorldImpl.class);
HelloWorld helloWorld12=factory.getBean("alias12",HelloWorldImpl.class);
HelloWorld helloWorld13=factory.getBean("alias13",HelloWorldImpl.class);
helloWorld2.sayHello();
helloWorld11.sayHello();
helloWorld12.sayHello();
helloWorld13.sayHello();
}
<bean name="bean3" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
<alias name="bean3" alias="alias21"/>
<alias name="bean3" alias="alias22"/>
public static void sayHelloWorldByAlias(){
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-definition.xml");
HelloWorld helloWorld01=factory.getBean("bean3",HelloWorldImpl.class);
HelloWorld helloWorld02=factory.getBean("alias21",HelloWorldImpl.class);
HelloWorld helloWorld03=factory.getBean("alias22",HelloWorldImpl.class);
helloWorld01.sayHello();
helloWorld02.sayHello();
helloWorld03.sayHello();
}