- 浏览: 1151725 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (411)
- Java Foundation (41)
- AI/机器学习/数据挖掘/模式识别/自然语言处理/信息检索 (2)
- 云计算/NoSQL/数据分析 (11)
- Linux (13)
- Open Source (12)
- J2EE (52)
- Data Structures (4)
- other (10)
- Dev Error (41)
- Ajax/JS/JSP/HTML5 (47)
- Oracle (68)
- FLEX (19)
- Tools (19)
- 设计模式 (4)
- Database (12)
- SQL Server (9)
- 例子程序 (4)
- mysql (2)
- Web Services (4)
- 面试 (8)
- 嵌入式/移动开发 (18)
- 软件工程/UML (15)
- C/C++ (7)
- 架构Architecture/分布式Distributed (1)
最新评论
-
a535114641:
LZ你好, 用了这个方法后子页面里的JS方法就全不能用了呀
页面局部刷新的两种方式:form+iframe 和 ajax -
di1984HIT:
学习了,真不错,做个记号啊
Machine Learning -
赵师傅临死前:
我一台老机器,myeclipse9 + FB3.5 可以正常使 ...
myeclipse 10 安装 flash builder 4.6 -
Wu_Jiang:
触发时间在将来的某个时间 但是第一次触发的时间超出了失效时间, ...
Based on configured schedule, the given trigger will never fire. -
cylove007:
找了好久,顶你
Editable Select 可编辑select
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
还有一种@Bean的方式(和@Configuration配合使用):
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-java
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
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的do-some-stuff-before-destroy也有以上三种的对应方式。
如果一个bean中,同时使用了三种中的多个,它们被调用的顺序是咋样的那?
实现 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/
使用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
IoC container & ApplicationContext:
spring bean 的所谓singleton,指的是对bean definition,在一个IoC container中是单例的。如果是以下的情况:
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
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
除以上三种方法外,另外还有一种:
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.
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
发表评论
-
Lucene & Solr
2013-05-07 17:30 2431Params of solr query (参见 solrj ... -
Continuous Integration Server:Jenkins & Hudson
2013-04-15 16:15 1478Jenkins: http://jenkins-ci.org/ ... -
Spring Integration
2013-03-26 16:52 3050Spring Integration Reference ... -
高可用与负载均衡:Haproxy(or lVS) + keepalived
2013-01-29 20:35 3199sources: Setting Up A High ... -
Spring Batch: 大数据量批量并行处理框架
2013-01-11 16:19 4850Spring Batch Documentati ... -
AOP: Aspect Oriented Programming
2013-01-06 11:13 2795The AspectJ Programming Gu ... -
Performance & Load test tool : JMeter
2012-12-18 14:28 1295Official: http://jmeter.apa ... -
rabbitmq & spring amqp
2012-12-04 00:09 8720My main AMQP post on blogger ... -
javaMail 邮件
2012-11-23 20:14 3506SMTP POP3的区别到底是什么? http://w ... -
未完 Spring MVC
2012-11-15 22:41 2136Annotations for Http pa ... -
JUnit 单元测试
2012-10-30 12:27 2577测试的分类: http://s ... -
Hibernate
2011-08-02 11:48 1190Hibernate缓存: 一级缓存的生命周期和session的 ... -
Maven Repository Management & Nexus
2011-07-30 11:39 1442Why do I need a Repositor ... -
XStream
2011-07-13 00:18 1388XStream 内置 Converters: http://x ... -
Hibernate注解: 联合主键:@IdClass vs @EmbeddedId
2011-07-12 21:01 15391Hibernate Annotations -> 2.2 ... -
Struts2 Tag 标签
2011-05-04 00:43 1644struts2 OGNL 中的#、%等符号的区别: Apach ... -
iBATIS cacheModel 缓存
2011-04-07 14:29 1362http://ibatis.apache.org/docs/d ... -
Template Engine: FreeMarker & Velocity
2011-01-16 22:56 4116FreeMarker http://freemar ... -
Struts2 标签 <s:url>中添加多个<s:param>的奇怪问题 待整理
2011-01-14 13:27 1925问题描述(我的回复): http://went3456.ite ... -
J2EE核心技术
2010-12-31 01:16 2667http://www.berheley.com/jsnl/J2 ...
相关推荐
Spring、SpringMVC和Mybatis是Java开发中最常用的三大开源框架,它们的整合使用,通常被称为SSM框架。这个框架组合提供了完整的后端服务解决方案,包括依赖注入(DI)、面向切面编程(AOP)、模型-视图-控制器(MVC...
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
java *spring工具类 方便在非spring管理环境中获取beanjava *spring工具类 方便在非spring管理环境中获取beanjava *spring工具类 方便在非spring管理环境中获取beanjava *spring工具类 方便在非spring管理环境中获取...
Spring Integration + Spring WS 整合 在 Java 领域中,Spring Integration 和 Spring WS 是两个常用的框架,它们分别负责集成系统和 Web 服务。今天,我们将探讨如何将这两个框架整合在一起,实现一个完整的 Web ...
包含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...
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...
Spring框架是Java应用程序开发中的一个核心组件,它提供了一个丰富的IOC(Inversion of Control,控制反转)和AOP(Aspect-Oriented Programming,面向切面编程)功能,使得开发者能够更方便地管理对象和实现模块化...
在Java开发领域,Spring Boot和Spring Batch的整合是构建高效批处理系统的一种常见方式。Spring Boot以其简洁的配置和快速的启动能力深受开发者喜爱,而Spring Batch作为Spring框架的一部分,专注于批量处理任务,...
在构建分布式系统时,Spring Cloud Gateway 作为微服务架构中的边缘服务或 API 网关,扮演着至关重要的角色。它负责路由请求到相应的微服务,并可以提供过滤器功能,如限流、熔断等。而Spring Security 则是 Java ...
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 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
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权限管理系统。该系统可以实现微服务RBAC权限管理,通过RBAC权限管理机制对用户访问系统的权限进行限制,从而提高系统的安全性和可用性。同时...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
Spring Cloud和Spring Boot是两个非常重要的Java开发框架,它们在微服务架构中扮演着核心角色。Spring Boot简化了创建独立的、生产级别的基于Spring的应用程序的过程,而Spring Cloud则为开发者提供了快速构建分布式...
《Spring AI Core 0.8.1:开启人工智能之旅》 在现代软件开发领域,Spring框架以其强大的功能和灵活性,已经成为Java开发中的首选框架之一。而Spring AI Core则是Spring生态系统中专门为人工智能(AI)和机器学习...
《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从本书中参透Spring框架的出色架构和设计思想,还能从...
Spring 框架是 Java 开发中的一个核心组件,它为构建企业级应用程序提供了全面的编程和配置模型。Spring 4.3.14 是该框架的最后一个4.x系列正式版,发布于2018年2月24日。这个版本在Spring 5.0发布之前提供了一个...
在IT行业中,Spring框架是Java应用开发中的一个关键组件,它提供了一整套服务来简化企业级应用的构建。RabbitMQ则是一个流行的开源消息队列系统,它基于AMQP(Advanced Message Queuing Protocol)协议,用于高效地...
Spring Cloud系列教程 Spring Boot Spring Cloud Stream 和 Kafka案例教程 springcloud生产者与消费者项目实战案例 Spring Cloud 中断路器 Circuit Breaker的应用 配置 Spring Cloud Config Server Spring Cloud ...