Spring | 您所在的位置:网站首页 › zc名字首字母 › Spring |
Bean id的命名约定: 1.遵循XML命名规范 2.由字母数字下划线组成 3.驼峰式,首个单词字母小写,第二个单词首字母要大写 样例项目的结构 xml应该放在src下面的conf中,如果不方法src下将会找不到conf文件 conf-definition.xml HelloWorld接口 package com.zc.spring.chapter04.definition; public class HelloWorldImpl implements HelloWorld { @Override public void sayHello() { System.out.println("Hello World!"); } }Main运行类 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(); } }
|
CopyRight 2018-2019 实验室设备网 版权所有 |