Spring框架入门篇(一)
Spring概念
1. spring是开源的轻量级框架
2. spring核心主要两部分:
(1)aop:面向切面编程,扩展功能不是修改源代码实现
(2)ioc:控制反转,
- 比如有一个类,在类里面有方法(不是静态的方法),调用类里面的方法,创建类的对象,使用对象调用方法,创建类对象的过程,需要new出来对象
- 把对象的创建不是通过new方式实现,而是交给spring配置创建类对象
3 . spring是一站式框架
(1)spring在javaee三层结构中,每一层都提供不同的解决技术
- web层:springMVC
- service层:spring的ioc
- dao层:spring的jdbcTemplate
4. spring版本
(1)hibernate5.x
(2)spring4.x
Spring 的ioc操作
1 . 把对象的创建交给spring进行管理
2 . ioc操作两部分:
(1)ioc的配置文件方式
(2)ioc的注解方式
IOC底层原理
1. ioc底层原理使用技术
(1)xml配置文件
(2)dom4j解决xml
(3)工厂设计模式
(4)反射
2. 画图分析ioc实现原理
第一步导入jar包
(1)做spring最基本功能时候,导入四个核心的jar包就可以了
(2)导入支持日志输出的jar包
第二步创建类,在类里面创建方法
public class HelloWorld { public void printHello() { System.out.println("获取对象"); } }
第三步创建spring配置文件,配置创建类
(1)spring核心配置文件名称和位置不是固定的 - 建议放到src下面,官方建议applicationContext.xml
(2)引入schema约束
(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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.keduox.entity.HelloWorld"></bean> </beans>
第四步写代码测试对象创建
@Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld obj = (HelloWorld) context.getBean("hello"); obj.printHello(); }
Spring的bean管理(xml方式)
1 在spring里面通过配置文件创建对象
2 bean实例化三种方式实现
第一种使用类的无参数构造创建(重点)
<bean id="hello" class="com.keduox.entity.HelloWorld"></bean>
第二种使用静态工厂创建
(1)创建静态的方法,返回类对象
public class Bean2Factory { //静态的方法,返回Bean2对象 public static Bean2 getBean2() { return new Bean2(); } }
使用静态工厂创建对象 <bean id="bean2" class="cn.itcast.bean.Bean2Factory" factory-method="getBean2"></bean>
第三种 使用实例工厂创建
(1)创建不是静态的方法,返回类对象
public class Bean3Factory { //普通的方法,返回Bean3对象 public Bean3 getBean3() { return new Bean3(); } }
<!-- 使用实例工厂创建对象 --> <!-- 创建工厂对象 --> <bean id="bean3Factory" class="cn.itcast.bean.Bean3Factory"></bean> <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
Bean标签常用属性
(1)id属性:起名称,id属性值名称任意命名
- id属性值,不能包含特殊符号
- 根据id值得到配置对象
(2)class属性:创建对象所在类的全路径
(3)name属性:功能和id属性一样的,id属性值不能包含特殊符号,但是在name属性值里面可以包含特殊符号
(4)scope属性(常用)—>>>singleton:默认值,单例 - prototype:多例
- request:创建对象把对象放到request域里面
- session:创建对象把对象放到session域里面
- globalSession:创建对象把对象放到globalSession里面
- prototype:每次对这个bean的实例请求都会导致一个新的实例的创建。当用户需要不受其他用户对象影响的对象或有类 似的需求时,这是一个较理想的解决办法。对于前台Action(控制层),肯定不能使用singleton的模式,必须是一 个线程请求对应一个独立的实例。
属性注入介绍:依赖注入(DI)
1 创建对象时候,向类里面属性里面设置值
2 属性注入的方式介绍(三种方式)
(1)使用set方法注入
(2)使用有参数构造注入
(3)使用接口注入
3 在spring框架里面,支持前两种方式
(1)set方法注入(重点)
(2)有参数构造注入
<!-- 使用有参数构造注入属性 --> <bean id="demo" class="cn.itcast.property.PropertyDemo1"> 使用有参构造注入 <constructor-arg name="username" value="aabb"></constructor-arg> </bean>
<!-- 使用set方法注入属性 --> <bean id="book" class="cn.itcast.property.Book"> name属性值:类里面定义的属性名称 value属性:设置具体的值 <property name="bookname" value="易筋经"></property> </bean>
注入对象类型属性
1. 创建User类和UserInfo类
public class User implements Serializable{ private String id; private String userName; private UserInfo userInfo; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public UserInfo getUserInfo() { return userInfo; } public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } }
public class UserInfo { private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
2. 配置文件中映射关系
<bean id="userinfo" class="com.keduox.entity.UserInfo"> <property name="age" value="23"></property> </bean> <!--注入对象类型属性--> <bean id="user" class="com.keduox.entity.User"> <property name="id" value="1"></property> <property name="userName" value="perkinl"></property> <!--user类的属性 ref引用配置文件中user对象的id--> <property name="userInfo" ref="userinfo"></property> </bean>3. 代码测试
@Test public void test4(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); User u = (User) context.getBean("user"); System.out.println("注入对象后的属性信息"+u.getId()+u.getUserName()+u.getUserInfo().getAge()); }
1. 数组
2. list集合
3. map集合
4. properties类型
<!-- 注入复杂类型属性值 --> <bean id="person" class="cn.itcast.property.Person"> <!-- 数组 --> <property name="arrs"> <list> <value>小王</value> <value>小马</value> <value>小宋</value> </list> </property> <!-- list --> <property name="list"> <list> <value>小奥</value> <value>小金</value> <value>小普</value> </list> </property> <!-- map --> <property name="map"> <map> <entry key="aa" value="lucy"></entry> <entry key="bb" value="mary"></entry> <entry key="cc" value="tom"></entry> </map> </property> <!-- properties --> <property name="properties"> <props> <prop key="driverclass">com.mysql.jdbc.Driver</prop> <prop key="username">root</prop> </props> </property> </bean>