一) Spring 介绍、IOC控制反转思想与DI依赖注入
一。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依赖注入的更多相关文章
- Spring 什么是 IOC 控制反转 ?什么是依赖注入?spring的用处 好处 为什么要用
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha Spring是一个开源的控制反转(Inversion of Control ,IoC)和 ...
- IoC(控制反转)和DI(依赖注入)
一.IOC 1.目标类 提供UserService接口和实现类 获得UserService实现类的实例 之前开发中,直接new一个对象即可,使用spring之后,将由spring创建 -->I ...
- Spring IOC(控制反转)思想笔记
Spring IOC(控制反转)思想笔记 IOC控制反转基本理念就是将程序控制权从程序员手中交给用户自定义,从而避免了因为用户一个小需求的变化使得程序员需要改动大量代码. 案例 如果按照之前javaw ...
- 关于.NET中的控制反转(三)- 依赖注入之 Autofac
一.Autofac简介 Autofac和其他容器的不同之处是它和C#语言的结合非常紧密,在使用过程中对你的应用的侵入性几乎为零,更容易与第三方的组件集成.Autofac的主要特性如下: 组件侵入性为零 ...
- 三大框架 之 Spring(IOC控制反转、DI依赖注入)
目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...
- Spring 01: Spring配置 + IOC控制反转 + Setter注入
简介 Spring框架是一个容器,是整合其他框架的框架 他的核心是IOC(控制反转)和AOP(面向切面编程),由20多个模块构成,在很多领域都提供了优秀的问题解决方案 特点 轻量级:由20多个模块构成 ...
- Spring-初识Spring框架-IOC控制反转(DI依赖注入)
---恢复内容开始--- IOC :控制反转 (DI:依赖注入)使用ioc模式开发 实体类必须有无参构造方法1.搭建Spring环境下载jarhttp://maven.springframework. ...
- Spring的IOC控制反转和依赖注入-重点-spring核心之一
IoC:Inverse of Control(控制反转): 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,好比于MVC.就是将原本在程序中手动创建对象的控制权,交由S ...
- Spring 04: IOC控制反转 + DI依赖注入
Spring中的IOC 一种思想,两种实现方式 IOC (Inversion of Control):控制反转,是一种概念和思想,指由Spring容器完成对象创建和依赖注入 核心业务:(a)对象的创建 ...
随机推荐
- 模式识别笔记4-集成学习之AdaBoost
目前集成学习(Ensemble Learning) 分为两类: 个体学习器间存在强依赖关系.必须串行化生成的序列化方法:Boosting 个体学习器间不存在强依赖关系,可同时生成的并行化方法:Bagg ...
- Eureka源码探索(一)-客户端服务端的启动和负载均衡
1. Eureka源码探索(一)-客户端服务端的启动和负载均衡 1.1. 服务端 1.1.1. 找起始点 目前唯一知道的,就是启动Eureka服务需要添加注解@EnableEurekaServer,但 ...
- 图解 -- Win10 OpenSSH
一.安装OpenSSH 客户端 .OpenSSH 服务器 设置 -> 管理可选功能 -> 添加功能 -> [OpenSSH 客户端] [OpenSSH 服务器] 1.设置 2. ...
- 小白学习Python之路---开发环境的搭建
本节内容 1.Python的介绍 2.发展史 3.安装Python 4.搭建开发环境 5.Hello World程序 一.Python的介绍 Python的创始人为荷兰人吉多·范罗苏姆(Guido v ...
- 当List<String> list =new ArrayList<String>(20); 他会扩容多少次
当List<String> list =new ArrayList<String>(20); 他会扩容多少次?A 0 B 1 C 2 D 3答案是A: 因为这个集合 ...
- 【Spark篇】---SparkSql之UDF函数和UDAF函数
一.前述 SparkSql中自定义函数包括UDF和UDAF UDF:一进一出 UDAF:多进一出 (联想Sum函数) 二.UDF函数 UDF:用户自定义函数,user defined functio ...
- 从零开始学习PYTHON3讲义(九)字典类型和插入排序
<从零开始PYTHON3>第九讲 第六讲.上一讲我们都介绍了列表类型.列表类型是编程中最常用的一种类型,但也有挺明显的缺陷,比如: data = [5,22,34,12,87,67,3,4 ...
- MyBridgeWebViewDemo【集成JsBridge开源库的的封装的webview】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用的是第三方库lzyzsd/JsBridge,但是不同的是,将自己封装的MyBridgeWebView通过继承BridgeWebV ...
- django插入数据库错误:mysql的1267错误
错误信息: django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IM ...
- 浅谈JavaWeb架构演变
一 JavaWeb架构演变 在java架构模式中,我们可以将MVC架构模式抽象为如下结构: 1.View层.View层即UI层,可采用的技术如JSP,Structs,SpringMVC等 2.Con ...