zoukankan      html  css  js  c++  java
  • 吴裕雄天生自然SPRINGSpring IoC

    1.声明Bean的注解


    (1)@Component 该注解是一个泛化的概念,仅仅表示一个组件对象(Bean),可以作用在任何层次上,没有明确的角色。 (2)@Repository 该注解用于将数据访问层(DAO)的类标识为Bean,即注解数据访问层Bean,其功能与@Component()相同。 (3)@Service 该注解用于标注一个业务逻辑组件类(Service层),其功能与@Component()相同。 (4)@Controller 该注解用于标注一个控制器组件类(Spring MVC的Controller),其功能与@Component()相同。
    2.注入Bean的注解
    
       (1)@Autowired
        该注解可以对类成员变量、方法及构造方法进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除setter 和getter方法。默认按照Bean的类型进行装配。
       (2)@Resource
        该注解与@Autowired功能一样。区别在于,该注解默认是按照名称来装配注入的,只有当找不到与名称匹配的Bean才会按照类型来装配注入;而@Autowired默认按照Bean的类型进行装配,如果想按照名称来装配注入,则需要结合@Qualifier注解一起使用。
        @Resource注解有两个属性:name和type。name属性指定Bean实例名称,即按照名称来装配注入;type属性指定Bean类型,即按照Bean的类型进行装配。
       (3)@Qualifier
        该注解与@Autowired注解配合使用。当@Autowired注解需要按照名称来装配注入,则需要结合该注解一起使用,Bean的实例名称由@Qualifier注解的参数指定。
    1.使用Eclipse创建Web应用并导入JAR包
        2.创建DAO层
        3.创建Service层
        4.创建Controller层
        5.创建配置类
        6.创建测试类
        7.运行结果
    package annotation.dao;
    
    public interface TestDao {
        public void save();
    
    }
    package annotation.dao;
    
    import org.springframework.stereotype.Repository;
    
    @Repository("TestDaoImpl")
    public class TestDaoImpl implements TestDao{
        @Override
        public void save() {
            System.out.println("testDao save");
        }
    
    }
    package annotation.service;
    
    public interface TestService {
        public void save();
    
    }
    package annotation.service;
    
    import javax.annotation.Resource;
    import org.springframework.stereotype.Service;
    import annotation.dao.TestDao;
    
    @Service("TestSeviceImpl")
    public class TestSeviceImpl implements TestService{
        @Resource(name="TestDaoImpl")
        private TestDao testDao;
        @Override
        public void save() {
            testDao.save();
            System.out.println("testService save");
        }
    
    }
    package annotation.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import annotation.service.TestService;
    
    @Controller
    public class TestController {
        @Autowired
        private TestService testService;
        public void save() {
            testService.save();
            System.out.println("testController save");
        }
    
    }
    package annotation;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("annotation")
    public class ConfigAnnotation {
    
    }
    package annotation;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import annotation.controller.TestController;
    
    
    public class TestAnnotation {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(ConfigAnnotation.class);
            TestController tc = appCon.getBean(TestController.class);
            tc.save();
            appCon.close();
        }
    }

  • 相关阅读:
    02 . Vue入门基础之条件渲染,列表渲染,事件处理器,表单控件绑定
    iptables防火墙简介,原理,规则编写,做网络防火墙,常见案例
    Go之发送钉钉和邮箱
    Jumpserver简介,部署及使用
    01 . Go语言实现SSH远程终端及WebSocket
    KVM简介,安装及常见使用,维护详解
    05 . Gin+Vue开发一个线上外卖应用(Session集成及修改用户头像到Fastdfs)
    04 . Gin+Vue开发一个线上外卖应用(用户名密码和图形验证码)
    03 . Gin+Vue开发一个线上外卖应用(用户数据创建,插入,跨域处理)
    02 . Go之Gin+Vue开发一个线上外卖应用(集成第三方发送短信和xorm生成存储数据库表)
  • 原文地址:https://www.cnblogs.com/tszr/p/15309189.html
Copyright © 2011-2022 走看看