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

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

關於注解的各個標簽,可以單獨百度一下,很多講解。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM