Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。 简述: 1、方便解耦,简化开发 Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 2、AOP编程的支持 Spring提供面向切面编程,可以方便的实现对程序进行权限拦截,运行监控等功能 3、声明式事务的支持 只需要通过配置就可以完成对事务的管理,而无需手动编程 优点:
所有的依赖关系被集中统一的管理起来,清晰明了每个类只需要关注于自己的业务逻辑修改依赖关系将是一件很容易的事情Spring需要的jar
spring-beansspring-contextspring-corespring-expressioncommons-logging控制反转、依赖注入的概念
IOC(Inverse of Control) 控制反转的概念,就是将原本中手动创建对象的控制权交给了Spring框架管理。即创建对象控制权被反转到了Spring框架
DI(Dependency Injection) 依赖注入的概念,就是在Spring创建这个对象过程中,将这个对象所依赖的属性注入进去。
1、导入相关jar包 2、创建resources文件夹,将其设置为Resources Root文件夹 创建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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> </beans>3、新建java文件夹,并将其设置为Sources Root文件夹。创建包,在包下新建UserService接口,随便写个方法,这里写个sayHello()方法。
public interface UserService { public void sayHello(); }4、在包下新建UserService的实现类UserServiceImpl。这里调用sayHello打印一句话
public class UserServiceImpl implements UserService { //依赖的属性 private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayHello() { System.out.println("Hello Spring "+name); } }5、回到配置文件applicationContext.xml中,添加如下代码
<!--UserService的创建权交给了Spring--> <bean id="userService" class="com.ioc.demo1.UserServiceImpl"> <!--设置属性--> <property name="name" value="张三"/> </bean>6、测试类,几种方法对比。
import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; /** * 测试类 */ public class SpringDemo1 { @Test /** * 传统方式 */ public void Demo1(){ UserService userService = new UserServiceImpl(); userService.sayHello(); } @Test /** * Spring的方式实现 */ public void Demo2() { //创建Spring的工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //通过工厂获得类: UserService userService = (UserService) applicationContext.getBean("userService"); userService.sayHello(); } @Test /** * 读取磁盘系统中的配置文件 */ public void demo3(){ //创建spring的工厂类 ApplicationContext applicationContext = new FileSystemXmlApplicationContext("f:\\applicationContext.xml"); //通过工厂获得类: UserService userService = (UserService) applicationContext.getBean("userService"); userService.sayHello(); } @Test /** * 传统方式的工厂类:BeanFactory */ public void demo4(){ //创建工厂类 BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); //通过工厂获得类: UserService userService = (UserService) beanFactory.getBean("userService"); userService.sayHello(); } @Test /** * 传统方式的工厂类:BeanFactory */ public void demo5(){ //创建工厂类 BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("f:\\applicationContext.xml")); //通过工厂获得类: UserService userService = (UserService) beanFactory.getBean("userService"); userService.sayHello(); } }id和name: 一般情况下,装配一个Bean时,通过指定一个id属性作为Bean的名称 id属性在ioc容器中必须是唯一的 如果Bean的名称中含有特殊字符,就需要使用name属性
class class 用于设置一个类的完全路径名称,主要作用是IOC容器生成类的实例
Bean作用范围 默认单例模式 scope=“singleton” 多例:scope=“prototype”
1、 使用类构造器实例化(默认无参数) 2、 使用静态工厂方法实例化(简单工厂模式) 3、 使用实例工厂方法实例化(工厂方法模式)
1、使用类构造器实例化(默认无参数) 创建一个类
/** * Bean的实例化的三种方式一,采用无参数的构造方法的方式 */ public class Bean1 { public Bean1(){ System.out.println("Bean被实例化了"); } }配置文件
<!--第一种:无参构造器--> <bean id="bean1" class="com.ioc.demo2.Bean1"/>测试
@Test public void demo1(){ //创建工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //通过工厂获得类的实例 Bean1 bean1 = (Bean1) applicationContext.getBean("bean1"); }2、 使用静态工厂方法实例化(简单工厂模式) 创建一个类Bean2
public class Bean2 { }创建Bean2静态工厂
** * Bean2的静态工厂 */ public class Bean2Factory { public static Bean2 createBean2(){ System.out.println("Bean2Factory的方法已经执行了"); return new Bean2(); } }配置文件
<!--第二种方式:静态工厂的方式--> <bean id="bean2" class="com.ioc.demo2.Bean2Factory" factory-method="createBean2"/>测试
@Test public void demo2(){ //创建工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Bean2 bean2 = (Bean2) applicationContext.getBean("bean2"); }3、 使用实例工厂方法实例化(工厂方法模式) 新建类Bean3
/** * 实例化Bean的第三种方式,实例工厂实例化 */ public class Bean3 { }新建Bean3实力工厂类Bean3Factory
/** * Bean3的实例工厂 */ public class Bean3Factory { public Bean3 createBean3(){ System.out.println("Bean3Factory被执行了"); return new Bean3(); } }配置文件
<!--第三种方式:实例工厂的方式--> <bean id="bean3Factory" class="com.ioc.demo2.Bean3Factory"/> <bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"/>测试
@Test public void demo3(){ //创建工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Bean3 bean3 = (Bean3) applicationContext.getBean("bean3"); }