Spring的学习之旅(一)
来源:互联网 发布:淘宝以前的评价不见了 编辑:程序博客网 时间:2024/06/05 11:24
Spring是javaee体系中比较重要的框架,作为一个java程序员,应该很好的掌握它。那么Spring到底是一个什么东西呢?(春天到了,夏天是不是也快来了呢?当然不是!)
- spring是一个轻量级java框架,使用它你可以不实现它提供的API;
- spring能和其他的第三方框架很好的嵌套使用;
- spring的DI/IOC(依赖注入),反转资源的获取方向。IOC是一个创建Bean并管理Bean的一个容器,使用Spring,能更好的管理JaveBean。以前使用一个对象时,我们需要new一个,而使用spring,我们只需要告诉它资源获取方式,它就会主动把对象推送给我们;
- spring的AOP(面向切面编程),主要是用来写日志,事务及权限管理。 -
我使用的IDE是Eclipse,使用Spring需下载相关插件,Eclipse内就可下载,相关教程参阅:http://www.cnblogs.com/wenruo/p/6283024.html
导入jar包
使用Spring需要导入相应的jar包,我是从maven官网上下载下来的相关依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
配置xml文件
生成Spring Bean Configuration File
加入相应命名空间:打开xml文件后,点击下方的Namespaces,增加相应命名空间(每个命名空间的含义,以后再详述)
创建JavaBean
Car:`package test.newtest;
/**
* @Tittle:Car
* @author Administrator
* @date 2017年9月26日下午2:50:33
*/
public class Car {
private int no;
private String name;
public Car() { super(); // TODO Auto-generated constructor stub}public Car(int no, String name) { super(); this.no = no; this.name = name;}public int getNo() { return no;}public void setNo(int no) { this.no = no;}public String getName() { return name;}public void setName(String name) { this.name = name;}@Overridepublic String toString() { return "Car [no=" + no + ", name=" + name + "]";}
}`
XML文件中配置Bean
<?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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!--通过依赖注入的方式配置Bean,依赖注入的方式有: - 构造函数注入; - setter注入; - 静态工厂方法注入; - 实例工厂方法注入。--><!-- 通过配置car 采用p标签是为了节省命名空间--><bean name="car" class="test.newtest.Car" p:no="1" p:name="BWM"></bean><!-- 通过构造函数配置Car(用index来指定构造函数的顺序) --><bean name="car2" class="test.newtest.Car"><constructor-arg index="1" value="fsss"></constructor-arg><constructor-arg index="0" value="1"></constructor-arg></bean><!-- 通过构造函数配置Car(用name来指定构造函数) -->**<!-- 当通过构造函数配置Bean时,既没有指定Index也没有指定Name时,spring会按照类的构造函数顺序进行注入 -->**<bean name="car3" class="test.newtest.Car"><constructor-arg name="name" value="fsss"></constructor-arg><constructor-arg name="no" value="1"></constructor-arg></bean><!-- 通过setter配置Car --><bean name="car4" class="test.newtest.Car"><property name="name" value="sjfs"></property><property name="no" value="2"></property></bean></beans>
获取Bean
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App { public static void main( String[] args ) { /*使用Spring的ApplicationContext来解析配置文件,其实现类有:ClassPathXmlApplicationContext(加载ClassPath下的XML文件)和FileSystemXmlApplicationContext(加载系统文件)*/ ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-rel.xml"); //通过Bean的name获取: Car car =(Car) ctx.getBean("car4"); /*通过Bean的类型获取: Car car =ctx.getBean(Car.class); */ System.out.println(car.toString()); }}
- Spring的学习之旅(一)
- 一.Spring学习之旅----初识Spring
- spring boot 学习之旅(一)
- Spring学习之旅(一) 依赖注入(DI)与Spring Bean的装配
- spring的学习(一)
- spring学习之AOP(一)
- Spring AOP学习之:一
- Spring学习之SpEL(一)
- Spring学习笔记一之模拟Spring
- Spring学习之Spring概述(一)
- Spring的学习之路(一)Bean
- 学习spring mvc之路一:简单的Hello word
- Spring框架的学习之路(一)
- Spring Boot学习(一)之Controller的使用
- 我的spring之旅一
- spring学习之FactoryBean学习(一)
- Spring-Cloud 学习之旅 --- 快速开始(一)
- Spring Boot学习之旅:(一)HELLO WORLD
- 干货分享《如何系统的学好Python》
- 预告 | 如何系统的学好Python
- MySQL工具推荐 | 基于MySQL binlog的flashback工具
- 运维之春:2017运维前线解密
- 又搞飞机了,号称有五重备份的GitLab居然也歇了
- Spring的学习之旅(一)
- 公开课发布《MySQL Group Replication思想》
- 公开课发布《MySQL体系结构及常见高可用架构》
- 专栏 | MySQL 5.7系列之sys schema(1) by吴炳锡
- [译文]MySQL发生死锁肿么办?
- 有人@你 | ACMUG 2016 年会活动视频发布
- 来来来,美女DBA教你MySQL高可用架构选型
- MySQL的Crash Safe和Binlog的关系
- 纳尼,mysqldump导出的数据居然少了40万?