Spring学习笔记(1)---------初识Spring

一、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框架的总体架构图


二、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的设计理念

其实Spring就是面向Bean的编程(BOP,Bean Oriented Programming),Bean在Spring 中才是真正的主角。

Bean在Spring中作用就像Object对OOP的意义一样,没有对象的概念就像没有面向对象编程,Spring中没有Bean也就没有Spring存在的意义。就像一次演出舞台都准备好了但是却没有演员一样。为什 么要Bean这种角色Bean或者为何在Spring如此重要,这由Spring框架的设计目标决定,Spring为何如此流行,我们用Spring的原因是什么,想想你会发现原来Spring解决了一个非常关键的问题他可以让 你把对象之间的依赖关系转而用配置文件来管理,也就是他的依赖注入机制。而这个注入关系在一个叫Ioc容器中管理,那Ioc容器中有又是什么就是被Bean包裹的对象。Spring正是通过把对象包装在 Bean中而达到对这些对象管理以及一些列额外操作的目的。

它这种设计策略完全类似于Java实现OOP的设计理念,当然了Java本身的设计要比Spring复杂太多太多,但是都是构建一个数据结构,然后根据这个数据结构设计他的生存环境,并让它在这个环境中 按照一定的规律在不停的运动,在它们的不停运动中设计一系列与环境或者与其他个体完成信息交换。这样想来回过头想想我们用到的其他框架都是大慨类似的设计理念。

核心组件如何协同工作

前面说Bean是Spring中关键因素,那Context和Core又有何作用呢?前面吧Bean比作一场演出中的演员的话,那Context就是这场演出的舞台背景,而Core应该就是演出的道具了。只有他们在一起才能 具备能演出一场好戏的最基本的条件。当然有最基本的条件还不能使这场演出脱颖而出,还要他表演的节目足够的精彩,这些节目就是Spring能提供的特色功能了。


 
三、Spring IoC的XML配置
1、添加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
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类: 

    
    
  1. public class UserDao {
  2. public String getName() {
  3. return "boya";
  4. }
  5. }
ExampleBean类: 
public class ExampleBean {
    private String name;
    private UserDao userDao;
<span class="keyword" style="font-weight:bold;">public</span> <span class="keyword" style="font-weight:bold;">void</span> print(){
    System.<span class="keyword" style="font-weight:bold;">out</span>.println(<span class="string" style="color:rgb(221,17,68);">"Name is :"</span>+name);
}

<span class="keyword" style="font-weight:bold;">public</span> <span class="keyword" style="font-weight:bold;">void</span> userPrint(){
    System.<span class="keyword" style="font-weight:bold;">out</span>.println(<span class="string" style="color:rgb(221,17,68);">"User name is :"</span>+userDao.getName());
}

<span class="comment" style="color:rgb(153,153,136);font-style:italic;">//省略getter、setter方法</span>

}

4、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
 
四、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类: 

   
   
  1. @Repository
  2. public class UserDao {
  3. public String getName() {
  4. return “boya”;
  5. }
  6. }
ExampleBean类: 

   
   
  1. @Service
  2. public class ExampleBean {
  3. @Resource
  4. @Value( “boya”)
  5. private String name;
  6. @Resource
  7. private UserDao userDao;
  8. public void print(){
  9. System.out.println( “Name is :”+name);
  10. }
  11. public void userPrint(){
  12. System.out.println( “User name is :”+userDao.getName());
  13. }
  14. }
前面介绍了,用于装配属性的注解@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 :boya
User name is :boya
 
 
 

转自:https://blog.csdn.net/u011225629/article/details/45417519

猜你喜欢

转载自blog.csdn.net/qq_15014327/article/details/84578639