spring注解装配bean简单例子

由于刚开始学习注解的使用,所以就不介绍更多的注解了,这次演示只使用Controller与Resource注解。分别表示注册为bean和装配bean。Controller可以添加bean的id,不添加则自动修改为第一个字母小写的原名。

  • 首先创建一个接口:
package bean;
public interface Animals {
    public void print();
}
  • 创建两个实现接口的类:
package bean;

import org.springframework.stereotype.Controller;
@Controller   //不指定名字默认注册为头个字母小写,cat,装配的时候发现有多个实现类或则找不到实现的bean,都会报错
public class Cat implements Animals {

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println("i am a Cat");
    }

}
package bean;

import org.springframework.stereotype.Controller;

@Controller//注册为bean
public class Dog implements Animals {

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println("i am a Dog");
    }

}
  • 直接使用接口的类:
package bean;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
@Controller
public class Pet{
    @Resource(name = "cat")//使用cat注入
    Animals animal;
    public void printType()
    {
        animal.print();
    }
}
  • 注解完了,注解只是元数据不会起什么功能性的作用,应该还要有处理注解的处理器,还有要标注要扫描注解的注册为bean的包,即配置文件spring.xml如下:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    
    <context:annotation-config />
    
    <context:component-scan base-package="bean" />
    
 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />


beans>
  • 测试类:
package bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String args[])
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        //获得Cat的bean
        Animals a1 = (Cat)context.getBean("cat");
        a1.print();
        //获得Dog的bean
        a1 = (Dog)context.getBean("dog");
        a1.print();
        //pet中有依赖注入
        Pet p = (Pet)context.getBean("pet");
        p.printType();

    }
}

运行结果:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
i am a Cat
i am a Dog
i am a Cat

以上可以初步理解注释的工作原理,还有很多注释需要学习。
参考文章

你可能感兴趣的:(企业应用,框架学习)