Spring学习(一)

Spring学习(一)
1.Spring简介
Spring是一个 轻量级的控制反转(IoC)和面向切面(AOP) 的容器框架。
·轻量级:体积很小,开销很小,无侵入性
·核心思想:IoC(Inverse of Control控制反转)/DI(Dependency Injection依赖注入),AOP(Aspect Oriented Programming )
 
2.框架部署
Spring2.5.6:
①导入Jar包:
*spring2.5.6/dist/spring.jar
*spring2.5.6/lib/dom4j/*.jar
*spring2.5.6/lib/Jakarta-commons/commons-logging.jar
②创建配置文件:
*在src目录中创建
*配置文件名为:applicationContext.xml
*引入规范(和DTD规范的引入方式不同)
a>根标签名为beans
b>找到spring-beans-2.5.xsd规范文档(org.springframework.beans.factory.xml包下)
c>三个属性:xmlns/xmlns:xsi/xsi:schemaLocation
d>属性值来自于spring-beans-2.5.xsd文件的第3~5行
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
③创建类:Student(stuId,stuName,stuSex)
④在applicationContext.xml中对创建的类进行配置
<beanid="stu"class="com.softeem.test.Student">
<propertyname="stuId"value="123"/>
<propertyname="stuName"value="王二麻子"/>
<propertyname="stuSex"value="男"/>
</bean>
⑤借助于ClassPathXmlApplicationContext类加载配置文件,然后调用getBean方法借助于Spring容器获取Student对象
ClassPathXmlApplicationContext c =
new ClassPathXmlApplicationContext("applicationContext.xml");
Student s = (Student)c.getBean("stu");
System.out.println(s.getStuName());
Spring3.2.4:
①导入Jar包:
*spring-beans-3.2.4.RELEASE.jar
*spring-core-3.2.4.RELEASE.jar
*commons-logging.jar
②创建配置文件:
*在src目录中创建
*配置文件名为:applicationContext.xml
*引入规范(和DTD规范的引入方式不同)
a>根标签名为beans
b>找到spring-beans-3.2.xsd规范文档
spring-beans-3.2.4.RELEASE.jar→org.springframework.beans.factory.xml
c>三个属性:xmlns/xmlns:xsi/xsi:schemaLocation
d>属性值来自于spring-beans-3.2.xsd文件的第3~5行
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
</beans>
③创建类:
publicclass Ticket {
	privateintticketId;
	private StringfromStation;
	private StringdestStation;
	privatefloatprice;
}
④在applicationContext.xml中对创建的类进行配置
<beanid="stu"class="com.softeem.test.Ticket">
<propertyname="ticketId"value="10001"/>
<propertyname="fromStation"value="武汉"/>
<propertyname="destStation"value="南京"/>
<propertyname="price"value="186.66"/>
</bean>
⑤借助于ClassPathXmlApplicationContext类加载配置文件,然后调用getBean方法借助于Spring容器获取Student对象
ClassPathResource c =new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(c);
//Ticket t = (Ticket)factory.getBean("ticket");
Ticket t = factory.getBean(Ticket.class);
System.out.println(t.getPrice());
3.IOC介绍
IoC/DI:控制反转/依赖注入
我们可以把实例化对象、对象属性赋值等工作反转交给Spring容器处理,通过Spring容器,我们无需再手动的实例化对象,便可获取该实例。
IoC:Spring容器可以完成创建对象、给对象的属性赋值的功能。spring实际上是一个容器框架,可以配置各种bean(action/service/domain/dao),并且可以维护bean与bean的关系,当我们需要使用某个bean的时候,我们可以getBean(id),使用即可.
ioc是什么?
答 :ioc(inverse of controll ) 控制反转: 所谓控制反转就是把创建对象(bean),和维护对象(bean)的关系的权利从程序中转移到spring的容器(applicationContext.xml),而程序本身不再维护.
DI是什么?
答: di(dependency injection) 依赖注入: 实际上di和ioc是同一个概念,spring设计者认为di更准确表示spring核心技术
示例:从Spring容器中获取对象
Student.java
publicclass Student implements Serializable {
privateintstuId;
private String stuName;
private String stuSex;
}
applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<beanid="stu"class="com.pojos.Student">
<propertyname="stuId"value="0"/>
<propertyname="stuName"value="admin"/>
<propertyname="stuSex"value="123456"/>
</bean>
</beans>
Test.java
publicclass Test {
publicstaticvoid main(String[] args) {
//依赖于Spring容器,获取一个Student对象
ApplicationContext c =new ClassPathXmlApplicationContext(
"applicationContext.xml");
Student stu=(Student) c.getBean("stu");
System.out.println(stu);
}
}
4.BeanFactory和ApplicationContext
BeanFactory:bean工厂,最简单的容器,提供了基础的依赖注入支持。创建各种类型的Bean。如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建。
bean工厂只把bean的定义信息载进来,用到的时候才实例化。
BeanFactory factory =new XmlBeanFactory(new ClassPathResource(
"applicationContext.xml"));
就可得到一个bean。
User user = (User) factory.getBean("user");
ApplicationContext:应用上下文,建立在bean工厂基础之上,提供系统架构服务。
ApplicationContext c =new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) c.getBean("user");
结论:
1.如果使用ApplicationContext ,则配置的bean如果是 singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)
2.如果是 BeanFactory ,则当获取beanfacotry时候,配置的bean不会被马上实例化,当使用的时候,才被实例(好处节约内存,缺点就是速度)
3.规定: 一般没有特殊要求, 应当使用ApplicatioContext完成

5.三种获取ApplicationContext对象
1. ClassPathXmlApplicationContext -> 通过类路径
举例:applicationcontext.xml在src目录下
ApplicationContext c =new ClassPathXmlApplicationContext("applicationContext.xml");
2. FileSystemXmlApplicationContext -> 通过 文件绝对路径
举例:
ApplicationContext c=new FileSystemXmlApplicationContext("C:/applicationContext.xml");
3. XmlWebApplicationContext从 web系统中加载。

6.Spring属性注入的三种方式
①set方法注入:通过<bean>元素property标签给属性赋值(要求目标类中必须提供属性的set方法), 可以注入任何东西,从基本类型到集合类,甚至是应用系统的bean
<beanid="course"class="com.softeem.pojos.Course">
<propertyname="courseId"value="1"/>
<propertyname="courseName">
<value>Java软件开发</value>
</property>
</bean>
②构造器注入:
set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选的,构造注入的优势是通过构造强制依赖关系,不可能实例化不完全的或无法使用的bean。
不指定参数顺序:必须与构造器参数类型和个数保持一致
<beanid="course"class="com.softeem.pojos.Course">
<constructor-arg [type="int"]value="1"/>
<constructor-arg [type="java.lang.String"]value="Java软件开发"/>
</bean>
指定构造函数参数顺序
<beanid="cour"class="com.softeem.pojos.Course">
<constructor-argindex="0" [type="int"]value="1"/>
<constructor-argindex="1" [type="java.lang.String"]value="Java软件开发"/>
</bean>
③接口注入








 
 
 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值