Spring 第一天的学习
spring是什么:
struts是web框架(jsp/action/actionform)
hibemate是orm框架,处于持久层
spring是一种框架,是一种容器框架,该框架可以配置各个层的组件,并维护bean与bean之间的关系的框架,可以管理web层,业务层,dao层,持久层,该框架可以配置各个层的组件,并且维护各个bean之间的关系
什么是bean
bean是java中的任何一种对象,javabean,service,action,数据源,dao,ioc,di
ioc:控制反转,英文全称 inverse of control
di: 依赖注入,英文全称为 dependency injection
spring层次图
快速入门案例
- 写一个普通的类
package com.service;
/**
* Created by hejian on 16/9/5.
*/
public class UserService {
private String name;
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("hellow" + name);
}
}
- applicationContext.xml配置如下,此文件为核心配置文件,主要配置bean,该文件一般放在src目录下,也可以放在其它目录下,此文件配置bean,这个bean可以是service,dao,domain,action,数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd">
<bean id = "userService" class="com.service.UserService">
<!-- bean元素的作用:当spring框架,加载时,spring会自动创建一个bean对象,并放入内存,并管理起来
相当于:UserService userservice = new UserService();
-->
<property name="name"><!--添加属性 此处相应的类中必须有setName方式,否则注入不进去-->
<value>美团</value>
</property>
<!--也可以这样写<property name = "name" value = "美团" />-->
</bean>
</beans>
- 测试这个类中的方法
package com.test;
import com.service.BybService;
import com.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by hejian on 16/9/5.
*/
public class Test {
//使用传统方法来调用UserService的sayHello方法
public static void main(String [] args){
UserService userService = new UserService();
userService.setName("美团");
userService.sayHello();
//使用spring来调用,需创建applicationContext.xml文件
//得到spring容器对象applicationContext.xml对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ac.getBean("userService");//getBean返回一个对象的实例 其中ac.getBean("")括号中的名称是bean中的id,应是一个类名
us.sayHello();
}
上述过程如下
- 引入spring的相关jar包,常用的包有spring-beans-4.0.0.RELEASE.jar,commons-logging-1.2.jar,spring-context-4.0.0.RELEASE.jar包
- 创建一个applicationContext.xml文件,是spring的核心文件
- 创建一个bean,id = “为类名称” class =”为类的全路径”
bean与 bean的嵌套
- 写一个普通的类,引用BybService类
package com.service;
/**
* Created by hejian on 16/9/5.
*/
public class UserService {
private String name;
private BybService bybService;
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("hellow" + name);
bybService.sayByb(); //引用BybService类的一个方法
}
}
- 被引用的一个普通的类BybService
package com.service;
/**
* Created by hejian on 16/9/5.
*/
public class BybService {
private String name;
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayByb(){
System.out.println("hellow" + name);
}
}
application.xml文件的bean配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd">
<bean id = "userService" class="com.service.UserService">
<!-- bean元素的作用:当spring框架,加载时,spring会自动创建一个bean对象,并放入内存,并管理起来
相当于:UserService userservice = new UserService();
-->
<property name="name"><!--添加属性 此处相应的类中必须有setName方式,否则注入不进去-->
<value>美团</value>
</property>
<!--也可以这样写<property name = "name" value = "美团" />-->
<bean id = "bybService" ref = "bybservice" >
<!--上述的id值为UserService中引用的属性,ref值为**下一个bean的id 值**-->
</bean>
<bean **id = "bybService"** class ="com.service.Bybservice">
<property name = "name" value = "张三" />
</bean>
</beans>
spring运行原理
spring框架什么时候被加载
- 当ClassPathXmlApplicationContext(“applicationContext.xml”)执行的时候,spring 容器对象被创建,同时applicationContext.xml配置bean就会被创建。
bean是怎样被加载的
- spring容器扫描applicationContext.xml文件,同时通过反射机制在内存中创建 bean中元素
- 且通过getbean()获取相应的bean
- 对应的bean应用相应的set,get 方法