一。spring介绍
1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)
2.AOP面向切面的编程思想与动态代理
3.作用:项目的粘合剂,总管,使项目的维护性和扩展性更好

二。spring作为bean的管理容器使用步骤
1.pom.xml加入spring的依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
</dependencies>
</project>

2.在src目录下创建beans.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="bt" class="com.bean.BeanTest"></bean> <bean id="stuDAO" class="com.dao.StudentDAO"></bean>
<bean id="stuMysqlDAO" class="com.dao.StudentMysqlDAO"></bean> <bean id="stuSer" class="com.service.StudentService">
<property name="stuDAO">
<ref bean="stuMysqlDAO" />
</property>
</bean> <bean id="stuControl" class="com.control.StudentControl">
<property name="stuSer">
<ref bean="stuSer" />
</property>
</bean>
</beans>

3.将java对象加入到配置文件中注册
  <bean id="hello" class="com.bean.Hello"></bean>
4.在代码中获取容器,拿到bean对象,运行
  // 初始化spring的容器
  BeanFactory bf = new ClassPathXmlApplicationContext("beans.xml");
  // 从容器中拿到javabean
  Hello h = (Hello) bf.getBean("hello");

三。ioc反转控制。依赖注入
1.set方法的注入
2.构造方法的注入

四。注解的方式完成注入
1.补充schema
2.打开注解开关
  <!-- 打开spring的注解开关 -->
  <context:annotation-config></context:annotation-config>
  <!-- 告诉spring到哪些包下去扫描bean对象 -->
  <context:component-scan base-package="com"></context:component-scan>
3.给类加注解
  @comptent:表示注册为组件
  @Repository:表示注册为DAO组件
  @Service:表示注册为业务组件
  @Controller:注册为控制器组件
4.注入的注解
  @Autowired:写在属性上,表示自动按照类型注入
  注意:
    a。属性的类型必须是接口
    b。如果一个接口有多个实现类, 通过@Qualifier("db2")指定实现类
附:spring注解可参考此链接 https://www.cnblogs.com/wlxslsb/p/10718402.html

实例:使用注解的形式来写一个Spring项目

 1.dao层  

IStudentDAO.java
package com.dao;

public interface IStudentDAO {
public void saveStu();
}
StudentDAO.java
package com.dao;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository; //@Component("oracle")
@Repository("oracle")
public class StudentDAO implements IStudentDAO{
@Override
public void saveStu() {
System.out.println("使用oracle数据库");
System.out.println("正在保存学员对象");
} }
StudentMysqlDAO.java
package com.dao;

import org.springframework.stereotype.Component;

@Component("mysql")
public class StudentMysqlDAO implements IStudentDAO{ @Override
public void saveStu() {
System.out.println("正在使用mysql数据库");
System.out.println("保存学员对象");
} }

  2.Service层

IStudentService.java
package com.service;

public interface IStudentService {
public void addStudent();
}
StudentService.java
package com.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import com.dao.IStudentDAO;
import com.dao.StudentDAO;
//@Component
@Service
public class StudentService implements IStudentService{
// IStudentDAO stuDAO = new StudentDAO();
@Autowired
@Qualifier("mysql")//当借口有多个实现类时,必须指明注入哪个实现类
IStudentDAO stuDAO; public void setStuDAO(IStudentDAO stuDAO) {
this.stuDAO = stuDAO;
} @Override
public void addStudent() {
System.out.println("拿到数据");
System.out.println("调用dao保存数据");
stuDAO.saveStu();
} }

3.Control层

StudentControl.java
package com.control;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller; import com.service.IStudentService;
//@Component("sc")
@Controller
public class StudentControl {
@Autowired
IStudentService stuSer; public void setStuSer(IStudentService stuSer) {
this.stuSer = stuSer;
} public String execute(){
System.out.println("接收页面参数");
System.out.println("调用业务层");
stuSer.addStudent();
return "success";
}
}

4.配置文件

  beans.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 打开spring的注解功能 -->
<context:annotation-config></context:annotation-config>
<!-- 告诉spring到哪些包下去扫描bean对象 -->
<context:component-scan base-package="com"></context:component-scan> </beans>

5.测试代码

RunTest.java
package com.bean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.control.StudentControl;
import com.service.IStudentService; public class RunTest {
public static void main(String[] args) {
//根据beans.xml去构造出bean工厂,就是spring的容器
BeanFactory bf = new ClassPathXmlApplicationContext("beans.xml"); StudentControl sc = (StudentControl) bf.getBean("sc");
sc.execute();
}
}

