zoukankan      html  css  js  c++  java
  • 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 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.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
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
    5.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    6.     <bean id="userDao" class="com.boya.spring.ioc.UserDao" />  
    7.     <bean id="exampleBean" class="com.boya.spring.ioc.ExampleBean">  
    8.         <property name="name" value="boya" />  
    9.         <property name="userDao" ref="userDao" />  
    10.     </bean>  
    11. </beans>  
    3、分别创建User类和ExampleBean类
    User类:
    1. public class UserDao {  
    2.     public String getName() {  
    3.         return "boya";  
    4.     }  
    5. }   
    ExampleBean类:
    1. public class ExampleBean {  
    2.     private String name;  
    3.     private UserDao userDao;  
    4.       
    5.     public void print(){  
    6.         System.out.println("Name is :"+name);  
    7.     }  
    8.       
    9.     public void userPrint(){  
    10.         System.out.println("User name is :"+userDao.getName());  
    11.     }  
    12.       
    13.     //省略getter、setter方法  
    14. }  
    4、IoC容器测试
    1. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
    2. ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);  
    3. exampleBean.print();  
    4. 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,如下:
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xmlns:context="http://www.springframework.org/schema/context"  
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
    6.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    7.            http://www.springframework.org/schema/context   
    8.            http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    9.     <context:component-scan base-package="com.boya.spring.ioc" />  
    10. </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.       
    9.     public void print(){  
    10.         System.out.println("Name is :"+name);  
    11.     }  
    12.       
    13.     public void userPrint(){  
    14.         System.out.println("User name is :"+userDao.getName());  
    15.     }  
    16. }  
    前面介绍了,用于装配属性的注解@Autowired和@Service是可以用于成员变量的,所以当我们在成员变量设置这两个注解后,setter方法就可以去除了。
     
    3、IoC容器测试
    1. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
    2. ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);  
    3. exampleBean.print();  
    4. exampleBean.userPrint();  
    输出结果:
    Name is :boya
    User name is :boya
  • 相关阅读:
    Linux时间同步
    idea中创建多module时,找不到创建class文件问题
    Docker中安装Redis并设置外网可访问
    Docker简介与安装
    RabbitMQ基础篇
    git emoji
    RabbitMQ安装
    ActiveMQ
    消息中间件介绍
    IDEA使用GsonFormat完成JSON和JavaBean之间的转换
  • 原文地址:https://www.cnblogs.com/Raymond-YYC/p/4054294.html
Copyright © 2011-2022 走看看