Spring3.x教程(一) Spring及Spring IoC介绍
来源:互联网 发布:java释放内存空间 编辑:程序博客网 时间:2024/05/17 08:35
一、Spring是什么
通常说的Spring其实指的是Spring Framework,它是Spring下的一个子项目,Spring围绕Spring Framework这个核心项目开发了大量其他项目,比如Spring Security,Spring Data,Spring WebFlow等等。
Spring是为简化Java EE开发而生,而在Java EE中使用最多的就是Spring Framework,接下来我们主要就是学习Spring Framework。
Spring Framework包括他的核心解决方案IoC容器、Spring AOP。另外,还有对Web、数据访问层的支持。
下面是Spring Framework架构图:
二、Spring IoC容器
Spring IoC(Inversion of Control,控制反转)是Spring Framework的最核心的部分。
所谓控制反转,是指通过使用IoC容器对象依赖关系的管理被反转了,也就是说,对象之间的依赖关系由IoC容器进行管理,并且由Ioc容器通过依赖注入(DI,Dependency Injection)的方式来完成对象的注入。
在使用Spring的开发中,我们需要将所有的类在Spring的IoC容器中进行注册,告诉Spring你是什么,你需要什么,然后IoC容器会在你需要哪个对象时为你创建这个对象以及该对象依赖的其他对象实例(这就是依赖注入)。这些类的创建、销毁都会交由IoC容器来管理,而不再由引用它的对象维护。将以前的“对象-对象”的依赖模式转变为了“对象-IoC容器-对象”的依赖模式。
Spring提供了两种注册IoC容器的方式:XML方式和注解的方式。
三、Spring IoC的XML配置
1、添加Spring基本依赖包
aopalliance-1.0.jarcommons-logging-1.1.1.jarspring-aop-3.2.0.RELEASE.jarspring-beans-3.2.0.RELEASE.jarspring-context-3.2.0.RELEASE.jarspring-core-3.2.0.RELEASE.jarspring-expression-3.2.0.RELEASE.jar
2、添加Spring配置文件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.0.xsd"> <bean id="userDao" class="com.boya.spring.ioc.UserDao" /> <bean id="exampleBean" class="com.boya.spring.ioc.ExampleBean"> <property name="name" value="boya" /> <property name="userDao" ref="userDao" /> </bean></beans>
3、分别创建User类和ExampleBean类
User类:
public class UserDao { public String getName() { return "boya"; }}
ExampleBean类:
public class ExampleBean { private String name; private UserDao userDao; public void print(){ System.out.println("Name is :"+name); } public void userPrint(){ System.out.println("User name is :"+userDao.getName()); } //省略getter、setter方法}
4、IoC容器测试
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);exampleBean.print();exampleBean.userPrint();
输出:
Name is :boyaUser name is :boya
四、Spring IoC注解
Spring 的依赖配置方式与 Spring 框架的内核自身是松耦合设计的。然而,直到 Spring 3.0 以前,使用 XML 进行依赖配置几乎是唯一的选择。Spring 3.0 的出现改变了这一状况,它提供了一系列的针对依赖注入的注解,这使得 Spring IoC 在 XML 文件之外多了一种可行的选择。下面介绍如何使用这些注解进行依赖配置的管理。
我将注解分为两类,第一类用于属性装配,第二类用于类的注册。
属性装配使用的注解一般有两种,分别是Autowired、Resource。
@Autowired
1、@Autowired默认按照类型匹配的方式(byType)进行注入
3、@Autowired注解可以用于成员变量、setter方法、构造器函数等
4、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出异常
5、Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了
@Resource
1、@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了
2、@Resource 有两个属性,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。
需要将某个类在IoC容器注册时,可以使用@Component、@Repository、@Service和 @Controller
@Component
1、@Component是所有受Spring管理组件的通用形式,而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(分别对应了持久化层、服务层和表现层)
2、使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如UserDao类定义的Bean名称就是userDao。你也可以指定Bean的名称: @Component("abc")
下面,我们把上面的示例改为注解配置的形式。
首先,需要修改配置文件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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.boya.spring.ioc" /></beans>
<context:component-scan />的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类使用的注解都会被处理。
2、在类中添加注解
UserDao类:
@Repositorypublic class UserDao { public String getName() { return "boya"; }}
ExampleBean类:
@Servicepublic class ExampleBean { @Resource @Value("boya") private String name; @Resource private UserDao userDao; public void print(){ System.out.println("Name is :"+name); } public void userPrint(){ System.out.println("User name is :"+userDao.getName()); }}
前面介绍了,用于装配属性的注解@Autowired和@Service是可以用于成员变量的,所以当我们在成员变量设置这两个注解后,setter方法就可以去除了。
3、IoC容器测试
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);exampleBean.print();exampleBean.userPrint();
输出结果:
Name is :boyaUser name is :boya
Spring Ioc XML配置示例代码:http://download.csdn.net/detail/boyazuo/5165204
Spring IoC注解配置示例代码:http://download.csdn.net/detail/boyazuo/5165191
- Spring3.x教程(一) Spring及Spring IoC介绍
- Spring学习(一)---IoC介绍
- Spring3.x教程(二) Spring AOP
- Spring3.0学习札记一Spring IOC控制反转(1)
- Spring3.0学习札记一Spring IOC控制反转(2)
- Spring3.0学习札记一Spring IOC控制反转(3)
- Spring3.0学习札记一Spring IOC控制反转(4)
- Spring3.x--Spring消息
- Spring教程系列(一)-IoC与DI
- spring3.X -----> spring 4.X
- Spring学习(一)概念及IOC
- Spring IoC教程
- Spring ioc注解教程
- Spring学习教程-IOC
- Spring的IoC介绍
- Spring IOC介绍
- Spring IoC介绍
- Spring IOC原理介绍
- git diff
- whois命令简介
- 【zoj2760】【最大流】How Many Shortest Path
- POJ1458--Common Subsequence
- System.Data.OracleClient需要Oracle客户端软件8.1.7或更高版本 解决
- Spring3.x教程(一) Spring及Spring IoC介绍
- SQL学习笔记4
- VS2010 Addins 外接程序(插件)开发
- 缓存技术
- POJ3624--Charm Bracelet--0-1背包
- 高度不够高(站得高才能看的远)
- 如何在12周内由零基础成为一名程序员
- eclipse设置字体大小
- Lock obtain timed out: Lock@的解决方法(lucene)