Spring3.0中@Import注解的功能

Spring3.0中@Import注解的功能

@Import注解的功能:

  • 多配置类的引入
  • 单个或多个Bean的引入
  • @EnableXXX注解的实质是使用@Import注解向Spring容器中注入其他Bean

使用@Import增强AOP功能详见:Spring3.0中基于注解形式的AOP
多配置类的引入详见:Spring3.0中多配置类引入设置

导入Bean

//DemoService.class是普通类,DemoAspect.class是增强类
@Configuration
@Import({DemoService.class ,DemoAspect.class})
@EnableAspectJAutoProxy//注册增强Aspect类到容器中,自动判断是使用JDK方式还是使用CGLIB方式进行增强
public class Coinfig {
}

获取Bean

public class Demo1  {
    public static void main(String[] args) {
    	//获取容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Coinfig.class);
		//获取Bean
        DemoService demoService = context.getBean("com.example.service.DemoService", DemoService.class);
        //方法调用
        demoService.m1();
    }
}

如果当前容器中只有一个类型的Bean,前面的BeanName属性值可以省略context.getBean("DemoService.class);,否则需要使用全路径的名称进行获取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值