spring 註解實例

先不說網上的那些例子了,百度到的都是一些零碎的東西。我之因此記博客,除了總結以外,很大一個緣由是對網上的某些東西真的很無語。java

拿註解來講,什麼入門實例的東西,說是入門,卻連一個基本的hello world 都沒有,呵呵。spring

以前一直都是用xml配置,註解如今用的也多了,要好好看看。app

本篇裏面都是基礎,代碼清單都會列全。maven

首先是引入spring包,這裏用的是maven,pom.xml加入:測試

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

而後maven install,引入包。this

 

接着,xml的配置文件,這裏包括頭文件,以及註解須要的配置:spa

beans.xmlcode

<?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>

好了,從如今開始。component

代碼結構:xml

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

關於接口的,要在實現類上面添加註解說明。坑爹的,網上有篇文章說是要在接口上添加註解,不能在實現類上面,致使錯誤了半天。

關於註解的各個標籤,能夠單獨百度一下,不少講解。

相關文章
相關標籤/搜索