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)

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. Python re 正则表达式简介

    1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...

  2. Python 基础 二

    Python 基础 二 今天对昨天学习的Python基础知识进行总结,学而不思则惘,思而不学则殆! 一.先对昨天学习的三大循环的使用情况进行总结: 1.while循环的本质就是让计算机在满足某一条件的 ...

  3. CSS3 制作网格动画效果

    在线演示      源码下载

  4. Linux 上搭建 git 的服务器

    搭建服务器 假设服务器的名字是 git.example.com. 首先,添加一个叫做git的用户adduser git. 然后如果不存在的话, 为这个用户新建一个主目录mkdir /home/git, ...

  5. Ajaxfileupload 总结(包括插件处理json格式bug的解决方案)

    Ajaxfileupload 是一款轻量级js的上传插件,简单容易上手,今天简单学习了下. 1,引用jquery和Ajaxfileupload .js <script src="~/S ...

  6. Hadoop的安装(日志四)

    上一篇:SSH免密码(日志三) 1,进入conf目录,查看所有的文件 2,第一个修改的是hadoop-env.sh的文件,配置javahome的地址 3,第二个就是配置core-site的文件,包括临 ...

  7. 简易版DES加密和解密详解

    在DES密码里,是如何进行加密和解密的呢?这里采用DES的简易版来进行说明. 二进制数据的变换 由于不仅仅是DES密码,在其它的现代密码中也应用了二进制数据,所以无论是文章还是数字,都需要将明文变换为 ...

  8. POJ1019-Number Sequence数学

    题目链接:http://poj.org/problem?id=1019 题目大意: 题目的意思很清楚了,就是把数字的每一位都当成是单个的字母来对待,然后求第i位的数是哪一个.(1<=i<= ...

  9. [笔记]ACM笔记 - 排序小技巧

    Description 一个数组,要求先对前n个数字排序(以方便后续操作):又要求对前n+i个数字排序:又要求对前n+j - 前n+k个数字排序(i.j.k的大小远小于n,且i.j.k间没有大小关系) ...

  10. python爬虫第一天

    python爬虫第一天 太久没折腾爬虫 又要重头开始了....感谢虫师大牛的文章. 接下来的是我的随笔 0x01 获取整个页面 我要爬的是百度贴吧的图,当然也是跟着虫师大牛的思路. 代码如下: #co ...