看代码
package logan.spring.study; public class HelloWorld { private String name; public void setName2(String name){ System.out.println("setName: "+ name); this.name = name; } public void hello(){ System.out.println("hello: " + name); } public HelloWorld(){ System.out.println("HelloWorld‘s Constructor..."); } }
package logan.spring.study; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub //1.创建Spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从 IOC容器中获取Bean实例 HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello"); //3.调用hello方法 System.out.println(helloWorld); } }
applicationContext
<?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/spring-beans.xsd"> <!-- 配置bean class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数 id:表示容器的bean,id唯一 --> <bean id="hello" class="logan.spring.study.HelloWorld"> </bean> </beans>
输出结果
五月 18, 2017 8:51:39 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org[email protected]3eb07fd3: startup date [Thu May 18 20:51:39 CST 2017]; root of context hierarchy 五月 18, 2017 8:51:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] HelloWorld‘s Constructor... [email protected]
ApplicationContext本身是一个容器
ApplicationContext的主要实现类:
ClassPathXmlApplicationContext():从类路径下加载配置文件
FileSystemXmlApplicationContext:从文件系统加载配置文件
ConfigurableAppliactionContext扩展于ApplicationContext,新增加两个主要方法,refresh()和close(),让ApplicationContext具有启动,刷新和关闭上下文的功能。
ApplicationContext在初始化上下文时就实例化所有单例的Bean。
WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作。
获取bean还可以用一下方式:
package logan.spring.study; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub //1.创建Spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从 IOC容器中获取Bean实例 HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello"); HelloWorld hello = ctx.getBean(HelloWorld.class); //3.调用hello方法 System.out.println(hello); } }
这样做有缺点,就是如果在配置文件里面配置两个Bean,它就会报错。
<?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/spring-beans.xsd"> <!-- 配置bean class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数 id:表示容器的bean,id唯一 --> <bean id="hello" class="logan.spring.study.HelloWorld"> </bean> <bean id="hello2" class="logan.spring.study.HelloWorld"> </bean> </beans>
这样就会报错
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘logan.spring.study.HelloWorld‘ available: expected single matching bean but found 2: hello,hello2 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1041) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093) at logan.spring.study.Main.main(Main.java:14)
所以在利用类型返回IOC容器中的Bean,但是要求IOC容器中必须只能有一个该类型的Bean。
时间: 2024-12-23 16:07:56