码迷,mamicode.com
首页 > 编程语言 > 详细

Spring入门第二课

时间:2017-05-18 22:10:04      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:...   .com   uniq   获取   准备   初始化   实例化   expect   函数   

看代码

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.springframework.context.support.ClassPathXmlApplicationContext@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...
logan.spring.study.HelloWorld@48eff760

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。

Spring入门第二课

标签:...   .com   uniq   获取   准备   初始化   实例化   expect   函数   

原文地址:http://www.cnblogs.com/LoganChen/p/6869862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!