zoukankan      html  css  js  c++  java
  • Spring框架学习

    【资料】 ★★☆  Spring 中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实作这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会 注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。  

    【资料】★★☆通过ApplicationContextAware获取bean

    【资料】★★☆在非web应用的系统中怎样手动去销毁Spring容器   

    【资料】★★★★★ 详解 Spring 3.0 基于 Annotation 的依赖注入实现

    -------------------------------------分割线  上面是转载的部分内容,下面是个人的学习总结---------------------------------

    Spring框架在java中应用非常广泛。配制方法也有很多种。其本身在jsp项目中Myeclipse整合的很好。自动生成相对应的xml配置文件。

    我自己学习,记录一下自己在java项目中使用Spring的配置

    Spring框架提供了一个抽象接口ApplicationContextAware接口

    接口内部有一个setApplicationContext()方法

    在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的

    public void setApplicationContext(ApplicationContext context) throws BeansException方法,获得ApplicationContext对象。

    新建一个类 实现这个接口

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class SpringContext implements ApplicationContextAware{
        private static ApplicationContext applicationContext;
    
      @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{ AppContext.applicationContext = applicationContext; } public static Object getBean(String beanName){ if (beanName == null) { return null; } return applicationContext.getBean(beanName); } }

    配置文件中添加下面一条。因为不需要任何其他属性,所以不需要写任何其他东西

    <bean class="dev.server.config.spring.SpringContext"></bean>
    

     这么配置之后。在代码中或者实际使用中。我们可以使用

    SpringContext.getBean("contextName");

    来直接获取想要获取的bean了。

    ApplicationContext 接口的最常用的实现类是 ClassPathXmlApplicationContextFileSystemXmlApplicationContext,以及面向 Portlet 的 XmlPortletApplicationContext 和面向 web 的 XmlWebApplicationContext,它们都是面向 XML 的。

    Spring 3.0 新增了另外两个实现类:AnnotationConfigApplicationContext 和 AnnotationConfigWebApplicationContext

    详细内容可以直接参考五星级资料

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    ctx.registerShutdownHook();
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    
    @Configuration
    @ImportResource("classpath:resource/beans.xml")
    public class SpringConfig {
    
    }

    配置的内容全部都在beans的xml文件中。

    在web应用的项目中,如果项目服务关闭了,spring会自动销毁创建的容器,但是在非web应用的项目中,则需要我们手动去销毁。spring在AbstractApplicationContext中定义了一个registerShutdownHook()的方法,只要我们调用该方法就行了。

    Spring 2.5 在 @Repository 的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:

    • @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
    • @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
    • @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。

    自动检索基础包dev.server之下的全部文件。

    <context:annotation-config />
    <context:component-scan base-package="dev.server" />
     
  • 相关阅读:
    Windows API
    c# 各类型数据库连接字符串格式
    [C#/C++]C#调用非托管DLL的APIs
    (F#) How to enable F# template working under Visual Studio 2010 Shell.
    ubuntu文件、目录操作基本命令
    javascript curry
    C#中组件与控件的主要区别是什么?
    下拉菜单
    js 尺寸和位置 笔记
    $.each
  • 原文地址:https://www.cnblogs.com/zou90512/p/3140125.html
Copyright © 2011-2022 走看看