Spring

[b]Spring API & Reference:[/b]
[url]http://static.springsource.org/spring/docs/current/[/url]


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


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


通过注解方式实现将bean纳入spring ioc管理的方式种种:
@Component @Controller @Service @Repository
后三个全是@Component,他们三个其实只不过是为了方便在三层架构中更好的区分各层的bean,对应每层的特定的@Component。
[url]http://forum.springsource.org/showthread.php?71068-Component-vs-Service[/url]
[url]http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning[/url][quote]@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.[/quote]另外还有JSR-330中定义的 @Named(a standard equivalent to the @Component annotation)。
还有一种@Bean的方式(和@Configuration配合使用):
[url]http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-java[/url][quote]Annotating a class with the @Configuration indicates that [b]the class can be used by the Spring IoC container as a source of bean definitions[/b]. The @Bean annotation tells Spring that [b]a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context[/b].
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
is equivalent to:
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
[/quote]


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


xml bean配置方式中的<bean id="base" abstract="true">、<bean id=“xxx”class="Xxx" parent="base"/>这种抽象配置,在注解的bean配置方式中,就是一个不被spring管理的抽象类(依赖的注入在该抽象类中)+被spring管理的抽象类实现类:
[url]http://stackoverflow.com/questions/2515367/static-factory-method-spring[/url][quote]
<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 [b]abstract beans in Spring don't need a class(xml配置方式中的abstract bean是不需要有实际存在的Class文件的)[/b]):
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. [b]Also you can override it, e.g. in @PostConstruct[/b].[/quote]


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


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


想要让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中,同时使用了三种中的多个,它们被调用的顺序是咋样的那?[quote][url]http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lifecycle-combined-effects[/url]
[b]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.[/b]
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[/quote]除以上三种方法外,另外还有一种:
实现 SmartLifecycle 接口,并重写其相应方法(start()等),重写的isAutoStartup()返回true。注意必须是 SmartLifecycle ,其父接口 Lifecycle 是不具备“Ioc container加载时立即调用start()方法”的功能的,是需要显式调用的,而SmartLifecycle的start()方法是可以在Ioc container加载时被自动调用的(isAutoStartup()需返回true)。[b](Lifecycle is meant for explicit invoking. Only SmartLifecycle will be invoked by the spring framework automatically)[/b]
[b]@PostConstruct / afterPropertiesSet() 方法被执行多次的问题:[/b]
原因是因为在不同的Ioc container中都被加载了;而spring单例只是针对 ioc container 说的,所以如果有多个ioc container(比如 war 项目中的 Spring Root Context 与 Servlet Web Context 就是两个),最好是分开他们,各自 scan 各自的包。


about Spring Bean Scopes:
[url]http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes[/url]
[url]http://www.mkyong.com/spring/spring-bean-scopes-examples/[/url][quote]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 [b]default scope is singleton[/b].
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. [b]The scope of the Spring singleton is best described as [color=red]per container and per bean.[/color][/b][/quote]


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

@Autowired
@Qualifier("idOfBean")
2 使用 @Resource
@Resource(name="idOfBean")



[b]IoC container & ApplicationContext:[/b]
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 的依赖注入实现
[url]http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-iocannt/index.html?ca=drs-tp4608[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值