Spring——使用注解配置spring

小知识,大挑战!本文正在参与「程序员必备小知识」创作活动

本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。

  • 使用注解开发,必须要保证导入了aop的依赖

在这里插入图片描述

  • ==使用注解需要导入context约束,增加注解的支持==

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--扫描指定的包,这个包下的注解就会生效-->
        <context:component-scan base-package="com.cheng"/>
        <!--开启注解的支持-->
        <context:annotation-config/>
        
    
    </beans>
    复制代码

bean的注入

@Component

可以使用此注解描述 Spring 中的 Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可。

//@Component组件 等价于<bean id="user" class="com.cheng.pojo.User"/>
@Component
public class User {
    public String name = "万里";
}
复制代码

@Component的衍生注解:

  • @Repository 用于将数据访问层(DAO层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

    @Repository
    public class UserDao {
    }
    复制代码
  • @Service 通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

    @Service
    public class UserService {
    }
    复制代码
  • @Controller 通常作用在控制层,用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

    @Controller
    public class UserController {
    }
    复制代码

这三个注解功能和@Component 都相同,都是用来装配bean,但要对应mvc三层架构来使用。

普通属性的注入

//相当于 <property name="name" value="万里"/>
@Value("万里")
public String name;
   
//@Value也可以放在set方法上
@Value("万里")
public void setName(String name) {
   this.name = name;
复制代码

作用域注解

//将bean的作用域设置为单例模式
@Scope("singleton")
public class User {
    public String name = "万里";
}
复制代码

xml与注解:

  • xml更加万能,适用于任何场合,维护简单方便
  • 注解只能在当前类使用并生效,维护相对复杂

xml与注解的最佳实践:

  • xml用来管理bean
  • 注解只负责完成属性的注入

使用Java的方式配置Spring

Spring JavaConfig 是 Spring 社区的产品,它提供了配置 Spring IoC 容器的纯 Java 方法。因此它有助于避免使用 XML 配置。在spring4之后,它成为了一个核心功能。

实体类

package com.cheng.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value("万里")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}
复制代码

配置类

//@Configuration 代表当前类是一个配置类,就和之前的applicationContext.xml一样
@Configuration
@ComponentScan("com.cheng")//扫描指定的包
@Import(ChengConfig2.class)//引入其他配置类
public class ChengConfig {
    @Bean//注册一个bean,相当于一个bean标签 id=方法名  class=方法的返回值
    public User getUser(){
        return new User();
    }
}
复制代码

测试类

    @Test
    public void test(){
        //以注解的方式实例化spring容器
        ApplicationContext context = new AnnotationConfigApplicationContext(ChengConfig.class);
        User user = (User) context.getBean("getUser");
        System.out.println(user.getName());
    }
复制代码

猜你喜欢

转载自juejin.im/post/7019285109140357133