1.Spring项目依赖的jar包有5个:

2.applicationContext.xml文件中,如下bean的property的name值对应的是HelloWorld类中的setter方法,即name对应的是getName(),如果setter方法写为setName1,则name应该写为name="name1"。

  1. <bean id = "helloWorld" class="com.swl.spring.beans.HelloWorld">
  2. <property name="name" value="long"></property>
  3. </bean>
  1. public class HelloWorld {
  2. private String name;
  3.  
  4. public String getName() {
  5. return name;
  6. }
  7.  
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11.  
  12. public void hello(){
  13. System.out.println("hello "+name);
  14. }
  15. }

3.如果按照第11行的方式加载applicationContext.xml文件时,应该把applicationContext.xml文件放在文件夹src下,与包同一级别,而非包中,见图。

  1. package com.swl.spring.beans;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class Main {
  7. public static void main(String[] args){  //这两步由Spring完成
  8. // HelloWorld helloWorld = new HelloWorld();
  9. // helloWorld.setName("song ");
  10. //加载applicationContext.xml文件,创建ApplicaitonContext,从中取出bean
  11. ApplicationContext ctx = HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello");
  12. helloWorld.hello();
  13. }
  14. }

如果像下图这样将applicationContext.xml文件放在包中,语句

  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

会报错

  1. 六月 11, 2017 8:59:46 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4d405ef7: startup date [Sun Jun 11 20:59:46 CST 2017]; root of context hierarchy
  3. 六月 11, 2017 8:59:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from class path resource [applicationContext.xml]
  5. 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
  6. at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
  7. at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
  8. at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
  9. at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
  10. at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
  11. at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
  12. at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
  13. at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
  14. at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
  15. at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)
  16. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)
  17. at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
  18. at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
  19. at com.swl.spring.beans.Main.main(Main.java:11)
  20. Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
  21. at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
  22. at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
  23. ... 13 more

4.我们观察下面代码11行做了什么工作

  1. //Main类 1 package com.swl.spring.beans;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class Main {
  7. public static void main(String[] args){
  8. // HelloWorld helloWorld = new HelloWorld();
  9. // helloWorld.setName("song ");
  10. //创建Spring IOC容器对象
  11. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  12. // HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello");
  13. // helloWorld.hello();
  14. }
  15. }
  1. //HelloWorld类 1 package com.swl.spring.beans;
  2.  
  3. public class HelloWorld {
  4. private String name;
  5.  
  6. public HelloWorld() {
  7. System.out.println("HelloWorld Constructor");
  8. }
  9.  
  10. public String getName() {
  11. return name;
  12. }
  13.  
  14. public void setName(String name) {
  15. System.out.println("HelloWorld setName");
  16. this.name = name;
  17. }
  18.  
  19. public void hello(){
  20. System.out.println("hello "+name);
  21. }
  22. }
  1. //applicationContext.xml文件<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id = "hello" class="com.swl.spring.beans.HelloWorld">
  6. <property name="name" value="long"></property>
  7. </bean>
  8. </beans>

无参构造函数HelloWorld和setName()方法都输出标识语句,运行,在控制台可以看到如下输出

  1. 六月 11, 2017 9:14:30 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4d405ef7: startup date [Sun Jun 11 21:14:30 CST 2017]; root of context hierarchy
  3. 六月 11, 2017 9:14:30 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from class path resource [applicationContext.xml]
  5. HelloWorld Constructor
  6. HelloWorld setName

可以看到,语句

  1. 11 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

执行,创建IOC容器对象过程中,会调用构造函数构造bean,并调用setName()方法为name属性赋值。

5.通过上面全类名的方式来配置Bean,用到了Java的反射机制,这就要求Bean必须要有一个无参的构造器;如果没有,会报错。

我们将HelloWorld的构造器改成有参数的,其余代码和4中相同:

  1. public HelloWorld(String s) {
  2. System.out.println("HelloWorld Constructor");
  3. }

运行,会报出如下错误:

  1. 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>()
  2. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
  3. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147)
  4. ... 13 more

6.也可以利用类型来获取IOC中的Bean,如下

  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  2. // HelloWorld helloWorld = (HelloWorld) ctx.getBean("hello");
  3. HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

