Spring(1)—初识
- Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
- 特点:
轻量——从大小与开销两方面而言Spring都是轻量的。
控制反转——Spring通过一种称作控制反转(IoC)的技术促进了低耦合。
面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发
容器——Spring包含并管理应用对象的配置和生命周期
其中最重要的就是AOP和IOC - 一个HelloWorld案例
1.jar包
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
2.类HelloWorld
public class HelloWorld {
public void hello(){
System.out.println("欢迎学习Spring框架!");
}
}
3.新建配置文件applicationContext.xml(已经安装了Spring插件),并添加配置
<?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-3.2.xsd">
<!-- 配置Bean -->
<bean id="helloWorld" class="com.test.hello.HelloWorld"></bean>
</beans>
4.测试类TestHelloWorld
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHelloWorld {
public static void main(String[] args) {
//1.加载applicationContext.xml文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取配配置中的实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
helloWorld.hello();
}
}
运行结果:
Biz's setter...(这行字符是下一个案例中输出的,用于说明,一旦加载了applicationContext.xml配置文件中的Bean不管有没有执行这段代码,Spring容器都会立即向setter方法中执行注入)
欢迎学习Spring框架!
4 . 一个login案例
因为上一个案例已经把jar包导进来了所以直接上代码
①目录结构
②接口及接口的实现类
接口:UserBiz、UserDAO
实现类:UserBizImpl、UserDAOImpl
//UserBiz
public interface UserBiz {
public boolean login(String username, String password);
}
//UserBizImpl
import com.test.login.Biz.UserBiz;
import com.test.login.dao.UserDAO;
public class UserBizImpl implements UserBiz {
//使用userDAO接口声明一个对象
UserDAO userDAO;
//并为其添加set方法,用于依赖注入
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
System.out.println("Biz's setter...");
}
@Override
public boolean login(String username, String password) {
return userDAO.login(username, password);
}
}
//UserDAO
public interface UserDAO {
public boolean login(String username, String password);
}
//UserDAOImpl
import com.test.login.dao.UserDAO;
public class UserDAOImpl implements UserDAO {
@Override
public boolean login(String username, String password) {
if(username.equals("admin") && password.equals("123")){
return true;
}
return false;
}
}
③配置applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 配置Bean -->
<bean id="helloWorld" class="com.test.hello.HelloWorld"></bean>
<!-- 配置创建UserDAOImpl的实例 -->
<bean id="userDAO" class="com.test.login.dao.Impl.UserDAOImpl"></bean>
<!-- 配置创建UserBizImpl的实例,其实现原理是使用反射机制 -->
<bean id="userBiz" class="com.test.login.Biz.Impl.UserBizImpl">
<!-- 引用UserDAOImpl的Bean实例,其实现原理是使用setter方法 -->
<property name="userDAO" ref="userDAO"></property>
</bean>
</beans>
④测试类TestSpringDI
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.login.Biz.UserBiz;
public class TestSpringDI {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserBiz userBiz = (UserBiz) ctx.getBean("userBiz");
System.out.println(userBiz.login("admin", "123"));
}
}
⑤结果:
Biz's setter...
true
Spring(1)—初识的更多相关文章
- spring transaction 初识
spring 事务初识 1.spring事务的主要接口,首先盗图一张,展示出spring 事务的相关接口.Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibern ...
- spring之初识Ioc&Aop
Spring框架的作用 spring是一个轻量级的企业级框架,提供了ioc容器.Aop实现.dao/orm支持.web集成等功能,目标是使现有的java EE技术更易用,并促进良好的编程习惯. Spr ...
- Spring Boot 初识
发展到今天,spring已经是一个大家族了,如果想要使用其中的两到三个组件就会有多复杂的配置,有时候还有会版本不一致的错误,让人很无奈.于是,就有了spring Boot,spring Boot ...
- Spring Boot初识(4)- Spring Boot整合JWT
一.本文介绍 上篇文章讲到Spring Boot整合Swagger的时候其实我就在思考关于接口安全的问题了,在这篇文章了我整合了JWT用来保证接口的安全性.我会先简单介绍一下JWT然后在上篇文章的基础 ...
- Spring Boot初识(3)- Spring Boot整合Swagger
一.本文介绍 如果Web项目是完全前后端分离的话(我认为现在完全前后端分离已经是趋势了)一般前端和后端交互都是通过接口的,对接口入参和出参描述的文档就是Mock文档.随着接口数量的增多和参数的个数增加 ...
- Spring Boot初识(2)- Spring Boot整合Mybaties
一.本文介绍 首先读这篇文章之前如果没有接触过Spring Boot可以看一下之前的文章,并且读这篇文章还需要你至少能写基本的sql语句.我在写这篇文章之前也想过到底是选择JPA还是Mybaties作 ...
- Spring Boot初识(1)-了解Spring Boot
写在前面:半年工作经验的Java程序员一枚,奈何公司用的是自研的Web框架和RPC框架,本着good good study,day day up的精神和为以后发展的考虑觉得自己需要学点开源的东西,写的 ...
- spring batch初识
Spring Batch是什么? Spring Batch是一个基于Spring的企业级批处理框架,按照我师父的说法,所有基于Spring的框架都是使用了spring的IoC特性,然后加上自己的一些 ...
- 【Spring Boot&&Spring Cloud系列】Spring Boot初识
项目代码地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/Spring-boot-helloworld 一.Spring B ...
随机推荐
- [TJOI2018]智力竞赛【网络流】
题解: 这垃圾题意 问题二分之后等价于 可重复路径判断能否覆盖一张图 1.用floyd连边(来保证可重复) 然后拆点跑最大流 然后答案=n-最大流 但这样子做本来复杂度就比较高,边数增加了n倍 2.我 ...
- BZOJ4675
题解: 考虑这么一件事情,n个人买彩票,在不断抽走卡片的时候,他们的中奖概率是不变的 所以在这道题中,由于每个人的操作次数是确定的,所以选每k个点的概率是相同的(因为如果我们逐一考虑每一次操作这个问题 ...
- 安全测试robots
http://stock.pingan.com/robots.txt
- HDU3488 Tour KM
原文链接http://www.cnblogs.com/zhouzhendong/p/8284304.html 题目传送门 - HDU3488 题意概括 给一个n的点m条边的有向图. 然后让你把这个图分 ...
- Java中随机数生成的问题
[生成随机数序列] 我们只能利用Math.random()方法只能生成一个在[0,1)之间的double类型浮点数. 但如果我们想要生成[min, max]之间的随机整数时该怎么办呢? 此时可以用: ...
- Shell工具| 流程控制
1. 流程控制 if 判断 ()[ 条件判断式 ],中括号和条件判断式之间必须有空格 ()if后要有空格 [kris@hadoop datas]$ vim if.sh #!/bin/bash -eq ...
- mvc 过滤器篇
1,重写类作为特性 2 写一个controller ,让其他controller集成 四种过滤器 1身份验证过滤器 2异常处理过滤器 3行为过滤器 4结果过滤器
- gevent实现生产者消费者
from gevent import monkey;monkey.patch_all()from gevent.queue import Queue #队列 gevent中的队列import geve ...
- h5 rem js自动适配
(function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...
- 动手创建 SSD 目标检测框架
参考:单发多框检测(SSD) 本文代码被我放置在 Github:https://github.com/XinetAI/CVX/blob/master/app/gluoncvx/ssd.py 关于 SS ...