一) Spring 介绍、IOC控制反转思想与DI依赖注入的更多相关文章

  1. Spring 什么是 IOC 控制反转 ?什么是依赖注入?spring的用处 好处 为什么要用

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha Spring是一个开源的控制反转(Inversion of Control ,IoC)和 ...

  2. IoC(控制反转)和DI(依赖注入)

    一.IOC 1.目标类 提供UserService接口和实现类 获得UserService实现类的实例 之前开发中,直接new一个对象即可,使用spring之后,将由spring创建  -->I ...

  3. Spring IOC(控制反转)思想笔记

    Spring IOC(控制反转)思想笔记 IOC控制反转基本理念就是将程序控制权从程序员手中交给用户自定义,从而避免了因为用户一个小需求的变化使得程序员需要改动大量代码. 案例 如果按照之前javaw ...

  4. 关于.NET中的控制反转(三)- 依赖注入之 Autofac

    一.Autofac简介 Autofac和其他容器的不同之处是它和C#语言的结合非常紧密,在使用过程中对你的应用的侵入性几乎为零,更容易与第三方的组件集成.Autofac的主要特性如下: 组件侵入性为零 ...

  5. 三大框架 之 Spring(IOC控制反转、DI依赖注入)

    目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...

  6. Spring 01: Spring配置 + IOC控制反转 + Setter注入

    简介 Spring框架是一个容器,是整合其他框架的框架 他的核心是IOC(控制反转)和AOP(面向切面编程),由20多个模块构成,在很多领域都提供了优秀的问题解决方案 特点 轻量级:由20多个模块构成 ...

  7. Spring-初识Spring框架-IOC控制反转(DI依赖注入)

    ---恢复内容开始--- IOC :控制反转 (DI:依赖注入)使用ioc模式开发 实体类必须有无参构造方法1.搭建Spring环境下载jarhttp://maven.springframework. ...

  8. Spring的IOC控制反转和依赖注入-重点-spring核心之一

    IoC:Inverse of Control(控制反转): 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,好比于MVC.就是将原本在程序中手动创建对象的控制权,交由S ...

  9. Spring 04: IOC控制反转 + DI依赖注入

    Spring中的IOC 一种思想,两种实现方式 IOC (Inversion of Control):控制反转,是一种概念和思想,指由Spring容器完成对象创建和依赖注入 核心业务:(a)对象的创建 ...

随机推荐

  1. vue 中 vue-router、transition、keep-alive 怎么结合使用?

    <transition :name="name" mode="out-in" name="fade"> <keep-ali ...

  2. JavaScript正则表达式基础

    ECMAScript 3 开始支持正则表达式,其语法和 Perl 语法很类似,一个完整的正则表达式结构如下: var expression = / pattern / flags ; 其中,模式(pa ...

  3. dotnet中Stream、string及byte[]的相关操作

    string与byte[](UTF-8) //string to byte[] string str = "abc中文"; //0x61 0x62 0x63 0xE4 0xB8 0 ...

  4. Excel日期中那个著名的bug

    一个软件中的bug能够持续多久?答案不一,大多数bug在软件测试阶段就已经被干掉,又有许多死在Preview阶段,抑或正式上线后不久被干掉,有些则伴随软件终生,直到下一代产品发布才寿终正寝,而Exce ...

  5. dat.gui stats.js 通用参数配置及图像统计工具

    在网上看到了一个非常好的JS烟雾效果 https://paveldogreat.github.io/WebGL-Fluid-Simulation/看源码时发现了dat.gui很好用. dat.gui ...

  6. java常用API的总结(1)

    本篇是对于这一段时间以来接触到的常用api的一些总结,便于以后的查阅.... 一.正则表达式 对于正则表达式,我的感觉就是当我们在做某些题的时候正则表达式会省去我们很多的时间,并且正则表达式的使用格式 ...

  7. 网络学习笔记(二):TCP可靠传输原理

      TCP数据段作为IP数据报的数据部分来传输的,IP层提供尽最大努力服务,却不保证数据可靠传输.TCP想要提供可靠传输,需要采取一定的措施来让不可靠的传输信道提供可靠传输服务.比如:出现差错时,让发 ...

  8. stylus 详解与引入

    Stylus介绍及特点Stylus 是一个基于Node.js的CSS的预处理框架,诞生于2010年,比较年轻,可以说是一种新型语言,其本质上做的事情与 Sass/LESS 等类似, 可以以近似脚本的方 ...

  9. Hadoop3.2.0使用详解

    1.概述 Hadoop3已经发布很久了,迭代集成的一些新特性也是很有用的.截止本篇博客书写为止,Hadoop发布了3.2.0.接下来,笔者就为大家分享一下在使用Hadoop3中遇到到一些问题,以及解决 ...

  10. 搞懂MySQL InnoDB B+树索引

    一.InnoDB索引 InnoDB支持以下几种索引: B+树索引 全文索引 哈希索引 本文将着重介绍B+树索引.其他两个全文索引和哈希索引只是做简单介绍一笔带过. 哈希索引是自适应的,也就是说这个不能 ...