一spring介绍
1、Spring是一个开源轻量级框架
2、一站式(full-stack)框架
在javaee三层结构中每一层都提供了不同的解决技术
Web层:springmvc
service层是spring的ioc
Dao层spring的jdbcTemplate
3、核心主要两个部分:
(1)面向切面aop
比如要扩展一个功能不是修改源代码实现
(2)控制反转ioc(把对象交给spring配置)
比如有一个类,在类里面有方法不是静态的方法,想要调用类的方法需要创建类的对象,使用对象调用方法,创建类的对象的过程,需要new出来
而ioc把我们对象的创建不是使用new来创建而是交给spring进行配置类对象
4、spring版本
(1)hibernate5.x
(2)spring4.x
使用基本的javabean代替ejb(重量级框架)
二 控制反转ioc
1、web调用service,service调用Dao层一开始都是通过new一个对象来管理,但是我们想要的是高内聚低耦合:就是一个类中紧密相连,不同的类中关联度低,在ioc中是这么做的。
2、ioc的底层原理
(1)xml配置文件
(2)Dom4j解决xml
(3)工厂设计模式
(4)反射
3、注解方式配置
<?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/spring-beans.xsd "
>
<!-- ioc入门 -->
<bean id="user" class="cn.itcast.ioc.User"></bean>
</beans>
public void testUser() {
//1加载spring配置文件,根据创建对象
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationControl.xml");
//因为文件在src下所以直接可以写文件名
//根据配置文件的配置把对象创建
//2得到配置创建的对象
//context.Bean("id值");
User user=(User) context.getBean("user");//得到就是对象
System.out.println(user);//如果user对象能创建得到的是地址
user.add();
}
}
4、bean常用属性
(1)id属性
Id就是起一个名字,根据这个id值在代码中得到对象,一般写成小写的类名,不能写特殊符号,不能写中文
(2)Class属性
创建对象所在的类的全路径
(3)Name属性
和id本身一样,但是name里面可以包含特殊符号,现在不用了
(4)Scope属性
设置类的特点
*Singleton:默认值,单例,对象只创建一次
*Prototype:每创建一次就会有一个对象
Request:创建对象把对象放在request域里面
Session:创建对象把对象放在session域里面
Globalsession:创建对象把对象放在globalSession里面(比如登录了百度贴吧那么百度地图之类的都还是能有登录的)
5、属性注入方式
5.1使用set方式注入(默认,常用)
public class PropertyDemo2 {
//要写一个有参构造
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
<!-- set方法注入属性 -->
<!-- 创建对象 -->
<bean id="propertydemo2" class="cn.itcast.property.PropertyDemo2" >
<!-- 注入属性值 name是类里面定义的属性名称 -->
<property name="username" value="property2"></property>
</bean>
5.2使用有参构造函数
public class PropertyDemo1 {
//要写一个有参构造
private String username;
public PropertyDemo1(String username) {
super();
this.username = username;
}
//把参数名输出
public void test1() {
System.out.println("demo1"+username);
}
}
<!-- 使用有参构造注入属性 -->
<bean id="propertydemo1" class="cn.itcast.property.PropertyDemo1" >
<!-- 在bean标签里面使用有参的构造来注入 -->
<constructor-arg name="username" value="小王"></constructor-arg>
</bean>
6、注入对象
1、创建service类和dao类
(1)在service类得到dao类
(2)在service里面把dao作为一个类型的属性
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
(3)生成dao类型属性的set方法
(4)用set注入
<!-- 注入对象类型的属性 -->
<!-- 配置service和dao对象 -->
<bean id="userDao" class="cn.itcast.property.UserDao" > </bean>
<bean id="userService" class="cn.itcast.property.UserService" >
<!-- 注入dao对象
现在不写value,是对象所以要写ref(引用)写userDaoid的值
name写属性的名称
-->
<property name="userDao" ref="userDao"></property>
</bean>
7、IOC和DI的区别
1、IOC是控制反转把对象的创建交给Spring进行配置管理
创建对象
2、DI:叫做依赖注入
创建对象过程中,可以向类里面的属性中设置值就叫依赖注入
依赖注入需要在IOC基础上完成操作
三、面向切面AOP
1、AOP概念
面向切面(方面)编程,拓展功能不通过源代码实现
采用横向抽取机制,取代了传统的纵向继承体系重复性代码
比如在原来写好的代码上增加每个方法执行完都要记录,这时候如果要修改每个方法加上日志的输入代码是很麻烦的,使用AOP可以在不改动原来的代码基础上扩张。