zoukankan      html  css  js  c++  java
  • 4.Spring注解方式 实现 IOC 和DI

           1. Spring注解

                  Spring除了默认的使用xml配置文件的方式实现配置之外,也支持使用注解方式实现配置,这种方式效率更高,配置信息清晰,修改更方便,推荐使用。

                  所谓注解就是给程序看的提示信息,很多时候都用来作为轻量级配置的方式。

                  关于注解的知识点,参看java基础课程中java基础加强部分的内容。

                 

           2. Spring引入context名称空间

                  在MyEclipse中导入spring-context-3.2.xsd约束文件,要求Spring来管理。

                  在applicationContext.xml文件中,引入该schema文件:

    **可以将以上头信息加入MyEclipse模版,方便后续自动生成。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
     </beans>

           3. Spring注解方式实现IOC

                  a. 开启包扫描

                  在spring的配置文件中,开启包扫描,指定spring自动扫描哪些个包下的类。只有在指定的扫描包下的类上的IOC注解才会生效。会生成bean对象放到map中

                       

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
     <!-- 配置ioc包扫描 spring启动的时候就会自动扫描指定的包,如果包下的所有类只要有我们特定的spring注解,就会被spring管理起来
     spring 就会生成对应的bean对象,而特定的注解就是@Component @Controller @Service @Repository 其实是一种注解,代表不同层罢了,如果不扫描的话就算有注解那么spring也不会管理这个类创建bean-->
     <context:component-scan base-package="cn.tedu.beens,cn.tedu.p"></context:component-scan>
     </beans>

                  b. 使用注解注册bean

                  在配置的包中的类上使用@Component注解,则这个类会自动被注册为bean,使用当前类的class为<bean>的class,通常情况下使用类名首字母小写为<bean>id。

                  案例:

                            

    @Component
    public class Person {
    String name;
    int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    }

                  c. bean的id

                  通常情况下注解注册bean使用类名首字母小写为bean的id,但是如果类名的第二个字母为大写则首字母保留原样.

                

    cn.tedu.beans.Person --> <bean id="person" class="cn.tedu.beans.Person"/>
    
    cn.tedu.beans.PErson --> <bean id="PErson" class="cn.tedu.beans.Person"/>
    
    cn.tedu.beans.NBA --> <bean id="NBA" class="cn.tedu.beans.NBA"/>

    也可以通过在@Component中配置value属性,明确的指定bean的id 这个很重要

           案例:

    迄今为止,所接触到的Bean都是“无知觉”的,就像黑客帝国中机械工厂里面“养殖”的人类,他们虽然能完成一定的功能,但是根本不知道自己在工厂(BeanFactory)中的代号(id),或者自己是在哪个工厂(BeanFactory的引用)中沉睡。所以,本节的目的就是要创造出一个尼奥一样的Bean,让他知道自己在工厂中的id和自己原来躺在哪个工厂中。这里,称之为,Bean对Spring有知觉。
    但是有言在先,如果要在Spring容器中做到这两点,当然,可以自己通过某些方法实现,代价是大量冗余代码,好处是跟Spring解耦。如果使用Spring提供的接口,好处当然减少代码的规模,“缺点”(如果算的话)是与 Spring耦合。总之,无论采取那种办法还是要看实际需要。
    让Bean对Name有知觉
    作用:让Bean获取自己在BeanFactory配置中的名字(根据情况是id或者name)。

                         可以使bean类实现BeanNameAware接口,并实现其中的setBeanName方法,spring容器会在初始化bean时,调用此方法告知当前bean的id。通过这个方式可以获取bean的id信息。

    package domain;
    
    
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.stereotype.Component;
    
    @Component("a")
    public class Animal implements BeanNameAware{
        private static String beanName=null;
        @Override
        public void setBeanName(String name) {
            this.beanName=name;
    
        }
        public    static String getBeanName() {
            return beanName;
        }
    
    }
    //调用System.out.println(Animal.getBeanName());  结果为a
    额外说明:

    Spring 自动调用。并且会在Spring自身完成Bean配置之后,且在调用任何Bean生命周期回调(初始化或者销毁)方法之前就调用这个方法。换言之,在程序中使用BeanFactory.getBean(String beanName)之前,Bean的名字就已经设定好了。所以,程序中可以尽情的使用BeanName而不用担心它没有被初始化。
    当然,Bean之中一定要有个String类型变量来保存BeanName的值,这个是在编写Bean代码时有程序员手工完成的,而不是通过什么特殊的饿配置。

    4.Spring注解方式实现DL

    a.在配置文件中开启注解实现DI

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 开启IOC包扫描 -->
     <context:component-scan base-package="domain"></context:component-scan>
     <!-- 开启注解配置DL -->
      <context:annotation-config></context:annotation-config>
     </beans>

           

                  b. 注解方式注入spring内置支持的类型数据 - 非集合类型

                  spring中可以通过@Value注解来实现spring内置支持的类型的属性的注入。

    package domain;
    
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Person {
    @Value("徐旺骑")
    String name;
    @Value("20")
    int age;
    @Resource
    Animal a;
    public Animal getA() {
        return a;
    }
    public void setA(Animal a) {
        this.a = a;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    
    }

    这种方式可以实现spring内置支持类型的注入,但是这种方式将注入的值写死在了代码中,后续如果希望改变注入的值,必须来修改源代码,此时可以将这些值配置到一个properties配置文件中,再在spring中进行引入。

     

  • 相关阅读:
    Kafka-Docker:使用Docker运行Apache Kafka的步骤
    Apache Kafka Connect
    如何创建Kafka客户端:Avro Producer和Consumer Client
    Kafka排队:Apache Kafka作为消息传递系统
    Kafka Broker | 命令行选项和过程
    Apache Kafka Consumer 消费者集
    Apache Kafka Producer For Beginners
    设置Kafka集群的方法
    Apache Kafka工作流程| Kafka Pub-Sub Messaging
    Linux Ubuntu config source
  • 原文地址:https://www.cnblogs.com/xuwangqi/p/11406404.html
Copyright © 2011-2022 走看看