Spring+Maven學習實驗- Spring 注解的配置


注解是為 Spring 容器提供 Bean 定義的信息,表現形為把 XML 定義的信息通過類注解描述出來。眾所周知,Spring容器三大要素:Bean 定義、 Bean 實現類以及 Spring 框架。如果采用 XML 配置,Bean 定義和 Bean 實現類本身分離,而采用注解配置,Bean 定義在 Bean 實現類上注解就可以實現。以下簡單列舉幾個注解方式:

1 @Component

被此注解標注的類將被 Spring 容器自動識別,自動生成 Bean 定義。即:

packeage com.shiyanlou.spring;

@Component("shiyanlou")
public class shiyanlou{

}

與在XML中配置以下效果相同

<bean id="shiyanlou" class="com.shiyanlou.spring.shiyanlou">

除此之外,Spring 有三個與 @Component 等效的注解:

  1. @Controller:對應表現層的 Bean,也就是 Action 。
  2. @Service:對應的是業務層 Bean 。
  3. @Repository:對應數據訪問層 Bean 。

2 @Autowired

@Autowired 可以用來裝配 bean,都可以寫在字段上,或者方法上。使用 @Autowired,首先要在在 applicationContext.xml 中加入 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> @Autowired 默認按類型裝配,默認情況下必須要求依賴對象必須存在,如果要允許 null 值,可以設置它的 required 屬性為 false 。例如:

@Autowired() 
@Qualifier("shiyanlouDao")
private ShiyanlouDao shiyanlouDao;

3 Configuration

通過使用注釋 @Configuration 告訴 Spring ,這個 Class 是 Spring 的核心配置文件,並且通過使用注釋 @Bean 定義 bean ,舉例說明:

package com.shiyanlou.spring.java_config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean(name="animal")
    public IAnimal getAnimal(){
        return new Dog();
    }
}

App.java 內容:

package com.shiyanlou.spring.java_config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {

    private static ApplicationContext context;

    public static void main(String[] args) {
        context = new AnnotationConfigApplicationContext(AppConfig.class);
        IAnimal obj = (IAnimal) context.getBean("animal");
        obj.makeSound();

    }

}

在 ApplicationContext.xml 文件中只需要添加:

<bean id="animal" class="com.lei.demo.java_config.Dog">

 

以下為實驗內容:

1.pom.xml配置

2.創建類IAnimal,Dog,AppConfig

package com.shiyanlou.spring.beannote;

public class IAnimal {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void makeSound(){}

}
package com.shiyanlou.spring.beannote;

public class Dog extends IAnimal {
    
    public void makeSound(){
        System.out.println("dog sound");
    }

}
package com.shiyanlou.spring.beannote;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean(name="animal")
    public IAnimal getAnimal(){
        return new Dog();
    }
}

3.創建ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<bean id="animal" class="com.shiyanlou.spring.beannote.Dog">

4.測試類App

package com.shiyanlou.spring.beannote;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    private static ApplicationContext context;

    public static void main(String[] args) {
        context = new AnnotationConfigApplicationContext(AppConfig.class);
        IAnimal obj = (IAnimal) context.getBean("animal");
        obj.makeSound();
    }

}

運行結果:

 


免責聲明!

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



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