Spring入门

spring介绍

什么是spring

Spring 是一个分层的 JavaSE/EEfull-stack(一站式) 轻量级 开源框架。

spring优点

  • 方便解耦,简化开发:Spring 就是一个大工厂,可以将所有对象创建和依赖关系维护,交给 Spring 管理
  • AOP 编程的支持:Spring 提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
  • 声明式事务的支持:只需要通过配置就可以完成对事务的管理,而无需手动编程
  • 方便程序的测试 Spring 对 Junit4 支持,可以通过注解方便的测试 Spring 程序
  • 方便集成各种优秀框架:Spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、 MyBatis、Quartz 等)的直接支持
  • 降低 JavaEE API 的使用难度:Spring 对 JavaEE 开发中非常难用的一些 API(JDBC、JavaMail、远程调用等),都提供了封装, 使这些 API 应用难度大大降低

spring搭建

导入jar包

在这里插入图片描述

创建一个对象

package bean;

public class User {
	private String name;
	private Integer age;
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name = name;
	}
	public Integer getAge(){
		return age;
	}
	public void setAge(Integer age){
		this.age = age;
	}
}

书写配置文件

  • 位置任意(建议放在src下
  • 配置文件名任意(建议applicationContext.xml
    在这里插入图片描述

导入约束

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注:如果找不到schema的话先试试关掉eclipse试试,然后再不行的话重新导入一次约束试试

书写配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"  xsi:schemaLocation="http://www.springframework.org/schema/beans  
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
	<!-- 将User对象交给spring容器管理 -->
	<bean name="user" class="bean.User"></bean>
</beans>

测试

编写测试类

在这里插入图片描述

package test;

import bean.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
	@Test
	public void fun1(){
		//1  创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//2 向容器对象“要”user对象
		User u = (User) ac.getBean("user");
		//打印“user”对象
		System.out.println(u);
	}
}

测试结果

在这里插入图片描述

spring概念

IOC:Inverse of control反转控制

将我们创建对象的方式反转

以前对象的创建是由我们开发人员自己维护,包括依赖关系也是自己注入。
使用了spring之后,对象的创建以及依赖关系可以由spring完成创建以及注入。

DI:Dependence Injection依赖注入

实现IOC思想需要DI做支持。

注入方式

set注入(也用
<!-- 第二种:set 方法的方式 -->  
<bean id="user" class="bean.User"> 
  <property name="name" value=" 小王"/>   
  <property name="age" value="30"/>  
</bean>
构造方法注入(推荐
<!-- 第一种:构造方法的方式 -->  
<bean id="user" class="bean.User"> 
  <constructor-arg name="name" value=" 小王 "/>   
  <constructor-arg name="age" value="30"/>  
  </bean>
字段注入(不推荐

注入类型

值类型注入
<!-- SpEL:Spring Expression Language. 语法:#{ SpEL } -->
 
 <!-- SpEL 的注入的方式 -->  
 <bean id="user" class="bean.User"> 
  <property name="name" value="#{' 小王'}"/>   
  <property name="age" value="#{30}"/> 
 </bean> 

<!-- Spring 的复杂类型的注入===================== -->  
<bean id="userName" class="bean.User"> 
  <!-- 数组类型的属性 -->  
   <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="aaa" value="111"/>     
    <entry key="bbb" value="222"/>     
    <entry key="ccc" value="333"/>    
    </map>  
     </property>    
  <!-- Properties 的注入 -->   
  <property name="properties">    
  <props> 
    <prop key="username">root</prop>     
    <prop key="password">123</prop>   
     </props>   
     </property>  
     </bean> 
引用类型注入
<!-- 注入对象类型的属性 -->  
<bean id="person" class="cn.itcast.spring.demo4.Person"> 
  <property name="name" value=" 会希 "/>   
  <!-- ref 属性:引用另一个 bean 的 id 或 name -->   
  <property name="car2" ref="car2"/>  </bean>
  <!--引用了另一个类的属性-->
    <bean id="car2" class="cn.itcast.spring.demo4.Car2"> 
<!--   <property name="name" value="#{'奔驰'}"/> -->  
 <property name="name" value="#{carInfo.carName}"/>  
  <property name="price" value="#{carInfo.calculatePrice()}"/> 
 </bean> 

spring中工厂(容器)

BeanFactory(过时) 和 ApplicationContext 的区别:

  • BeanFactory :是在 getBean 的时候才会生成类的实例.
  • ApplicationContext :在加载 applicationContext.xml(容器启动)时候就会创建.

spring配置详解

bean元素

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"  xsi:schemaLocation="http://www.springframework.org/schema/beans  
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
	<!-- 将User对象交给spring容器管理 -->
	<!-- Bean元素:使用该元素描述需要spring容器管理的对象
			    name: 给被管理的对象取个名字,获得对象时根据该名称获得对象。可以重复(不推荐),可以使用特殊字符
			    class属性:被管理对象的完整类名
			    id属性:与name属性一模一样。名称不可重复,不能使用特殊字符-->
	<bean name="user" class="bean.User"></bean>
	<!-- 整合 struts1 的时候: <bean name=”/loginAction” -->
</beans>

spring创建元素的方式

空参构造

public class User {
	public User(){
		System.out.println("User空参构造");
	}
}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"  xsi:schemaLocation="http://www.springframework.org/schema/beans  
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
	<!-- 创建方式1:空参构造创建 -->
	<bean name="user" class="bean.User"></bean>
</beans>

静态工厂方式(了解

create/UserFactory.java

package create;

import bean.User;

public class UserFactory {
	public static User createUser(){
		System.out.println("静态工厂方式");
		return new User();
	}
}

Demo.java

@Test
	public void fun2(){
		//1  创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("create/applicationContext.xml");
		//2 向容器对象“要”user对象
		User u = (User) ac.getBean("user2");
		//打印“user”对象
		System.out.println(u);
	}

create/applicationContext.xml

	<!-- 创建方式2:静态工厂创建 
			   调用UserFactory的createUser方法创建名为user2的对象,放入容器-->
			   
	<bean name="user2" class="create.UserFactory" factory-method="createUser"></bean>

实例工厂(了解

create/UserFactory.java

public User createUser2(){
		System.out.println("实例工厂方式");
		return new User();
	}
<!-- 创建方式3:实例工厂方式 
		        调用UserFactory对象的createUser2方法创建名为user3的对象,放入容器-->
	<bean name="user3"
		  factory-bean="userFactory"
		  factory-method="createUser2"></bean>
	
	<bean name="userFactory"
	class="create.UserFactory"></bean>

test/Demo.java

@Test
	public void fun3(){
		//1  创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("create/applicationContext.xml");
		//2 向容器对象“要”user对象
		User u = (User) ac.getBean("user3");
		//打印“user”对象
		System.out.println(u);
	}

bean元素进阶

scope属性

属性作用范围
singleton(默认值)单例对象:被标识为单例的对象在spring中只会存在一个实例
prototype多例原型:被标识为单例的对象每次获得时才会创建,每次创建都是新的对象
requestWEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
sessionWEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.

生命周期属性

初始化时的执行方法

通过配置标签上的 init-method作为 Bean 的初始化的时候执行的方法,spring会在对象创建之后立刻调用。

销毁时的执行方法

配置 destroy-method 作为 Bean 的销毁的时候执行的方法。 spring会在关闭销毁所有容器对象之前调用。

spring的分模块配置

在项目主配置文件下导入其他模块配置文件

<beans>
<!-- 导入其他spring配置文件 -->
	<import resource="create/applicationContext.xml"/>
</beans>

将spring应用到项目中:管理Service和Dao对象

导包

在这里插入图片描述

将Service对象以及Dao对象配置到spring容器

为项目添加applicationContext.xml并书写对象对应的bean

<!-- 配置Dao -->
	<bean name="customerDao" class="dao.impl.CustomerDaoImpl"/>
	<bean name="linkDao" class="dao.impl.LinkDaoImpl"/>
	<bean name="userDao" class="dao.impl.UserDaoImpl"/>
	<!-- 配置Service -->
	<bean name="customerService" class="service.impl.CustomerServiceImpl">
		<property name="cd" ref="customerDao"></property>
	</bean>
	<bean name="linkService" class="service.impl.LinkServiceImpl">
		<property name="cd" ref="customerDao"></property>
		<property name="ld" ref="linkDao"></property>
	</bean>
	<bean name="userService" class="service.impl.UserServiceImpl">
		<property name="ud" ref="userDao"></property>
	</bean>

在Action中获得Service容器中的对象

在每个action方法中都加入如下代码(错误示范,每次请求都会创建新容器

//1  创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//2 获得service对象
		CustomerService cs = (CustomerService) ac.getBean("customerService");

在web.xml下新建listener

导入spring-web这个jar包
在这里插入图片描述

<!-- 可以让spring容器随项目的启动而创建,随项目的关闭而销毁 -->
<listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  	<!-- 可以open type查找对应的类文件,复制完整类名即可 -->
  </listener>
  <!-- 指定加载spring配置文件的位置,得打开对应的类来寻找配置位置的参数,可以考虑背下来 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
在java文件某些方法中添加从Application域获得spring容器方法
//1.获得servletContext对象
servletContext sc = ServletActionContext.getServletContext();
//2.从sc中获得ac容器
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
//3.从容器中获得CustomerService
CustomerService cs = (CustomerService)ac.getBean("customerService");

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值