spring的基础
来源:互联网 发布:美工高级课程 编辑:程序博客网 时间:2024/05/19 13:27
Spring是一个控制反转和面向切面(AOP)的轻量级容器框架,是为了简化企业的开发
控制反转:应用本身不负责依赖对象的创建和维护,交给外部容器来维护和创建,这样,控制器就由应用转向了外部容器,控制权的转移就是所谓的反转,
private DaoBeans daobeans = new DaoBeans(); //这样的就是用应用来创建的,但是在spring中不会出现这样的代码,只会private DaoBeans daobeans ; 对象由spring容器来维护和创建
public void method(){
....
}
轻量级:轻量级和重量级的主要区别就是看其使用的服务多少,使用的服务越多,工作量就越多,所谓重量级也,对于spring容器而言,他也提供了很多的服务,但是好多的服务不是被默认打开的,如果需要使用的话,需要指定该服务,如果spring只使用了一些核心的服务的话,那他就是轻量级的,如果使用的大量的服务的话,那就是重量级的
spring的优点
·降低各层之间的耦合度
·可以使用容器提供的众多服务,如事务管理,消息管理
·容器提供单例模式的支持
·容器提供了AOP的支持
·容器提供了众多的辅助类
·对主流的框架提供了集成的支持
spring需要的jar包
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要
lib\aspectj/aspectjewaver.jar和aspectrt.jar
lib/cjlib\cglib-nodep-2.1_3.jar
如果使用了注解,如@resource、@PreDestroy等,还需要:
lib/j2ee/common-annotations.jar
Spring的一些基础知识:
——实例化容器://实例化容器
ApplicationContext ct = new ClassPathXmlApplicationContext(new String[]{"Beans.xml"});//配置文件可以是多个,所以可以是字符串数组
//得到里面的bean,通过getBean("beanIdname") 得到配置文件中的对象
ServiceBean sBean = (ServiceBean) ct.getBean("servicebean");
————spring默认为单例模式:
ServiceBean sBean = (ServiceBean) ct.getBean("servicebean");
ServiceBean sBean2 = (ServiceBean) ct.getBean("servicebean");
System.out.println(sBean==sBean2);//输出是true,这两个对象是相同的对象
如果想要每次getBean得到的是一个新的对象的话,可以使用
<bean id="servicebean" class="service.ServiceBean" scope="prototype">
//这个scope中还可以是request,session,global Sessio,对应的是web中的request,session,和application
————spring中bean的加载和销毁
·对应singleton(单例):在实例化容器的时候就实例化了bean对象
ApplicationContext ct = new ClassPathXmlApplicationContext(new String[]{"Beans.xml"});
·对应prototype(每次都是新对象的时候):在调用getBean的时候开始实例化
ServiceBean serviceBean = (ServiceBean) ct.getBean("servicebean");
销毁:
AbstractApplicationContext ct = new ClassPathXmlApplicationContext(new String[]{"Beans.xml"});
通过ct.close()来销毁容器;
可以在配置文件中指定:销毁的方法
<bean id="servicebean" class="service.ServiceBean" destroy-method="destroy"/>//指定要销毁的方法,这个方法肯定是出于这个bean对象中的
初始化方法
<bean id="servicebean" class="service.ServiceBean" init-method="initMethod"/>//通过init-method来指定该bean类中的初始化方法
——延迟加载(慎用)
如果对于单例模式的bean加载,我想要它不是在容器创建的时候加载的话,那么我应该这么设置呢?
<bean id="servicebean" class="service.ServiceBean" lazy-init="true"/> //通过lazy-init来指定,
如果对于整个配置文件中的bean都要延迟加载的话,那么可以在<beans... default-lazy-init="true">来指定
——bean实例化的三种模式:
1,由类构造器来完成,由spring的容器来完成(90%以上都是用这个)
<bean id="servicebean" class="service.ServiceBean" />
2,由静态工厂来创建
创建一个类,里面写个静态的方法来创建要创建的bean
public static ServiceBean createServiceBean(){
return new ServiceBean();
}
然后在bean中配置一下
<bean id="servicebean" class="test.CreateBean" factory-method="createServiceBean2">
3,由实例工厂来创建
和上面的类一样,但是这个类中不需要静态用静态的方法,在bean的配置中来指定是哪个方法
public ServiceBean createServiceBean2(){
return new ServiceBean();
}
配置:
<bean id="factoryBean" class="test.CreateBean"></bean>//先实例化创建bean的类
<bean id="servicebean" factory-bean="factoryBean" factory-method="createServiceBean2"></bean>
——依赖注入
在业务层中写private DaoBean daoBean ;并生成相应的setter和getter方法
<bean id="daobean" class="dao.DaoBean"/>
<bean id="servicebean" class="service.ServiceBean" >
<property name="daoBean" ref="daobean"></property>
</bean>
对于普通对象和集合的注入
要注意的是对于这里面的属性在类中药生成相应的setter和getter方法(手动注入)
<!-- 对于容器中普通的属性的注入 -->
<bean id="serviceBean2" class="service.ServiceBean2">
<!-- 对于普通属性的注入 -->
<property name="name" value="hwt"></property>
<!-- 对于set集合的注入 -->
<property name="set">
<set>
<value>第一个set元素</value>
<value>第二个set元素</value>
<value>第三个set元素</value>
</set>
</property>
<!-- 对于list的注入 -->
<property name="list">
<list>
<value>第一个list</value>
<value>第二个list</value>
<value>第三个list</value>
</list>
</property>
<!-- 对于properties的注入 -->
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="map">
<map>
<entry key="key_map_1" value="value_map_1"/>
<entry key="key_map_2" value="value_map_2"/>
<entry key="key_map_3" value="value_map_3"/>
</map>
</property>
</bean>
————构造函数注入
private String nameString = "wt";
private DaoBean daoBean ;
public ServiceBean2(String nameString,DaoBean daoBean) {
this.nameString = nameString;
this.daoBean = daoBean;
}
在配置文件中:
<bean id="serviceBean2" class="service.ServiceBean2">
<!-- 构造方法中的第index个元素赋值,如果是普通的方法的时候,就value来赋值,如果是bean对象,就要ref来映射 -->
<constructor-arg index="0" value="hwt"/>
<constructor-arg index="1" ref="daoBean"/>
</bean>
- spring的基础
- spring的基础
- SPRING BEAN的基础
- Spring的基础
- 【Spring】0 Spring基础:容器的作用
- Spring 的各个知识点 -- Spring基础
- spring基础包的引入
- Spring注解的基础用法
- Spring基础 Quartz的配置
- Spring最基础的知识
- Spring框架的基础使用(Spring框架的入门)
- spring-基础
- Spring基础
- Spring 基础
- Spring 基础
- spring 基础
- Spring基础
- spring基础
- 229. If you venture nothing, you will have nothing. 不入虎穴,焉得虎子
- (书摘)JavaServer Faces生命周期
- 快递查询接口 蛮不错
- END
- 任意前n个数求和
- spring的基础
- spring+jdbc
- spring的定时器
- 昨晚上去踢球了,膝盖疼的利害。
- struts1介绍
- 10个随机数中没有重复的数
- 推荐几款常用在线代码转换工具
- Android NDK 开发教程一:安装NDK
- Android测试教程(1):概述