一篇讲完Spring

IOC 和 DI模块    

      在spring中IOC和DI是两个不同的概念,但是又互相依赖、相辅相成。

       第一个Spring程序  helloWord   

      新建一个HelloWorld类

public class HelloWorld {
	private String username;
	public void setUsername(String username) {
		this.username = username;
	}
	public void sayHello(){
		System.out.println("欢迎你"+username);
	}
}

   在不用Spring之前,我们的测试方法是这样的

@Test
	public void test(){
		HelloWorld h = new HelloWorld();
		h.setUsername("123");
		h.sayHello();
	}

来看看使用了Spring之后的   先在项目中新建resources 目录  然后在该目录下新建applicationContext.xml加入scheme

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      ">
      
</beans>

然后新建lib目录   拷贝spring-beans-5.0.4.RELEASE.jar、spring-core-5.0.4.RELEASE.jar    并buildPath

下面通过配置文件来告诉Spring帮我们管理哪些类

在applicationContext..xml中  加入

<bean id="HelloWorld" class="com.xiaowww.hello.HelloWorld" />

小知识:很多框架都是基于反射,所以一般xml都需要配置id和全限定名   就像web.xml中的过滤器和webServlet一样

 

配置好了之后我们来用Spring的方法测试

@Test
	public void testSpring(){
		Resource resource = new ClassPathResource("applicationContext.xml");
		BeanFactory beanFactory = new XmlBeanFactory(resource);
		HelloWorld bean = (HelloWorld)beanFactory.getBean("HelloWorld");
		bean.setUsername("123");
		bean.sayHello();
	}

运行之后报错一篇讲完Spring

这是因为Spring依赖于commons的这个jar包    去maven上面下载   commons-logging-1.1.1.jar   添加到项目中,方法正常运行

 

以上就是用Spring的 IOC去管理我们所需要的类   而被Spring所管理的类我们称之为Bean

 

那什么是DI呢     DI就是依赖注入    也就是用Spring来帮我们注入某个类的属性值

我们刚刚在applicationContext.xml中创建好了一个HelloWorld  的Bean 实例  我们为这个Bean注入username属性

一篇讲完Spring

将刚才applicationContext.xml中的Bean改为上图    测试代码修改为

    @Test
	public void testSpring(){
		Resource resource = new ClassPathResource("applicationContext.xml");
		BeanFactory beanFactory = new XmlBeanFactory(resource);
		HelloWorld bean = (HelloWorld)beanFactory.getBean("HelloWorld");
		bean.sayHello();
	}

还是会输出   一篇讲完Spring   注意:此时我们并没有使用Set方法给username去赋值   而是通过Spring的配置去为Bean注入属性值   这个过程就叫做DI  依赖注入    (注入Bean的属性必须存在Set方法否则会报错)   

思考:Spring注入值得方式是使用set方法来注入的,那么你能模仿Spring的DI来写一个方法吗?

Spring的IOC和DI的原理   :    解析applicationContext.xml  -->获取Bean元素 -->利用全限定名创建对象(反射)-->获取Bean元素中的property子元素的name-->找到该属性的set方法(内省机制) -->为该属性注入值   -->输出Bean元素

Spring创建的对象必须存在无参构造器,与访问权限无关。

BeanFactory获取Bean的三种方式:

      Object getBean(String name) throws BeansException;//根据配置的ID获取Bean也就是上面我们使用的

      <T> T getBean(Class<T> requiredType) throws BeansException;//根据类型去获取Bean

      <T> T getBean(String name, Class<T> requiredType) throws BeansException;//根据名称和类型去获取Bean

一般不同框架的配置我们会分开使用但是会跟Spring整合 

Spring引入配置文件 <import resource="classpath:springMvc.xml"/>      默认从ClassPath根路径开始找

SpringJunit的使用

       我们平时测试都是用Junit   而Spring集成的Junit会让我们使用起来比上面的方法更简单方便

一篇讲完Spring

这就是完整的SpringJunit测试类       Junit5更简单 只需要一个注解  有兴趣的同学自行研究一下

这里的Autowired  就是根据  类型来获取Bean