zoukankan      html  css  js  c++  java
  • Spring context:component-scan代替context:annotation-config

    Spring context:component-scan代替context:annotation-config

    XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        <context:component-scan base-package="com.stono.sprtest" />
    </beans>

    AppBean:

    package com.stono.sprtest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AppBeans6 {
        @SuppressWarnings("resource")
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("appbeans6.xml");
            Singer2 singer2 = (Singer2) context.getBean("singer");
            System.out.println(singer2);
        }
    }

    POJO:

    package com.stono.sprtest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("singer") // 这里可以指定Bean的ID
    public class Singer2 {
        @Autowired
        // @Component标注的类,其Id是camel-casing类名;两个Intrument接口实现类,必须指定其中的一个;
        @Qualifier("saxophone")
        private InstrumentI instrument;
        @Value("justValue")
        private String name;
        @Override
        public String toString() {
            return "Singer2 [instrument=" + instrument + ", name=" + name + "]";
        }
    }
    package com.stono.sprtest;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Saxophone implements InstrumentI {
        private Integer age;
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
    }
    package com.stono.sprtest;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Cymbal implements InstrumentI {
    }
  • 相关阅读:
    基于WS流的RTSP监控,H5低延时,Web无插件,手机,微信ONVIF操控摄像头方案
    H5微信视频,直播低延时,IOS限制全屏播放,自动播放问题处理。
    最新IOS,safari11中对webrtc支持,IOS和android视频聊天,web低延时视频教学技术分析
    MySql 用户篇
    Sql Server 数据库帮助类
    [C#基础知识]转载 private、protected、public和internal的区别
    Mysql 插入语句
    .net core identityserver4 学习日志
    mysql 事务模板
    .net core 生成二维码
  • 原文地址:https://www.cnblogs.com/stono/p/4843716.html
Copyright © 2011-2022 走看看