现在的位置: 首页 > 综合 > 正文

Spring配置使用

2018年05月14日 ⁄ 综合 ⁄ 共 1118字 ⁄ 字号 评论关闭

Spring配置使用

控制反转(IOC)应用把对象控制权交给spring容器管理。反转指控制权的转移。

面向切面(AOP)拦截方法,用户没有权限就不让用户执行业务中的某些方法。

配置文件:applicationContext.xml,将bean放入Spring容器

头部:scheme

spring创建对象的方式:

1.默认调用构造函数

2.静态工厂方法创建

3. spring模式setter方式

 

bean配置:

<bean id="personService" class="com.lid.service.PersonServiceBean"/>

bean的作用域:  

默认是单利模式,及只创建一次bean   

如果想没调用一次就创建一个新的bean对象的话,只需指定一下作用域即可如下:  

<bean id="personService" class="com.lid.service.PersonServiceBean" scope="prototype"/>  

 

bean实例化:

1.默认方式:单例模式,当spring加载时创建

2.延时方式:则bean是在spring容器实例化以后,调用springgetBean方法时

 

从容器中取出bean实例:

创建spring核心实例ApplicationContext

ApplicationContext cxt = new ClassPathXmlApplicationContext(bean.xml);

PersonService ps = ctx.getBean("personService" ,PersonService .class);

 

依赖注入:

spring通过配置可以为Bean注入属性值(其属性值本身就是一个对象)等

系统自动提供所需要的实例,无需程序显示new获取。

spring通过xml配置文件来指定实例之间的依赖关系。

设值注入:

先通过无参构造器构造一个实例,然后调用setter方法注入依赖关系

<property name=”axe” ref=”stoneAxe” />

构造注入:

通过含参构造器实现依赖关系注入

<constructor-arg ref=”steelAxe” />

 

国际化支持:

<bean id=”messageSource”

class=”org.springframework.context.support.ResourceBundleMessageSource” />

<property name=”basenames”>

<list>

<value>messageBaseName</value>

.....

</list>

</property>

</bean>

 

【上篇】
【下篇】

抱歉!评论已关闭.