这种方式的缺点是在applicationContext.xml文件中配置两个类型相同的Bean时

  1. <bean id = "hello" class="com.swl.spring.beans.HelloWorld">
  2. <property name="name" value="long"></property>
  3. </bean>
  4.  
  5. <bean id = "hello2" class="com.swl.spring.beans.HelloWorld">
  6. <property name="name" value="long"></property>
  7. </bean>

虽然可以运行,但是报错

  1. HelloWorld Constructor
  2. HelloWorld setName
  3. HelloWorld Constructor
  4. HelloWorld setName
  5. 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
  6. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1041)
  7. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
  8. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
  9. at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)
  10. at com.swl.spring.beans.Main.main(Main.java:13)

Spring 4学习——问题与注意事项(一)的更多相关文章

  1. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  2. Spring MVC 学习 -- 创建过程

    Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...

  3. 《Spring MVC学习指南》怎么样?答:书名具有很大的欺骗性

    2016年6月21日 最近,因为工作需要,我从网上买了一本<Spring MVC学习指南>,ISBN编号: 978-7-115-38639-7,定价:49.00元.此书是[美]Paul D ...

  4. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

  5. Spring security 学习 (自助者,天助之!)

    自己努力,何必要强颜欢笑的求助别人呢?  手心向下不求人! Spring security学习有进展哦: 哈哈! 1.页面都是动态生产的吧! 2.设置权限:  a:pom.xml配置jar包 b:cr ...

  6. 转-Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable

    转-http://snowolf.iteye.com/blog/1628861/ Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariab ...

  7. 【转】Spring 注解学习手札(超好的springmvc注解教程)

    Spring 注解学习手札(一) 构建简单Web应用 Spring 注解学习手札(二) 控制层梳理 Spring 注解学习手札(三) 表单页面处理 Spring 注解学习手札(四) 持久层浅析 Spr ...

  8. 【转】Spring.NET学习笔记——目录

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  9. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable (转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

随机推荐

  1. IT职场经纬 |阿里web前端面试考题,你能答出来几个?

    有很多小伙伴们特别关心面试Web前端开发工程师时,面试官都会问哪些问题.今天小卓把收集来的"阿里Web前端开发面试题"整理贴出来分享给大家伙看看,赶紧收藏起来做准备吧~~ 一.CS ...

  2. java线程的实现

    一共有两种方法Thread类和Runnable接口,相对来讲,更趋向于用Runnable因为一个类可以实现多个接口,但是只能继承一个类,所以相对来说倾向用Runnable 第一种方法:用Thread其 ...

  3. 【2017-04-24】winform基础、登录窗口、窗口属性

    一.winform基础  客户端应用程序:C/S 客户端应用程序可以操作用户电脑中的文件,代码要在用户电脑上执行,吃用户电脑配置. 窗体是由控件和属性做出来的 控件:窗体里所放的东西."视图 ...

  4. junit测试Android项目

    关于junit测试Android项目方法主要有一下步骤: 1.导入junit4的jar包 在工厂中Build Path中Add Library->JUnit->JUnit4->Fin ...

  5. Web API 之SelfHost与OwinSelfHots加载外部程序

       下面就一些web api的一些基础内容进行阐述,然后就web api宿主承载中的实际业务问题进行解决 HttpController      HttpController的激活是由处于消息处理管 ...

  6. Elasticsearch 全量遍历数据

    1,利用分页,from,to参数,但是当数据量特别大的时候(大约100w),分页是不现实的,排序排不开. 2,利用scan功能. 上 Python代码 from elasticsearch impor ...

  7. poj3160强连通分量加dfs

    After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such ...

  8. MongoDB副本集的常用操作及原理

    本文是对MongoDB副本集常用操作的一个汇总,同时也穿插着介绍了操作背后的原理及注意点. 结合之前的文章:MongoDB副本集的搭建,大家可以在较短的时间内熟悉MongoDB的搭建和管理. 下面的操 ...

  9. for循环-0,1,1,2,2可以组成多少个不重复的五位数?

    今天想到了一个比较有意思的for循环题:0,1,1,2,2可以组成多少个不重复的五位数? 它主要是for循环多层嵌套外加if判断,代码如下: protected void Button1_Click( ...

  10. Java8新特性-Lambda表达式

    1.  什么是Lambda表达式? Lambda表达式就是可以把函数作为参数传递,或者说把代码作为数据传递给函数. 2. Lambda表达式的语法格式 基本语法格式如下: 基本语法下多个变体的说明: ...