Spring

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,IOC)