文章目录

  • 前言
  • 一、Spring概述
  • 二、Spring入门
  • 1.引入库
  • 2.导入Spring配置文件
  • 3.编写逻辑代码
  • 4.将这个类交给Spring去管理即注册到Spring容器中
  • 5.Spring容器的实例化
  • 6.获取对象方式
  • 三、Spring依赖注入
  • 1.xml注入
  • 2.注解注入
  • 2.1方案一:使用@Autowired
  • 2.2方案二:使用@Resource
  • 2.3@Autowired和@Resource区别



前言

为什么使用的Spring:
  1.代码耦合高
  2.对象之间依赖关系处理繁琐
  3.事务控制繁琐


一、Spring概述

Spring是一个轻量级的DI/IOC和AOP的容器框架
  轻量级:简单好用,通常来说功能不强大(但spring功能强大)
  DI(依赖注入):动态的向某个对象提供它所需要的其他对象,也可以为对象的属性字段赋值。(依赖注入又分为xml注入和注解注入)
  IOC(控制翻转):由spring控制对象的生命周期(创建,销毁)
  AOP(面向切面编程):解决重复代码。将相同的逻辑抽取出来,即将业务逻辑从应用服务中分离出来。然后以拦截的方式作用在一个方法的不同位置。



二、Spring入门

1.引入库

导包的时候注意,现在使用Spring,要完成最小导包,即:需要什么jar包,我们就导入什么jar包,用到了其他功能,再添加相应jar包。这个对认识框架的包是非常有帮助的:

菜鸟教程spring框架 spring菜鸟入门_spring

2.导入Spring配置文件

1. 在classpath的根目录下新建一个applicationContext.xml配置文件,文件名可以自定义,但是通常使用applicationContext这个名字:

<?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="..." class="...">
	<!-- collaborators and configuration for this bean go here -->
	</bean>
</beans>

3.编写逻辑代码

public class MyBean {
	public void hello(){
		System.out.println("hello spring...");
	}
}

4.将这个类交给Spring去管理即注册到Spring容器中

在配置文件中将这个Java类交给Spring管理。在applicationContext.xml中配置

<beans ...>
  <bean id="myBean" class="cn.itsource._01_hello.MyBean"></bean>
</beans>

5.Spring容器的实例化

Spring容器对象有两种:BeanFactory和ApplicationContext(推荐使用)

BeanFactory

@Test
public void testHelloSpring1() throws Exception {
	/**
	 *我们第一步是要启动框架,而启动框架则需要拿到Spring的核心对象
	 *咱们学习的第一个核心对象是BeanFactory : 顾名思义,这是一个创建Bean的工厂
	 *而Bean工厂创建对象又必需拿到配置文件中的数据
	 *因为:我们的第一步读取配置文件,拿到BeanFactory工厂	
	 */
	
	//第一步:读取资源文件
	Resource resource = new ClassPathResource("applicationContext.xml");
	//第二步:拿到核心对象 BeanFactory
	BeanFactory factory = new XmlBeanFactory(resource);
}

ApplicationContext(推荐使用)

@Test
public void testHelloSpring2() throws Exception {
	/**
	 *我们第一步是要启动框架,而启动框架则需要拿到Spring的核心对象
	 *咱们学习的第一个核心对象是BeanFactory : 顾名思义,这是一个创建Bean的工厂
	 *而Bean工厂创建对象又必需拿到配置文件中的数据
	 *因为:我们的第一步读取配置文件,拿到BeanFactory工厂	
	 */
	

	//加载工程classpath下的配置文件实例化
	String conf = "applicationContext.xml";
	ApplicationContext factory = new ClassPathXmlApplicationContext(conf);

}

6.获取对象方式

方式一:通过id直接拿到相应的Bean对象

//通过xml中配置的id拿到对象
MyBean bean = (MyBean)factory.getBean("myBean");
System.out.println(bean);

方式二:通过id与对象的Class对象拿到Bean对象(推荐使用)

//通过id与对象的class拿到Bean对象
MyBean bean = factory.getBean("myBean",MyBean.class);
System.out.println(bean);

三、Spring依赖注入

1.xml注入

顾名思义:在xml中进行配置,但是这种方式必须有对应的setter方法,所有这种注入方式又称之为属性注入或setter方法注入

public class MyBean{
	private OtherBean otherBean;
	public void hello(){
		otherBean.hello();
	}
     public void setOtherBean(OtherBean otherbean){
         this.OtherBean = OtherBean
}
}
public class OtherBean{
	public void hello(){
		System.out.println("otherbean hello");
	}
}
//xml配置:
<bean id="otherBean" class="cn.itsource.bean.OtherBean"></bean>
<bean id="myBean" class="cn.itsource.bean.MyBean">
  <property name="otherBean" ref="otherBean"></property>
</bean>

2.注解注入

顾名思义:通过注解实现注入,这种方式可以将注解写在setter方法上,也可以写在字段上,如果写在字段上可以不需要setter方法

2.1方案一:使用@Autowired

@Autowired为Spring提供的注解

public class MyBean{
	@Autowired 
	private OtherBean otherBean;
	public void hello(){
		otherBean.hello();
	}
}

public class OtherBean{
	public void hello(){
		System.out.println("otherbean hello");
	}
}
//xml配置:
<bean id="otherBean" class="cn.itsource.bean.OtherBean"></bean>
<bean id="myBean" class="cn.itsource.bean.MyBean"></bean>

2.2方案二:使用@Resource

public class MyBean{
    @Resource 
	private OtherBean otherBean;
	public void hello(){
		otherBean.hello();
	}
}

public class OtherBean{
	public void hello(){
		System.out.println("otherbean hello");
	}
}

2.3@Autowired和@Resource区别

@Autowired:默认类型匹配再按照名字匹配
@Resource:默认按照名字匹配然后按照类型匹配