书籍推荐
Spring概念
1.Spring是开源的轻量级框架
2.Spring核心主要三个部分:
- IOC:控制反转;
- DI:依赖注入;
前两个是Spring自带的,后面的是扩展出来的,其中Spring中的AOP实现是由AspectJ公司实现的。 - AOP:面向切面编程,扩展功能不是修改源代码实现。
IOC解释;在以前我们要用一个类的非静态方法,我们首先得创建类的对象,才能够调用里面的方法。但是在IOC里面,创建对象不是通过new方式实现,而是交给Spring配置创建类对象。
3.Spring是一站式框架
什么是Spring一站式框架:
- web层:在web层能够用SpringMVC解决。
- service层:能够用Spring的IOC解决。
- dao层:能够用Spring的jdbcTemplate解决。
也就是说Spring能够代替Hibernate,Struts2这两个框架。
4.Spring版本
- Struts2.x
- Hibernate5.x
- Spring4.x
Spring的IOC操作
- 把对象的创建交给Spring进行管理
IOC操作分为两种:
- IOC的配置文件方式
- IOC注解方式
IOC底层原理
IOC底层原理使用主要技术:
1.XML配置文件
2.dom4j解析XML
3.工厂设计模式
4.反射
Spring的IOC入门案例
操作流程:
第一步:导入jar包
- 下载最新版或者下载笔者创作时用的开发基础包
- jar包特点,都有三个jar包。jar代码包,jar文档包,jar源码包
- Spring核心jar包,Beans,Core,Context,SpEL(expression包)。做Spring最基本功能的时候,导入四个核心jar包就可以了。
- Spring本身没有日志jar包,如果要看执行日志,就得导入支持日志输出的jar包。
第二步:创建类,在类里面创建方法
public class UserService {
public void showUp(){
System.out.println("Hello Spring");
}
}
第三步:创建spring配置文件(xml 格式),配置创建类
- Spring核心配置文件名称和位置不是固定的。建议放到src下面,名称为applicationContext.xml
- 引入Schema约束
在我上传文件的html里面进入最后一个html页面,从页面的最后部位开始找
有beans作为标识符的schema约束。
- 配置对象创建
<bean id="userService" class="cn.domarvel.service.UserService"></bean>
<!-- id名字可以随便取,建议值为User类名首字母小写后的值 -->
- 这是整体配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xx="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="cn.domarvel.service.UserService"></bean>
</beans>
第四步:写代码测试对象创建
@Test
public void testUser(){
//加载Spring配置文件,根据配置文件创建对象,加入我现在的Spring配置文件名称为applicationContext.xml
ApplicationContext context=new ClassPathXmlApplicationContext("如果Spring配置文件放在src根目录下边,可以直接写文件名applicationContext.xml");//这里我写的是applicationContext.xml
((UserService)context.getBean("userService")).showUp();
}
//输出结果:Hello Spring
Spring的bean管理(XML方式)
Bean实例化方式:
1.在Spring里面通过配置文件创建对象
2.bean实例化的三种方式
- 第一种:使用类的无参数构造创建(重点),刚才我们通过bean标签在applicationContext.xml中配置,通过Spring方法创建的实例就是通过无参构造方法创建的对象。下面就不会惰诉了。
如果类里面没有无参构造方法,会出现异常。 - 第二种:使用静态工厂创建
创建静态工厂类:
public class StaticFactory {
public static UserService getUserService(){
return new UserService();
}
}
在applicationContext.xml中进行配置:
<bean id="staticFactoryUserService" class="cn.domarvel.factory.StaticFactory" factory-method="getUserService"></bean>
测试代码:
@Test
public void showStaticFactory01(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//这里面的参数必须要写,不然会报错。
((UserService)context.getBean("staticFactoryUserService")).showUp();
}
- 第三种:使用实例工厂创建
创建非静态的工厂类:
public class EntityFactory {
public UserService getEntityUserService(){
return new UserService();
}
}
在applicationContext.xml中进行配置:
<bean id="entityUserServiceFactory" class="cn.domarvel.factory.EntityFactory"></bean>
<!--
factory-bean:
这里面写要引用的bean标签。
factory-method:
这里面写当前bean(静态方法或者对象)要执行的方法。
-->
<bean id="showEntityUserService" factory-bean="entityUserServiceFactory" factory-method="getEntityUserService"></bean>
测试代码:
@Test
public void showEntityUserService(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
((UserService)context.getBean("showEntityUserService")).showUp();
}
Bean标签常用属性
id属性
id表示对获取该javabean对象时,能够通过该id的值获取。id的值随便,但是建议值为类名首字母小写后的值。(只是整个字符串中的第一个字母小写)
同时id属性值不能够有中文文字或者特殊符号,下划线(_),美元符号($)等都不能。
class属性
创建对象所在类的全路径
name属性
功能和id属性一样。但是id属性值不能包含特殊符号,而在name属性值中,name属性值可以包含特殊符号。现在这个属性已经不用了,这是以前Struts1.x的时代用的了。
scope属性
取值:
- singleton:默认值,单例的。
- prototype:多例的。
- request:WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中。
- session:WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中。
- globalSession:WEB项目中,应用在Porlet环境,如果没有Porlet环境,那么globalSession相当于session。
一般现在只用到前两个。
IOC的实现,依赖注入:
如果继续看下面的内容,请看:依赖注入详解
在Spring框架里面,依赖注入只支持两种
- set方法注入,我们用得最多。
- 有参数构造注入
Spring并不支持接口注入!!!
有参数构造注入
创建javabean类:
public class UserService {
private String name;
public UserService(String name){
this.name=name;
}
public UserService(){}
public void showUp(){
System.out.println("Hello Spring");
}
@Override
public String toString() {
return "UserService [name=" + name + "]";
}
}
配置Spring配置文件:
<bean id="showArgEntity" class="cn.domarvel.service.UserService">
<constructor-arg name="name" value="FireLang"></constructor-arg>
</bean>
创建测试类开始测试:
@Test
public void showArgIntect(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)context.getBean("showArgEntity");
System.out.println(userService);
}
set方法注入
创建javabean类:
public class UserService {
private String name;
public UserService(String name){
this.name=name;
}
public UserService(){}
public void setName(String name) {
this.name = name;
}
public void showUp(){
System.out.println("Hello Spring");
}
@Override
public String toString() {
return "UserService [name=" + name + "]";
}
}
Spring配置文件进行配置:
<bean id="showArgSetEntity" class="cn.domarvel.service.UserService">
<property name="name" value="FireHong"></property>
</bean>
测试代码:
@Test
public void showArgSet(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)context.getBean("showArgSetEntity");
System.out.println(userService);
}
注入对象类型属性(重点)
目的:
创建Service类和dao类
- 在Service得到dao对象
具体实现过程:
- 创建Service方法、创建Dao方法,在Service里面把dao作为类型属性
生成dao类型属性的set方法
//Dao方法 public class ArticleDao { public void showArticleList(){ System.out.println("这里有很多博客!!!"); } }
//创建Service类 public class ArticleServer { private ArticleDao articleDao; public void setArticleDao(ArticleDao articleDao) { this.articleDao = articleDao; } public void showArticleList(){ System.out.println("在Service中接受到很多博客!!!"); articleDao.showArticleList(); } }
- 配置Spring配置文件
<bean id="articleListDao" class="cn.domarvel.dao.ArticleDao"></bean>
<bean id="articleListServer" class="cn.domarvel.service.ArticleServer">
<!--
现在不要写value属性,因为刚才是字符串,现在是对象。
写ref属性:写dao这个类配置在bean标签中的id值
name属性值:配置类里面的属性名称。
-->
<property name="articleDao" ref="articleListDao"></property>
</bean>
//测试代码:
public class ArticleServiceTest {
@Test
public void showArticleListService(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ArticleServer articleServer=(ArticleServer)context.getBean("articleListServer");
articleServer.showArticleList();
}
}
p名称空间注入
讲解p名称空间用到的两个javabean实体类还是上面讲解对象注入的两个实体类,测试代码也是上面讲解对象注入用到的。
需要修改的就是Spring配置文件。修改成p名称空间注入
第一步:引入p名称空间‘
xmlns:p="http://www.springframework.org/schema/p"
<!-- 命名空间可以随便写,但是这个引入地址是固定的 -->
<!-- 比如: -->
xmlns:xxx="http://www.springframework.org/schema/p"
<!-- 这样也可以 -->
第二步:使用p名称空间
- 普通属性格式:
p:属性名称=""
- 对象类型属性:
p:属性名称-ref=""
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="articleListDao" class="cn.domarvel.dao.ArticleDao"></bean>
<bean id="articleListServerNameSpaceInject" class="cn.domarvel.service.ArticleServer" p:articleDao-ref="articleListDao"></bean>
<!-- 属性可以写多个 -->
</beans>
注入复杂类型属性
- 数组
- list集合
- Map集合
- properties类型
创建javabean实体类:
public class ComplexTypeService {
private String[] names;
private List<String> majors;
private Map<String, String> personToLikesMap;
private Properties properties;
public void setMajors(List<String> majors) {
this.majors = majors;
}
public void setPersonToLikesMap(Map<String, String> personToLikesMap) {
this.personToLikesMap = personToLikesMap;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public List<String> getMajors() {
return majors;
}
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
public Map<String, String> getPersonToLikesMap() {
return personToLikesMap;
}
public Properties getProperties() {
return properties;
}
}
Spring配置文件:
<?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="complexTypeService" class="cn.domarvel.service.ComplexTypeService">
<property name="names">
<list>
<value>FireLang</value>
<value>FireHongGirl</value>
</list>
</property>
<property name="majors">
<list>
<value>软件开发工程师</value>
<value>日语神奇交流</value>
</list>
</property>
<property name="personToLikesMap">
<map>
<entry key="FireLang" value="FireHongGirl"></entry>
<entry key="FireHongGirl" value="FireLang"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="FireLangSon">FireHongGirlSon</prop>
<prop key="FireLangSonAndSon">FireHongGirlSonAndSon</prop>
</props>
</property>
</bean>
</beans>
测试代码:
public class ComplexTest {
@Test
public void showComplexType(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ComplexTypeService complexTypeService=(ComplexTypeService)context.getBean("complexTypeService");
System.out.println("array: "+Arrays.toString(complexTypeService.getNames()));
System.out.println("list: "+complexTypeService.getMajors());
System.out.println("Map: "+complexTypeService.getPersonToLikesMap());
System.out.println("properties: "+complexTypeService.getProperties());
}
}
Spring配置文件实践后的得到的配置方法:
<bean id="itemOperationSet" class="cn.domarvel.controller.ItemOperationSet">
<property name="methodNameResolver.prefix" value="item"></property>
</bean>
<bean id="itemOperationSet" class="cn.domarvel.controller.ItemOperationSet">
<property name="methodNameResolver"><!-- 替换变量methodNameResolver的实现对象 -->
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/iteminsert.action">insert</prop>
</props>
</property>
</bean>
</property>
</bean>