二、IOC注解

本文介绍了Spring框架中的AOP注解(如@Component、@Controller等)的使用,包括如何导入依赖、配置组件扫描、基于注解的对象创建,以及组件扫描的过滤规则。还详细讲解了@Autowired、@Qualifier、@Resource和@Value的自动装配特性。适合全面理解Spring全注解开发的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二、IOC注解

1、注解需要导入aop的jar包
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
2、Spring针对bean管理提供的注解

存在4个创建Bean实例,功能一致,只是对应不同的层

  • @Component
  • @Controller
  • @Service
  • @Repository
3、基于注解的对象创建
  1. 引入依赖spring-aop

  2. 在配置文件中开启组件扫描

    <!--    开启组件扫描-->
    <!--    如果扫描多个包,可以使用逗号隔开-->
        <context:component-scan base-package="day01.domain"></context:component-scan>
    
  3. 创建类,在类上面添加创建对象注解(@Component、@Controller、@Service、@Respository)

    @Component("user") //等价于 <bean id="user" class="...">
    //仅使用 @Compinent bean的id为类名的首字母小写
    public class User {
    }
    
4、组件扫描的注意点
  1. 关于<context:include>

    <!--    use-default-filters="false" 表示不使用默认的filter,自己配置filter-->
    <!--    context:include-filter 表示要扫描那些内容-->
    <!--    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            表示只扫描带有Controller注解的类-->
        <context:component-scan base-package="day01.annotation" 
                                use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
  2. 关于<context:exclude>

    <!--    <context:exclude-filter 表示不扫描包下带有Controller注解的类-->
        <context:component-scan base-package="day01.annotation">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
5、基于注解的属性注入
  1. @Autowired : 根据属性类型进行自动装配

        //不需要添加setter方法了,底层已经完成了
        @Autowired
        private UserDao userDao;
    
  2. @Qualifier : 根据属性名称进行注入,要和@Autowired一起使用

        @Autowired
        @Qualifier(value = "userDaoImpl")
        private UserDao userDao;
    
  3. @Resource : 根据类型注入,也可以根据名称注入@Resource(name="")不是spring中的注解

    	@Resource//或者@Resource(name = "userDaoImpl")
        private UserDao userDao;
    
  4. @Value : 注入普通类型属性

    	@Value("张三")
        private String name;
    
6、全注解开发(多用于springboot)
  • 配置类

    @Configuration //表示这个一个配置类,用来代替xml文件
    @ComponentScan(basePackages = {"day01.annotation"})//表示包扫描
    public class SpringConfig {
    }
    
  • 测试类

        /**
         * 用来测试全注解
         * AnnotationConfigApplicationContext
         */
        @Test
        public void test2(){
            ApplicationContext context = 
                new AnnotationConfigApplicationContext(SpringConfig.class);
            UserService service = context.getBean("userServiceImpl", UserService.class);
            service.service();
        }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值