一。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. .NET Core 时代已经到了,你准备好了吗

    今天很多人都收到了阿里云函数计算支持.NET Core的短信了. 通过访问 https://help.aliyun.com/document_detail/112379.html 你可以看到最新的说明 ...

  2. 不可思议的纯 CSS 滚动进度条效果

    结论先行,如何使用 CSS 实现下述滚动条效果? 就是顶部黄色的滚动进度条,随着页面的滚动进度而变化长短. 在继续阅读下文之前,你可以先缓一缓.尝试思考一下上面的效果或者动手尝试一下,不借助 JS , ...

  3. 两个inline-block中间有空白,解决inline-block 元素之间的空白问题

    目录 一.遇到的问题 二.举个简单的栗子分析问题 三.解决办法 一.遇到的问题 前些天写瀑布流布局的时候,发现明明计算好了宽度使得一行能放下三张图片,实际效果却总是放不下,图片会挤到下一行去.上图: ...

  4. 我是如何自学 Python 的

    不少初学 Python 或者准备学习 Python 的小伙伴问我如何学习 Python.今天就说说我当时是怎么学习的. 缘起 我大学专业是电气工程,毕业后做的是自动化方面的工作.对于高级语言编程基本是 ...

  5. 吴恩达机器学习笔记61-应用实例:图片文字识别(Application Example: Photo OCR)【完结】

    最后一章内容,主要是OCR的实例,很多都是和经验或者实际应用有关:看完了,总之,善始善终,继续加油!! 一.图像识别(店名识别)的步骤: 图像文字识别应用所作的事是,从一张给定的图片中识别文字.这比从 ...

  6. 爬虫须知的HTTP协议

    HTTP请求: 1.HTTP请求主要分为"Get"和"Post"两种方法. 2.当我们在浏览器输入URL http://www.baidu.com 的时候, 浏 ...

  7. Self Host 使用 Exceptionless 实时监控程序运行日志服务

    Exceptionless 是一个可以对 ASP.NET Core, ASP.NET MVC,WebAPI, WebForms, WPF, Console 应用提供系统的日志,错误监控.报表等服务实时 ...

  8. C#版[击败99.69%的提交] - Leetcode 242. 有效的同构异形词 - 题解

    C#版 - Leetcode 242. 有效的同构异形词 - 题解 Leetcode 242.Valid Anagram 在线提交: https://leetcode.com/problems/val ...

  9. 从零开始学习PYTHON3讲义(十)自己做一个“电子记事本”

    <从零开始PYTHON3>第十讲 截至上一讲,我们已经完成了Python语言的基本部分.我们用了三讲来讨论Python语言的控制结构,用了两讲来介绍Python的基本数据类型.可以说仅就语 ...

  10. 【反编译系列】四、反编译so文件(IDA_Pro)

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 安卓应用程序的开发语言是java,但是由于java层的代码很容易被反编译,而反编译c/c++程序的难度比较大,所以现在很多安卓应用程 ...