>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 21755 个阅读者 刷新本主题
 * 贴子主题:  Spring入门基础知识 回复文章 点赞(0)  收藏  
作者:flybird    发表时间:2020-06-18 01:00:01     消息  查看  搜索  好友  邮件  复制  引用

  Spring 基础
 
 
一、认识Bean
 
在Spring中,那些组成应用的主体(backbone)及由Spring IoC容器所管理的对象被称之为bean。简单地讲,bean就是由Spring容器初始化、装配及被管理的对象,除此之外,bean就没有特别之处了(与应用中的其他对象没有什么区别)。而bean定义以及bean相互间的依赖关系将通过配置元数据来描述。
 
二、认识BeanFactory
 
org.springframework.beans.factory.BeanFactory是Spring IoC容器的实际代表者,IoC容器负责容纳此前所描述的bean,并对bean进行管理。
 
BeanFactory负责读取Bean定义文件,管理对象的加载、生成、维护Bean对象与Bean对象之间的依赖关系,负责Bean的生命周期,对于简单的应用程序来说,使用BeanFactory就足够来管理Bean,在对象的管理上就可以获得许多的便利性。
 
BeanFactory是整个Spring围绕的重点。它负责读取Bean配置管理。可以借由getBean()方法来获取Bean的实例。
 
三、ApplicationContext
 
不过作为一个应用程序框架,只提供Bean容器管理的功能是不够的,若要利用Spring所提供的一些特色以及高级的容器功能,则可以使用BeanFactory的子接口ApplicationContext,此接口的基本功能与BeanFactory接口很相似,另外还提供了一个应用程序所需的更完整的框架功能:
1、提供获取资源文件的更方便的方法;
2、提供文字消息解析的方法;
3、支持国际化消息;
4、ApplicationContext可以发布时间,对时间感兴趣的Bean可以接收到这些事件。
 
 
简而言之,BeanFactory提供了配制框架及基本功能,而ApplicationContext则增加了更多支持企业核心内容的功能。ApplicationContext完全由BeanFactory扩展而来,因而BeanFactory所具备的能力和行为也适ApplicationContext
 
Spring的创始者Rod Johnson建议使用ApplicationContext来取代BeanFactory,在实现ApplicationContext的类中,最常使用的大概是一下三个:
org.springframework.context.support.ClassPathXmlApplicationContext
org.springframework.context.support.FileSystemXmlApplicationContext
org.springframework.web.context.support.XmlWebApplicationContext
 
 
四、接口选择之惑
 
在实际应用中,用户有时候不知道到底是选择BeanFactory接口还是ApplicationContext接口。但是通常在构建J2EE应用时,使用ApplicationContext将是更好的选择,因为它不仅提供了BeanFactory的所有特性,同时也允许使用更多的声明方式来得到我们想要的功能。
 
五、Spring的Bean配置
 
Spring支持三种配置元数据格式:XML格式、Java属性文件格式或使用Spring公共API编程实现。
 
六、实例化Spring Ioc容器
 
Resource resource = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
 
ClassPathResource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
 
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
 
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) context;
 
七、Spring Demo
 
注入一个Bean,通过Bean上下文工具获取Bean对象并输出其中的属性。
 

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-17 16:53:54<br>
* <b>Note</b>: 测试Bean
*/

public class Person {
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}
 

package com.lavasoft.springnote.ch01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-18 15:00:46<br>
* <b>Note</b>: Spring上下文工具
*/

public class BeanContextHelper {
    private static ApplicationContext applicationContext;

    static {
        applicationContext = buildApplicationContext();
    }

    private BeanContextHelper() {
    }

    /**
     * 重新构建ApplicationContext对象
     * @return ApplicationContext
     */

    public static ApplicationContext buildApplicationContext() {
        return new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    /**
     * 获取一个ApplicationContext对象
     * @return ApplicationContext
     */

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

}
 


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="person" class="com.lavasoft.springnote.ch01.Person">
        <property name="name">
            <value>lavasoft</value>
        </property>
        <property name="age">
            <value>22</value>
        </property>
    </bean>
</beans>
 

package com.lavasoft.springnote.ch01;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-17 16:58:54<br>
* <b>Note</b>: BeanFactory获取工具(测试版)
*/

public class SpringUtils {
    private static SpringUtils _instance = new SpringUtils();

    private SpringUtils() {
    }

    public static ClassPathXmlApplicationContext getClassPathXmlApplicationContext() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        return context;
    }

    public static BeanFactory getBeanFactory() {
        Resource rs = new FileSystemResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(rs);
        return factory;
    }
}
 

package com.lavasoft.springnote.ch01;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-17 16:58:07<br>
* <b>Note</b>: 客户端测试
*/

public class Test {
    public static void main(String args[]) {
        Test test = new Test();
        test.test3();
//        test.test2();

    }

    public void test1() {
        //bean的配置文件“/applicationContext.xml”必须位于CLASSPATH下
        BeanFactory factory = SpringUtils.getBeanFactory();
        Person person = (Person) factory.getBean("person");
        System.out.println(person.getName());
        System.out.println(person.getAge());
    }

    public void test2(){
        //bean的配置文件“/applicationContext.xml”必须位于源码src下
        ApplicationContext context = SpringUtils.getClassPathXmlApplicationContext();
        Person person = (Person) context.getBean("person");
        System.out.println(person.getName());
        System.out.println(person.getAge());
    }

    public void test3(){
        ApplicationContext context = BeanContextHelper.getApplicationContext();
        Person person = (Person) context.getBean("person");
        System.out.println(person.getName());
        System.out.println(person.getAge());

    }
}

 
运行结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
lavasoft
22

Process finished with exit code 0

 
 
 
 


----------------------------
原文链接:https://blog.51cto.com/lavasoft/73432



程序猿的技术大观园:www.javathinker.net
  Java面向对象编程-->Java注解
  JavaWeb开发-->自定义JSP标签(Ⅰ)
  JSP与Hibernate开发-->Java对象持久化技术概述
  Java网络编程-->XML数据处理
  精通Spring-->Vue组件开发基础
  Vue3开发-->Vue CLI脚手架工具
  Maven 安装及环境配置
  10分钟认识RocketMQ!想进阿里连这个都不会?
  使用Spring MVC处理404错误的方法
  springMVC:HandlerInterceptor拦截器的使用
  vue父、子组件相关的传递
  Spring MVC 通过@Value注解读取.properties文件中的内容
  Redis solr一二事 - 在spring中使用jedis 连接调试单机redi...
  Spring Cloud构建微服务架构: 消息总线
  理解 RESTful 风格的 API
  RabbitMQ的用途、原理以及配置
  一文详解微服务架构(好文值得细读)
  Spring Boot集成shiro ehcache(使用shiro的缓存管理)
  阿里面试官问我:如何用Redis设计秒杀系统?我的回答让他比起...
  Spring Framework 组件注册 之 @Import
  Java核心库实现AOP过程
  更多...
 IPIP: 已设置保密
楼主      
1页 1条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。