1.Spring项目依赖的jar包有5个:
2.applicationContext.xml文件中,如下bean的property的name值对应的是HelloWorld类中的setter方法,即name对应的是getName(),如果setter方法写为setName1,则name应该写为name="name1"。
<bean id = "helloWorld" class="com.swl.spring.beans.HelloWorld"> <property name="name" value="long"></property> </bean>
public class HelloWorld { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void hello(){ System.out.println("hello "+name); } }
3.如果按照第11行的方式加载applicationContext.xml文件时,应该把applicationContext.xml文件放在文件夹src下,与包同一级别,而非包中,见图。
package com.swl.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args){ //这两步由Spring完成 // HelloWorld helloWorld = new HelloWorld(); // helloWorld.setName("song "); //加载applicationContext.xml文件,创建ApplicaitonContext,从中取出bean ApplicationContext ctx = HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello"); helloWorld.hello(); } }
如果像下图这样将applicationContext.xml文件放在包中,语句
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
会报错
六月 11, 2017 8:59:46 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4d405ef7: startup date [Sun Jun 11 20:59:46 CST 2017]; root of context hierarchy 六月 11, 2017 8:59:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.swl.spring.beans.Main.main(Main.java:11) Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ... 13 more
4.我们观察下面代码11行做了什么工作
//Main类 1 package com.swl.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args){ // HelloWorld helloWorld = new HelloWorld(); // helloWorld.setName("song "); //创建Spring IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); // HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello"); // helloWorld.hello(); } }
//HelloWorld类 1 package com.swl.spring.beans; public class HelloWorld { private String name; public HelloWorld() { System.out.println("HelloWorld Constructor"); } public String getName() { return name; } public void setName(String name) { System.out.println("HelloWorld setName"); this.name = name; } public void hello(){ System.out.println("hello "+name); } }
//applicationContext.xml文件<?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 id = "hello" class="com.swl.spring.beans.HelloWorld"> <property name="name" value="long"></property> </bean> </beans>
无参构造函数HelloWorld和setName()方法都输出标识语句,运行,在控制台可以看到如下输出
六月 11, 2017 9:14:30 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4d405ef7: startup date [Sun Jun 11 21:14:30 CST 2017]; root of context hierarchy 六月 11, 2017 9:14:30 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] HelloWorld Constructor HelloWorld setName
可以看到,语句
11 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
执行,创建IOC容器对象过程中,会调用构造函数构造bean,并调用setName()方法为name属性赋值。
5.通过上面全类名的方式来配置Bean,用到了Java的反射机制,这就要求Bean必须要有一个无参的构造器;如果没有,会报错。
我们将HelloWorld的构造器改成有参数的,其余代码和4中相同:
public HelloWorld(String s) { System.out.println("HelloWorld Constructor"); }
运行,会报出如下错误:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.swl.spring.beans.HelloWorld]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.swl.spring.beans.HelloWorld.<init>() at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ... 13 more
6.也可以利用类型来获取IOC中的Bean,如下
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); // HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello"); HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
这种方式的缺点是在applicationContext.xml文件中配置两个类型相同的Bean时
<bean id = "hello" class="com.swl.spring.beans.HelloWorld"> <property name="name" value="long"></property> </bean> <bean id = "hello2" class="com.swl.spring.beans.HelloWorld"> <property name="name" value="long"></property> </bean>
虽然可以运行,但是报错
HelloWorld Constructor HelloWorld setName HelloWorld Constructor HelloWorld setName Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.swl.spring.beans.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:1090) at com.swl.spring.beans.Main.main(Main.java:13)