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();
    }

}

运行结果:

原文地址:https://www.cnblogs.com/zoeyqq/p/6548616.html