zoukankan      html  css  js  c++  java
  • javaEE架构程序设计-Spring IOC容器_注解配置方式

    Spring IOC容器_注解配置方式

    1. 常用的注解
      (1)在容器器定义bean
      @Repository 仓库
      @Service 服务
      @Controller 控制器
      @Component 组件

    从编程的角度讲,无论采用哪一种注解方式,都是定义了一个bean,仅仅是逻辑含义不同而已从编程的角度讲,无论采用哪一种注解方式,都是定义了一个bean,仅仅是逻辑含义不同而已

    (2)DI,对象成员的初始化
    @Resource 对象
    @Value 基本数据类型,字符串,数值型,int

    1. 使用注解 编程思路
      (1)pom.xml文件中引入spring-context框架
      (2)在applicationContext.xml中设置注解支持
      (3)使用注解,让spring容器,自动的创建对象

    2. 创建项目
      spring02
      创建了包 gyh.yogurt.spring02
      创建类 Application

    3. 引入spring-context

    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>5.1.20.RELEASE</version>
    </dependency>
    
    1. 配置spring容器配置类,applicationContex.xml
      配置注解
    <context:component-scan base-package="yzg.kc2021.spring02"></context:component-scan>
    
    1. 以注解的方式创建bean

    (1)创建接口

    IStudent

    public interface IStudent {
        public void Create();       // 创建
    }
    

    (2)创建类Student,同时,在类的定义中,应用注解,创建bean

    @Repository("s1")
    public class Student implements IStudent {
        public void Create() {
            System.out.println("Student对象创建..." + this);
        }
    }
    

    引入ID(依赖注入后)

    //@Repository("s1")
    @Component("s1")
    public class Student implements IStudent {
    
        @Value("zhangsan")
        private String name = "";       //  姓名
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void Create() {
            System.out.println("Student(" + name + ")对象创建..." + this);
        }
    }
    
  • 相关阅读:
    164 Maximum Gap 最大间距
    162 Find Peak Element 寻找峰值
    160 Intersection of Two Linked Lists 相交链表
    155 Min Stack 最小栈
    154 Find Minimum in Rotated Sorted Array II
    153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
    152 Maximum Product Subarray 乘积最大子序列
    151 Reverse Words in a String 翻转字符串里的单词
    bzoj3994: [SDOI2015]约数个数和
    bzoj 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/suanai/p/14592768.html
Copyright © 2011-2022 走看看