zoukankan      html  css  js  c++  java
  • 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

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

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

  • 相关阅读:
    一些业内有名的网站收集
    WCF重载
    FCKEditor fckconfig.js配置,添加字体和大小 附:中文字体乱码问题解决
    查询第几条到第几条的数据的SQL语句
    SPOJ 9939 Eliminate the Conflict
    UVA 10534 Wavio Sequence
    HDU 3474 Necklace
    POJ 2823 Sliding Window
    UVA 437 The Tower of Babylon
    UVA 825 Walking on the Safe Side
  • 原文地址:https://www.cnblogs.com/juepei/p/3873039.html
Copyright © 2011-2022 走看看