zoukankan      html  css  js  c++  java
  • Java第三十八天,Spring框架系列,基于注解的IOC环境搭建(一)

    注解配置和 xml 配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样

    一、基本环境搭建

    1.创建Maven项目

    2.在pom.xml中配置打包方式

    <packaging>jar</packaging>

    3.在pom.xml中配置spring依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
    </dependencies>

    二、四大注解类型

    1.用于创建对象的注解,相当于: <bean id="" class="">

    @Component

    作用:

    把资源让 spring 来管理。相当于在 xml 中配置一个 bean

    属性:

    value:指定 bean 的 id。如果不指定 value 属性,默认 bean 的 id 是当前类的类名。首字母小写

    @Controller、@Service、@Repository

    这三个注解的功能与使用方法等同于@Component注解,只不过有以下行业规则:

    @Controller: 一般用于表现层的注解。

    @Service: 一般用于业务层的注解。

    @Repository: 一般用于持久层的注解。

    2.用于注入数据的注解,相当于: <property name="" ref="">或<property name="" value="">

    @Autowired

    只能注入其他bean类型的数据,而基本类型和String类型无法通过本注解实现

    集合类型的注入只能通过XL来实现

    作用:

    自动按照类型注入。只要容器中有唯一的一个bean对象类型与之匹配,就可以注入成功;当使用注解注入属性时, set 方法可以省略。它只能注入其他 bean 类型。当有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id

    作用位置:

    ①类属性

    ②类方法

    举例:

    package com.huhai.Dao.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Array;
    import java.util.*;
    
    /**
     * 持久层实现类
     */
    /**
     * 创建AccountDaoImpl对象的注解
     */
    @Component
    public class AccountDaoImpl implements IAccountDao {
    
        private String[] myStr;
        private List<String> myList;
        private Set<String> mySet;
        private Map<String, String> myMap;
        private Properties myPro;
    
    }
    
    package com.huhai.Service.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import com.huhai.Dao.Impl.AccountDaoImpl;
    import com.huhai.Service.IAccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * 业务层实现类
     */
    @Component
    public class AccountServiceImpl implements IAccountService {
    
        
        /**
         * 将已经创建的AccountDaoImpl对象注入变量的注解
         */
        @Autowired
        private AccountDaoImpl accountDao;
    
        public void save() {
    
            accountDao.save();
        }
    }
    

    @Qualifier

    只能注入其他bean类型的数据,而基本类型和String类型无法通过本注解实现

    集合类型的注入只能通过XL来实现

    作用:

    在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和@Autowire 一起使用;但是给方法参数注入时,可以独立使用

    属性:

    value:指定 bean 的 id

    例如:

    package com.huhai.Service.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import com.huhai.Dao.Impl.AccountDaoImpl;
    import com.huhai.Service.IAccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * 业务层实现类
     */
    @Component
    public class AccountServiceImpl implements IAccountService {
    
        @Autowired
        @Qualifier(value = "accountDaoImpl")
        private AccountDaoImpl accountDao;
    
        public void save() {
    
            accountDao.save();
        }
    }

    @Resource

    只能注入其他bean类型的数据,而基本类型和String类型无法通过本注解实现

    集合类型的注入只能通过XL来实现

    作用:

    直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型

    属性:

    name:指定 bean 的 id

    例如:

    package com.huhai.Service.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import com.huhai.Dao.Impl.AccountDaoImpl;
    import com.huhai.Service.IAccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    import java.util.Date;
    
    /**
     * 业务层实现类
     */
    @Component
    public class AccountServiceImpl implements IAccountService {
    
        @Resource(name = "accountDaoImpl")
        private AccountDaoImpl accountDao;
    
        public void save() {
    
            accountDao.save();
        }
    }
    

    @Value

    可以注入基本类型和String类型

    集合类型的注入只能通过XL来实现

    可以使用EL表达式

    作用:

    注入基本数据类型和 String 类型数据的

    属性:

    value:用于指定值

    例如:

    package com.huhai.Dao.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Array;
    import java.util.*;
    
    /**
     * 持久层实现类
     */
    @Component
    public class AccountDaoImpl implements IAccountDao {
    
        /**
        * 经过Value注解后,myStr的值就是数组{1, 2, 3, 4, 5, 6}了
        */
        @Value(value = "{1, 2, 3, 4, 5, 6}")
        private String[] myStr;
    }
    

    3.用于改变作用范围的注解,相当于: <bean id="" class="" scope="">

    @Scope

    作用:

    指定 bean 的作用范围

    属性:

    value的取值范围:

    • singleton
    • prototype
    • request
    • session
    • globalsession

    例如:

    package com.huhai.Service.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import com.huhai.Dao.Impl.AccountDaoImpl;
    import com.huhai.Service.IAccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Scope;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    import java.util.Date;
    
    /**
     * 业务层实现类
     */
    @Component
    @Scope(value = "singleton")
    public class AccountServiceImpl implements IAccountService {
    
        @Resource(name = "accountDaoImpl")
        private AccountDaoImpl accountDao;
    
        public void save() {
    
            accountDao.save();
        }
    }
    

    4.用于声明周期的注解,相当于: <bean id="" class="" init-method="" destroy-method="" />

    @PostConstruct

    作用:

    用于指定初始化方法

    @PreDestroy

    作用:

    用于指定销毁方法

    例如:

    package com.huhai.Service.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import com.huhai.Dao.Impl.AccountDaoImpl;
    import com.huhai.Service.IAccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Scope;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
    import java.util.Date;
    
    /**
     * 业务层实现类
     */
    @Component
    @Scope(value = "singleton")
    public class AccountServiceImpl implements IAccountService {
    
        @Resource(name = "accountDaoImpl")
        private AccountDaoImpl accountDao;
    
        public void save() {
    
            accountDao.save();
        }
    
        @PostConstruct
        private void init(){
            System.out.println("对象创建");
        }
    
        @PreDestroy
        private void destroy(){
            System.out.println("对象被销毁");
        }
    }
    
    package com.huhai;
    
    import com.huhai.Service.IAccountService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Realize {
    
        public static void main(String[] args) {
    
            ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as = ac.getBean("accountServiceImpl", IAccountService.class);
            as.save();
            ac.close();
        }
    }
    

    四、本项目搭建流程

    1.在resources目录下新建并编辑bean.xml配置文件如下:

    文件内容可以参考API文档

    spring-framework-5.0.2/spring-framework-5.0.2.RELEASE-docs/spring-framework-reference/core.html#spring-core

    <?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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config/>
    
    </beans>
    
    

    2.被配置文件中添加</context:component-scan>标签告知spring在创建容器时要扫描的包

    <!--告知spring在创建容器时要扫描的包-->
    <context:component-scan base-package=""></context:component-scan>
    <?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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
        <context:annotation-config/>
    
        <!--告知spring在创建容器时要扫描的包-->
        <context:component-scan base-package="com.huhai.Service.Impl"></context:component-scan>
        <context:component-scan base-package="com.huhai.Dao.Impl"></context:component-scan>
    
    </beans>
    
    

    4.持久层接口

    package com.huhai.Dao;
    
    /**
     *持久层接口
     */
    public interface IAccountDao {
        public abstract void save();
    }
    

    5.持久层接口实现类

    package com.huhai.Dao.Impl;
    
    import com.huhai.Dao.IAccountDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    
    import java.lang.reflect.Array;
    import java.util.*;
    
    /**
     * 持久层实现类
     */
    @Repository
    public class AccountDaoImpl implements IAccountDao {
    
        @Value(value = "{1, 2, 3, 4, 5, 6}")
        private String[] myStr;
        private List<String> myList;
        private Set<String> mySet;
        private Map<String, String> myMap;
        private Properties myPro;
    
        public void save() {
            System.out.println(Arrays.toString(myStr));
            System.out.println(myList);
            System.out.println(mySet);
            System.out.println(myMap);
            System.out.println(myPro);
        }
    }
    

    6.业务层接口

    package com.huhai.Service;
    
    /**
     *业务层接口
     */
    public interface IAccountService {
        public abstract void save();
    }

    7.业务层接口实现类

    package com.huhai.Service.Impl;
    
    import com.huhai.Dao.Impl.AccountDaoImpl;
    import com.huhai.Service.IAccountService;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
    import java.util.Date;
    
    /**
     * 业务层实现类
     */
    @Service
    @Scope(value = "singleton")
    public class AccountServiceImpl implements IAccountService {
    
        @Resource(name = "accountDaoImpl")
        private AccountDaoImpl accountDao;
    
        public void save() {
    
            accountDao.save();
        }
    
        @PostConstruct
        private void init(){
            System.out.println("对象创建");
        }
    
        @PreDestroy
        private void destroy(){
            System.out.println("对象被销毁");
        }
    }
    

    8.表现层

    package com.huhai;
    
    import com.huhai.Service.IAccountService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Controller;
    
    @Controller
    public class Realize {
    
        public static void main(String[] args) {
    
            ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as = ac.getBean("accountServiceImpl", IAccountService.class);
            as.save();
            ac.close();
        }
    }
    

    基于注解的 IoC 配置已经完成,但是有一个问题:我们依然离不开 spring 的 xml 配置文件,那么能不能不写这个 bean.xml,所有配置都用注解来实现呢?,下篇博文继续介绍

    作者:蓝月

    -------------------------------------------

    个性签名:能我之人何其多,戒骄戒躁,脚踏实地地走好每一步

  • 相关阅读:
    微信公众号开发授权和分享模块脚本封装
    给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案
    给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组
    项目启动报错:关于modals以及node版本相关
    假设业务需要,在页面一屏中一次性展示大量图片(100张),导致img组件同时发起大量的请求,导致浏览器性能被消耗殆尽,页面卡死。怎么优化?
    假设页面左侧有一个列表,点击列表某一项时,将根据当前id发起一个请求,并将响应结果展示在右侧。如果快速多次点击不同列表项,当网络不稳定时,请求返回的顺序与我点击顺序不符,导致展示的结果不是我最后一次点击的对应结果,怎么办?
    有一个按钮,点击后就发起一次请求,我现在要限制每2S只能发起一次请求,怎么办?
    微信小程序引入外部字体(字体图标过大,引入外链)
    Android项目打包遇com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details
    解决reverse改变原数组
  • 原文地址:https://www.cnblogs.com/viplanyue/p/13573751.html
Copyright © 2011-2022 走看看