1、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架构图:
2、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方式和注解的方式。
3、Spring IoC的XML配置
一、添加Spring基本依赖包
aopalliance-1.0.jar
commons-logging-1.1.1.jar
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
二、添加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>
三、分别建立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());
- }
-
-
- }
四、IoC容器测试
- ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
- ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
- exampleBean.print();
- exampleBean.userPrint();
输出:
Name is :boya
User name is :boya
4、Spring IoC注解
Spring 的依赖配置方式与 Spring 框架的内核自身是松耦合设计的。然而,直到 Spring 3.0 之前,使用 XML 进行依赖配置几乎是惟一的选择。Spring 3.0 的出现改变了这一情况,它提供了一系列的针对依赖注入的注解,这使得 Spring IoC 在 XML 文件以外多了一种可行的选择。下面介绍如何使用这些注解进行依赖配置的管理。
我将注解分为两类,第一类用于属性装配,第二类用于类的注册。
属性装配使用的注解通常有两种,分别是Autowired、Resource。
@Autowired
一、@Autowired默认按照类型匹配的方式(byType)进行注入
三、@Autowired注解能够用于成员变量、setter方法、构造器函数等
四、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出异常
五、Spring 容许咱们经过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了
@Resource
一、@Resource 的做用至关于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了
二、@Resource 有两个属性,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。因此若是使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。
须要将某个类在IoC容器注册时,可使用@Component、@Repository、@Service和 @Controller
@Component
一、@Component是全部受Spring管理组件的通用形式,而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(分别对应了持久化层、服务层和表现层)
二、使用@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属性指定了须要扫描的类包,类包及其递归子包中全部的类使用的注解都会被处理。
二、在类中添加注解
UserDao类:
- @Repository
- public class UserDao {
- public String getName() {
- return "boya";
- }
- }
ExampleBean类:
- @Service
- public 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方法就能够去除了。
三、IoC容器测试
- ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
- ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
- exampleBean.print();
- exampleBean.userPrint();
输出结果:
Name is :boya
User name is :boya