• spring注解实例


    转载自博客园博客,本来想按出处转载的,没找到转载按钮,就直接粘贴过来,估计过段时间要用到,先保存了!

    spring 注解实例

     

    先不说网上的那些例子了,百度到的都是一些零碎的东西。我之所以记博客,除了总结之外,很大一个原因是对网上的某些东西真的很无语。

    拿注解来说,什么入门实例的东西,说是入门,却连一个基本的hello world 都没有,呵呵。

    之前一直都是用xml配置,注解现在用的也多了,要好好看看。

    本篇里面都是基础,代码清单都会列全。

    首先是引入spring包,这里用的是maven,pom.xml加入:

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

    然后maven install,引入包。

    接着,xml的配置文件,这里包括头文件,以及注解需要的配置:

    beans.xml

    复制代码
    <?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-3.0.xsd">
            
           <context:annotation-config></context:annotation-config>
           <context:component-scan base-package="com.spring.ioc"></context:component-scan>
    </beans>
    复制代码

    好了,从现在开始。

    代码结构:

    Man包下是第二个例子。

    先说第一个例子,无接口的。

    person.java:

    复制代码
    package com.spring.ioc;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Person {
        private String name;
        private String sex;
        
        
        public Person() {
            name="wang";
            sex="man";
        }
    /*    public Person(String name, String sex) {
            super();
            name="wang";
            sex="man";
        }*/
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
         
    }
    复制代码

    里面初始化了一些数据,作为一个bean。

    depart.java:

    复制代码
    package com.spring.ioc;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Depart {
        
        @Autowired
        private Person person;
        
        public String getDepart(){
            String s=person.getName()+" in depart";
            return s;
        }
    }
    复制代码

    这个是为了演示,在depart里面注入person。

    主类测试用的:

    复制代码
    package com.spring.ioc;
    
    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;
    
    public class Main {
    
        public static void main(String[] args) {
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
            Depart depart=(Depart) applicationContext.getBean("depart");
            System.out.println(depart.getDepart());
        }
    }
    复制代码

    运行后,结果:

    wang in depart

    第二个例子,带有接口的例子:

    创建接口,man:

    package com.spring.ioc.Man;
    
    public interface Man {
        public String say();
    }

    然后有两个实现类:

    复制代码
    package com.spring.ioc.Man;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Chinese implements Man {
    
        public String say() {
            // TODO Auto-generated method stub
            return "你好";
        }
    
    }
    复制代码
    复制代码
    package com.spring.ioc.Man;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class American implements Man {
    
        public String say() {
            // TODO Auto-generated method stub
            return "hello";
        }
    
    }
    复制代码

    然后创建一个类,注入这两个接口实现类。

    复制代码
    package com.spring.ioc.Man;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ManService {
        @Autowired
        @Qualifier(value="chinese")
        private Man man;
        
        public String sayChineseHello(){
            return man.say()+",欢迎";
        }
        @Autowired
        @Qualifier(value="american")
        private Man aman;
        public String sayEnglishHello(){
            return aman.say()+",welcome";
        }
    }
    复制代码

    主类:

    复制代码
    package com.spring.ioc.Man;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
        
        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
            ManService manService=(ManService) context.getBean("manService");
            String string=manService.sayChineseHello();
            System.out.println(string);
            System.out.println(manService.sayEnglishHello());
        }
    }
    复制代码

    运行结果:

    你好,欢迎 hello,welcome

    关于接口的,要在实现类上面添加注解说明。坑爹的,网上有篇文章说是要在接口上添加注解,不能在实现类上面,导致错误了半天。

    关于注解的各个标签,可以单独百度一下,很多讲解。

  • 相关阅读:
    POJ 1236 Network of Schools(强连通分量缩点求根节点和叶子节点的个数)
    文本编辑器vim和gedit
    Ubuntu安装tensorflow
    剑指offer——python【第29题】最小的K个数
    剑指offer——python【第30题】连续子数组的最大和
    剑指offer——python【第37题】数字在排序数组中出现的次数
    剑指offer——python【第28题】数组 中出现次数超过一半的数字
    剑指offer——python【第31题】整数1出现的次数
    剑指offer——python【第54题】字符流中第一个不重复的字符
    剑指offer——python【第40题】数组中只出现一次的数字
  • 原文地址:https://www.cnblogs.com/dftencent/p/3873541.html
Copyright © 2020-2023  润新知