首先在项目中创建包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"/>
public static void sayHelloWorldByClass(){ //使用FileSystemXmlApplicationContext加载配置文件信息 BeanFactory beanFactory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); //获取bean实例 HelloWorld helloWorld=beanFactory.getBean(HelloWorldImpl.class); helloWorld.sayHello(); }
<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程序入口,新建方法来调用bean
public 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(); }