`

Spring

    博客分类:
  • J2EE
阅读更多
Spring API & Reference:
http://static.springsource.org/spring/docs/current/


Spring 常用注解说明:
http://www.techferry.com/articles/spring-annotations.html#Scope



使用Placeholder读取properties文件之四种方式:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util-properties
1 PropertyPlaceholderConfigurer
2 PropertiesFactoryBean
3 <context:property-placeholder location="classpath:com/foo/jdbc.properties"/>
4 <util:properties id="globalProperties" location="configuration.properties" />
util schema例子:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util-properties


通过注解方式实现将bean纳入spring ioc管理的方式种种:
@Component @Controller @Service @Repository
后三个全是@Component,他们三个其实只不过是为了方便在三层架构中更好的区分各层的bean,对应每层的特定的@Component。
http://forum.springsource.org/showthread.php?71068-Component-vs-Service
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning
引用
@Component serves as a generic stereotype for any Spring-managed component; whereas, @Repository, @Service, and @Controller serve as specializations of @Component for more specific use cases (e.g., in the persistence, service, and presentation layers, respectively). What this means is that you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects.
另外还有JSR-330中定义的 @Named(a standard equivalent to the @Component annotation)。
还有一种@Bean的方式(和@Configuration配合使用):
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-java
引用
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.
@Configuration
public class AppConfig {
  @Bean
  public MyService myService() {
      return new MyServiceImpl();
  }
}
is equivalent to:
<beans>
  <bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>



Dependency Inject:
1 xml方式
xml方式的5种 autowire mode:
http://springindepth.com/book/in-depth-ioc-autowiring.html
http://www.mkyong.com/spring/spring-auto-wiring-beans-in-xml/
2 注解方式
注解方式实现DI(dependency inject)的方式种种:
@autowired @qualifier @resource @Inject
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/
http://www.springindepth.com/book/annotation-based-bean-config-dependency-injection.html




xml bean配置方式中的<bean id="base" abstract="true">、<bean id=“xxx”class="Xxx" parent="base"/>这种抽象配置,在注解的bean配置方式中,就是一个不被spring管理的抽象类(依赖的注入在该抽象类中)+被spring管理的抽象类实现类:
http://stackoverflow.com/questions/2515367/static-factory-method-spring
引用
<bean id="base" abstract="true">
    <property name="foo" ref="bar"/>
</bean>
<bean class="Wallace" parent="base"/>
<bean class="Gromit" parent="base"/>
is more or less eqivalent to this code (note that I created artificial Base class since abstract beans in Spring don't need a class(xml配置方式中的abstract bean是不需要有实际存在的Class文件的)):
public abstract class Base {
    @Autowired
    protected Foo foo;
}

@Component
public class Wallace extends Base {}

@Component
public class Gromit extends Base {}
Wallace and Gromit now have access to common Foo property. Also you can override it, e.g. in @PostConstruct.



BeanFactory vs ApplicationContext,一般请优先选择ApplicationContext:
http://stackoverflow.com/questions/243385/new-to-spring-beanfactory-vs-applicationcontext


容易混淆的BeanFactory和FactoryBean:
http://fluagen.blog.51cto.com/146595/38102
关于FactoryBean:
http://blog.springsource.org/2011/08/09/whats-a-factorybean/


想要让spring compoment bean 在被spring创建后立马做些事情,可以:
1 让该bean实现InitializingBean接口,并重写afterPropertiesSet()方法将欲做的事情写入其中;
2 bean定义是基于xml的话可以使用init-method,如:
<bean id="xxxService" class="..." init-method="init" />
3 注解的方式,将欲做的事情写在该bean的一个普通的方法里,并将该方法标记为@PostConstruct。
推荐使用注解的方式。
bean的do-some-stuff-before-destroy也有以上三种的对应方式。
如果一个bean中,同时使用了三种中的多个,它们被调用的顺序是咋样的那?
引用
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lifecycle-combined-effects
If multiple lifecycle mechanisms are configured for a bean, and each mechanism is configured with a different method name, then each configured method is executed in the order listed below. However, if the same method name is configured - for example, init() for an initialization method - for more than one of these lifecycle mechanisms, that method is executed once, as explained in the preceding section.
Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:
1. Methods annotated with @PostConstruct
2. afterPropertiesSet() as defined by the InitializingBean callback interface
3. A custom configured init() method
Destroy methods are called in the same order:
1. Methods annotated with @PreDestroy
2. destroy() as defined by the DisposableBean callback interface
3. A custom configured destroy() method
除以上三种方法外,另外还有一种:
实现 SmartLifecycle 接口,并重写其相应方法(start()等),重写的isAutoStartup()返回true。注意必须是 SmartLifecycle ,其父接口 Lifecycle 是不具备“Ioc container加载时立即调用start()方法”的功能的,是需要显式调用的,而SmartLifecycle的start()方法是可以在Ioc container加载时被自动调用的(isAutoStartup()需返回true)。(Lifecycle is meant for explicit invoking. Only SmartLifecycle will be invoked by the spring framework automatically)
@PostConstruct / afterPropertiesSet() 方法被执行多次的问题:
原因是因为在不同的Ioc container中都被加载了;而spring单例只是针对 ioc container 说的,所以如果有多个ioc container(比如 war 项目中的 Spring Root Context 与 Servlet Web Context 就是两个),最好是分开他们,各自 scan 各自的包。



about Spring Bean Scopes:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes
http://www.mkyong.com/spring/spring-bean-scopes-examples/
引用
In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller.
5 types of bean scopes supported :
singleton – Return a single bean instance per Spring IoC container
prototype – Return a new bean instance each time when requested
request – Return a single bean instance per HTTP request. *
session – Return a single bean instance per HTTP session. *
globalSession – Return a single bean instance per global HTTP session. *
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
Please be aware that Spring's concept of a singleton bean is quite different from the Singleton pattern as defined in the seminal Gang of Four (GoF) patterns book. The GoF Singleton hard codes the scope of an object such that one and only one instance of a particular class will ever be created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean.



使用xml的方式,可以很轻松的将同一个类注册为多个spring bean。如果是使用注解的方式,该怎么实现那?
http://stackoverflow.com/questions/2902335/instantiating-multiple-beans-of-the-same-class-with-spring-annotations
看的出来,不是不可以做到,但是比较麻烦,增加了代码的复杂性,可读性也不见得好。可以的出的结论就是,注解的方式,并不能完全替代xml的方式。
使用xml的方式将同一个class注册为多个spring beans后,使用注解的方式在需要用到这些beans的地方对其做注入,怎么明确指定注入哪个bean那?有两种方式:
1 使用 @Autowired + @Qualifier
@Autowired
@Qualifier("idOfBean")
2 使用 @Resource
@Resource(name="idOfBean")




IoC container & ApplicationContext:
spring bean 的所谓singleton,指的是对bean definition,在一个IoC container中是单例的。如果是以下的情况:
<bean id="customer1" class="com.sam.Customer">  
<bean id="customer2" class="com.sam.Customer">  
则Customer类被在两个bean definition中使用,故即使在同一个IoC container中,Customer也不是单例的,只是customer1和customer2两个BeanDefinition是单例的。
web 容器,如tomcat,与IoC container容器间的关系是什么????
IoC container是和ApplicationContext|BeanFactory对应的。。。。so???




srcs:
详解 Spring 3.0 基于 Annotation 的依赖注入实现
http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-iocannt/index.html?ca=drs-tp4608
分享到:
评论

相关推荐

    Spring+SpringMVC+Mybatis框架整合例子(SSM) 下载

    Spring、SpringMVC和Mybatis是Java开发中最常用的三大开源框架,它们的整合使用,通常被称为SSM框架。这个框架组合提供了完整的后端服务解决方案,包括依赖注入(DI)、面向切面编程(AOP)、模型-视图-控制器(MVC...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    SpringBatch+Spring+Mybatis+MySql (spring batch 使用jar)

    Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...

    spring3.0.5 所有jar文件

    包含spring 3.0.5的所有jar文件: org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE...

    spring2.0升级到spring3.0.5的开发包

    Spring框架是Java应用程序开发中的一个核心组件,它提供了一个丰富的IOC(Inversion of Control,控制反转)和AOP(Aspect-Oriented Programming,面向切面编程)功能,使得开发者能够更方便地管理对象和实现模块化...

    Spring Boot整合Spring Batch,实现批处理

    在Java开发领域,Spring Boot和Spring Batch的整合是构建高效批处理系统的一种常见方式。Spring Boot以其简洁的配置和快速的启动能力深受开发者喜爱,而Spring Batch作为Spring框架的一部分,专注于批量处理任务,...

    Spring Cloud Gateway 整合 Spring Security 统一登录认证鉴权

    在构建分布式系统时,Spring Cloud Gateway 作为微服务架构中的边缘服务或 API 网关,扮演着至关重要的角色。它负责路由请求到相应的微服务,并可以提供过滤器功能,如限流、熔断等。而Spring Security 则是 Java ...

    spring3.1 官方全部jar包

    spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....

    Spring MVC 入门实例

    这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...

    Getting started with Spring Framework: covers Spring 5(epub)

    Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...

    基于Spring Boot 3.0、 Spring Cloud 2022 & Alibaba 的微服务RBAC 权限管理系统

    介绍一个基于Spring Boot 3.0、Spring Cloud 2022 & Alibaba的微服务RBAC权限管理系统。该系统可以实现微服务RBAC权限管理,通过RBAC权限管理机制对用户访问系统的权限进行限制,从而提高系统的安全性和可用性。同时...

    最新版本的Struts2+Spring4+Hibernate4框架整合

    项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...

    Spring cloud与Spring boot 集成完整案例

    Spring Cloud和Spring Boot是两个非常重要的Java开发框架,它们在微服务架构中扮演着核心角色。Spring Boot简化了创建独立的、生产级别的基于Spring的应用程序的过程,而Spring Cloud则为开发者提供了快速构建分布式...

    spring-ai-core 0.8.1

    《Spring AI Core 0.8.1:开启人工智能之旅》 在现代软件开发领域,Spring框架以其强大的功能和灵活性,已经成为Java开发中的首选框架之一。而Spring AI Core则是Spring生态系统中专门为人工智能(AI)和机器学习...

    Spring技术内幕:深入解析Spring架构与设计原理

    《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从本书中参透Spring框架的出色架构和设计思想,还能从...

    spring 4.3.14(全)最新的spring4正式版

    Spring 框架是 Java 开发中的一个核心组件,它为构建企业级应用程序提供了全面的编程和配置模型。Spring 4.3.14 是该框架的最后一个4.x系列正式版,发布于2018年2月24日。这个版本在Spring 5.0发布之前提供了一个...

    SpringCloud项目实战各组件源代码案例

    Spring Cloud系列教程 Spring Boot Spring Cloud Stream 和 Kafka案例教程 springcloud生产者与消费者项目实战案例 Spring Cloud 中断路器 Circuit Breaker的应用 配置 Spring Cloud Config Server Spring Cloud ...

    spring整合rabbitmq需要的jar包(spring版本4.2.0)

    在IT行业中,Spring框架是Java应用开发中的一个关键组件,它提供了一整套服务来简化企业级应用的构建。RabbitMQ则是一个流行的开源消息队列系统,它基于AMQP(Advanced Message Queuing Protocol)协议,用于高效地...

    精通Spring MVC 4

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...

    spring v3.2源码

    个人觉得spring3.2的源代码比较好编译,没啥脾气,像我之前下载的spring的最新版本源代码,还有spring4.0的源代码,不论是使用jdk1.6还是1.7甚至是1.8编译都出问题,结果还是觉得spring3.2编译过程轻松一点,配合...

Global site tag (gtag.js) - Google Analytics