文章目录
Spring
1.spring是开源的轻量级框架,是一站式框架
注:使用spring最基本功能时候,需要导入四个核心的jar包+一个日志jar包
2.spring核心主要两部分:
(1)aop:面向切面编程,扩展功能不是修改源代码实现
(2)ioc:控制反转(把对象的创建不是通过new方式实现,而是交给spring配置创建类对象)
3.一站式框架:spring在javaee三层结构中都提供了不同的解决技术,即:
(1)web层:springMVC
(2)service层:spring的ioc
(3)dao层:spring的jdbcTemplate
4.SSM框架开发:Spring√+SpringMVC+Mybatis
IOC
1.ioc操作:把对象的创建交给spring进行管理
ioc的操作有两种方式:①配置文件(xml)方式;②注解方式
ioc和di的区别:
(1)ioc:控制反转
(2)di:依赖注入,即向类里面的属性中设置值(不能单独存在,需要在ioc基础之上 完成)
2.ioc的底层原理
(1)底层使用的技术:xml配置文件、dom4j解析、工厂设计模式、反射
(2)实现原理:
a.工厂模式:
b.ioc原理:
Spring的bean管理(xml方式)
1.bean实例化的三种方式:
(1)使用类的无参构造创建(重点)
<bean id="user" class="cn.test.ioc.User"></bean>
(2)使用静态工厂创建
public class BeanFactory{
public static Bean getBean(){
return new Bean();
}
}
<bean id="bean" class="cn.test.ioc.BeanFactory" factory-method="getBean"></bean>
(3)使用实例工厂创建
public class BeanFactory{
public Bean getBean(){
return new Bean();
}
}
<bean id="beanFacctory" class="cn.test.ioc.BeanFactory"></bean>
<bean id="bean" factort-bean="beanFacctory" factory-method="getBean"></bean>
2.案例
(1)配置文件:applicationContext.xml
<?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-4.1.xsd">
<bean id="user" class="cn.test.ioc.User"></bean>
</beans>
(2)ApplicationContext加载配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");//加载spring配置文件,创建对象
User user = (User)context.getBean("user");
3.bean的常用属性
(1)id:id属性值名称任意命名,根据id值得到配置对象
(2)class:创建对象所在类的全路径
(3)name:功能和id属性一样;区别:id不能包含特殊符号,而name可以(一般不用)
(4)scope四个属性值:
a.singleton:默认值,单例(重点,单例一个类只有一个对象)
b.prototype:多例(重点)
c.request:创建对象把对象放到request域里面
d.session:创建对象把对象放到session域里面
e.globalSession:创建对象把对象放到globalSession里面
属性注入
1.属性注入:建对象时候,向类里面属性里面设置值
2.三种注入方式:
(1)使用set方法注入√(重点)
public class User{
private String name;
public void setName(String name){
this.name = name;
}
}
(2)使用有参数构造注入√
public class User{
private String name;
public User(String name){
this.name = name;
}
}
(3)使用接口注入
public interface Dao{
public void delete(String name);
}
public class DaoImp implements Dao{
private String name;
public void delete(String name){
this.name = name;
}
}
3.spring支持的前两种注入方式:
(1)使用有参数构造注入
public class PropertyDemo{
private String username;
public PropertyDemo(String username){
this.username = username;
}
public void test(){
System.out.println("demo......");
}
}
在配置文件中注入
<bean id="demo" class="cn.test.PropertyDemo">
<constructor-arg name="username" value="moke123"></constructor-arg>
</bean>
(2)使用set方法注入(常用)
public class Book{
private String bookname;
public void setBookname(String bookname){
this.bookname = bookname;
}
public void demobook(){
System.out.println("book......");
}
}
在配置文件中注入
<bean id="book" class="cn.test.Book">
<property name="bookname" value="sanmao"></property>
</bean>
4.对象类型属性注入(重点)
例如,Service中的Dao
(1)在service里面把dao作为类型属性
(2)生成dao