Spring注解详解及案例

Spring注解浅入浅出——不吹牛逼不装逼

前情提要

上文书咱们说了《Spring浅入浅出》,对Spring的核心思想看过上篇的朋友应该已经掌握了,此篇用上篇铺垫,引入注解,继续深入学习。

 

引入注解

在Spring框架中,尽管使用XML配置文件可以很简单地装配Bean,但如果应用中有大量的Bean需要装配,会导致XML配置文件过于庞大,不方便以后的升级与维护,因此,推荐开发者使用注解的方式去装配Bean。

 

注解详解

对注解不清楚的可以看我博客《注解》 ,其实注解就是一个类,在开发中使用注解取代XML配置文件。

1. @Component 取代 <bean class=""></bean>
    @Component("id的值")

2. web开发中,提供3个@Component注解衍生注解(功能一样)取代 <bean class=""></bean>
    @Repository :dao层
    @Service :service层
    @Controller :web层
        注意:SpringMVC 中推荐使用注解哦!
3. 依赖注入,给私有字段设置,也可以给setter方法设置
    普通值:@Value("")
    引用值:
        方式1:按照【类型】注入
            @Autowired
        方式2:按照【类型+名称】注入1
            @Autowired
            @Qualifier("名称")
        方式3:按照【名称】注入2
            @Resource("名称")
4. 生命周期
    @PostConstruct :初始化
    @PreDestroy :销毁
5. 作用域
    @Scope("prototype") 多例

 

配置注解

使用注解有了Bean的实现类,但现在Spring容器并不知道去哪里扫描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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
    <!—使用context命名空间,通过Spring扫描指定包annotation及子包下所有Bean的实现类,进行注释解析-->
    <context:component-scan base-package="com.my"></context:component-scan>

</beans>

  

 

案例实践

一、Pom.xml

<dependencies>

  <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.11</version>

    <scope>test</scope>

  </dependency>

  <dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>javax.servlet-api</artifactId>

    <version>3.1.0</version>

  </dependency>

  <dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>jstl</artifactId>

    <version>1.2</version>

  </dependency>

  <dependency>

    <groupId>commons-logging</groupId>

    <artifactId>commons-logging</artifactId>

    <version>1.2</version>

  </dependency>



  <!--spring核心依赖-->

  <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>5.1.38</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-beans</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aop</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jdbc</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-expression</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-tx</artifactId>

    <version>5.1.5.RELEASE</version>

  </dependency>



</dependencies>

  

 

 

二、spring-config.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"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       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.xsd

       http://www.springframework.org/schema/tx

       http://www.springframework.org/schema/tx/spring-tx.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc.xsd

        ">

    <!--配置注解要扫描的包-->

    <context:component-scan base-package="com.my"></context:component-scan>
</beans>

  

三、UserDao

package com.my.dao;



public interface UserDao {

    public void add();

    public void delete();

    public void update();

    public void query();

}

  

 

四、UserDaoImpl

package com.my.dao.impl;



import com.my.dao.UserDao;

import org.springframework.stereotype.Repository;



@Repository

public class UserDaoImpl implements UserDao {

    @Override

    public void add() {

        System.out.println("UserDao中的添加功能实现了");

    }



    @Override

    public void delete() {

        System.out.println("UserDao中的删除功能实现了");

    }



    @Override

    public void update() {

        System.out.println("UserDao中的修改功能实现了");

    }



    @Override

    public void query() {

        System.out.println("UserDao中的查询功能实现了");

    }

}

  

 

五、UserService

package com.my.service;



public interface UserService {

    public void add();

    public void delete();

    public void update();

    public void query();

}

  

 

六、UserServiceImpl

package com.my.service.impl;



import com.my.dao.UserDao;

import com.my.dao.impl.UserDaoImpl;

import com.my.service.UserService;

import org.springframework.stereotype.Service;



@Service

public class UserServiceImpl implements UserService {



    private UserDao userDao = new UserDaoImpl();

    @Override

    public void add() {

        userDao.add();

    }



    @Override

    public void delete() {

        userDao.delete();

    }



    @Override

    public void update() {

        userDao.update();

    }



    @Override

    public void query() {

        userDao.query();

    }

}

  

 

七、UserController

package com.my.controller;



import com.my.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Controller;

import org.springframework.stereotype.Service;



@Controller

public class UserController {

    @Autowired

    private UserService userService;



    public void add(){

        userService.add();

    }



    public void delete(){

        userService.delete();

    }



    public void update(){

        userService.update();

    }



    public void query(){

        userService.query();

    }

}

  

 

八、测试Test2

package com.my.test;



import com.my.controller.UserController;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test2 {

    public static void main(String[] args) {

        ApplicationContext appCon = new ClassPathXmlApplicationContext("spring-config.xml");

        UserController userController = (UserController) appCon.getBean("userController");



        userController.add();

        userController.delete();

        userController.update();

        userController.query();

    }

}

  

 

 

九、测试结果

 

 

 

 

结束总结

看完此篇及上篇的朋友应该掌握Bean的两种常用装配方式,即基于XML配置的装配和基于注解的装配,其中此篇基于注解的装配方式尤其重要,它是当前的主流装配方式。

*****************************************************************************************************

我的博客园地址:https://www.cnblogs.com/zyx110/

  本文已独家授权给脚本之家(jb51net)公众号独家发布

posted @ 2019-08-06 16:52  泰斗贤若如  阅读(1788)  评论(2编辑  收藏  举报