巫巫巫政霖

Bean装配的属性

PropertyExplained in…

class

Section5.3.2, “Instantiating beans”

name

scope

constructor arguments

Section5.4.1, “Dependency injection”

properties

Section5.4.1, “Dependency injection”

autowiring mode

Section5.4.5, “Autowiring collaborators”

lazy-initialization mode

Section5.4.4, “Lazy-initialized beans”

initialization-method

the section called “Initialization callbacks”

destruction-method

the section called “Destruction callbacks”

==="examples.ExampleBean" init-method="init"/>="examples.ExampleBean" destroy-method="cleanup"/>带参构造函数产生Bean===</bean>===</bean>Property方式给Bean赋值===========a list element followed by a reference=====just some string=

Bean的作用域

ScopeDescription

(默认)单例,一个Ioc容器只有一个Bean

一个Ioc容器有多个相同的Bean,每次获取都重新生成一个Bean

Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware SpringApplicationContext.

Scopes a single bean definition to the lifecycle of an HTTPSession. Only valid in the context of a web-aware SpringApplicationContext.

Scopes a single bean definition to the lifecycle of a global HTTPSession. Typically only valid when used in a portlet context. Only valid in the context of a web-aware SpringApplicationContext.

Scopes a single bean definition to the lifecycle of aServletContext. Only valid in the context of a web-aware SpringApplicationContext.

Bean的生命周期

Bean的初始化:

方法1:实现org.springframewoek.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法

方法2:配置init-method方法

Bean的销毁:

方法1:实现org.springframewoek.beans.factory.DisposableBean接口,覆盖destroy方法

方法2:配置destroy-method方法

配置全局默认初始化,销毁方法(在<beans标签内配置,这是一种可选方法,被特定设置覆盖,或当Bean没有这个方法,也不会影响Bean的生成 )

default-init-method="方法名" default-destroy-method="方法名"

基于注解的生命周期 @PostConstruct and @PreDestroy

public class CachingMovieLister {@PostConstructpublic void populateMovieCache() {// populates the movie cache upon initialization…}@PreDestroypublic void clearMovieCache() {// clears the movie cache upon destruction…}}

Bean的自动装配

Table5.2.Autowiring modes

ModeExplanation

no

(Default) No autowiring. Bean references must be defined via aref element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.

byName

根据id跟变量名完全一致装配

byType

根据类型装配

constructor

通过查找IOC容器是否有与构造函数参数同类型,有就自动装配,没有则报错

default-autowire="byName"

配置读取资源文件<span style="font-size:14px;"><beans><!– enable processing of annotations such as @Autowired and @Configuration –><context:annotation-config/><context:property-placeholder location="classpath:/com/acme/jdbc.properties"/><bean class="com.acme.AppConfig"/><bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean></beans></span>

去陌生的街角,去做一切我们曾经或现在也很想做的事情,

巫巫巫政霖

相关文章:

你感兴趣的文章:

标签云: