Java-spring注解的作用

**1.@Qualifier:**通常与@Autowired搭配使用,通过指定具体的beanName来注入相应的bean

当容器中有多个类型相同的Bean时,可以使用@Qualifier注解来指定需要注入的Bean。@Qualifier注解可以用于字段、方法参数、构造函数参数等位置

java 复制代码
@Service
public class UserService {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserMapper userMapper ;

    public User getUserById(Int id) {
        return userRepository.findById(id).orElse(null);
    }

    // Other business methods...
}

上面的示例中,@Qualifier注解用于指定需要注入的Bean的名称为"userServiceImpl"。需要注意的是,@Qualifier注解通常与@Autowired或@Inject注解一起使用,用于在多个匹配的Bean中进行选择。

如果没有指定@Qualifier注解,Spring框架将使用默认的自动装配策略,即按照类型进行匹配。如果有多个类型相同的Bean,Spring框架将抛出异常。

因此,使用@Qualifier注解可以有效地解决这个问题。

2.@Bean是一个注解,用于告诉 Spring 框架将标注的方法返回的对象注册为一个 Bean(组件)

具体的作用:

①.使用 @Bean 注解可以将方法返回的对象注册为一个 Bean,并且该 Bean 会被 Spring 容器管理

②.依赖注入:当其他组件需要使用这个 Bean 时,Spring 框架会自动将该 Bean 注入到相应的位置,实现依赖注入

③.自定义组件配置:通过 @Bean 注解可以对 Bean 进行自定义配置,例如设置属性值、初始化方法、销毁方法等

④.替代 XML 配置:需要通过 XML 配置文件来定义和配置 Bean,而现在通过 @Bean 注解可以在 Java 代码中实现同样的功能,避免了繁琐的 XML 配置

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cloud.bus.auto.mapper.IUserMapper">
  <resultMap id="BaseResultMap" type="com.cloud.bus.common.entity.TUser">
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="ACCT_SCOPE" jdbcType="VARCHAR" property="acctScope" />
    <result column="CONFIG_STATUS" jdbcType="VARCHAR" property="configStatus" />
    <result column="IS_MERGE" jdbcType="VARCHAR" property="isMerge" />
    <result column="LAST_ACCT_DATE" jdbcType="DATE" property="lastAcctDate" />
    <result column="HAPPENED_YEAR" jdbcType="VARCHAR" property="happenedYear" />
  </resultMap>
  <sql id="Base_Column_List">
    ID, HOSPITAL_ID, ACCT_SCOPE, CONFIG_STATUS, IS_MERGE, SCHEDULE_ACCT_DATE,
    LAST_ACCT_DATE,LAST_OPERATE_DATE,HAPPENED_YEAR
  </sql>

  <update id="updateUser">
    update T_User
    <set>
      <if test="hospitalId != null and hospitalId != ''">
        HOSPITAL_ID = #{hospitalId,jdbcType=VARCHAR},
      </if>
     //.......
       <if test="runAcctDate != null and runAcctDate != ''">
        RUN_ACCT_DATE = #{runAcctDate,jdbcType=VARCHAR},
      </if>
    </set>
    where ID = #{id,jdbcType=VARCHAR}
  </update>
  <select id="queryUserList" resultType="com.cloud.bus.common.vo.UserVO">
      select
      <include refid="Base_Column_List"/>
      from T_User
      <include refid="Example_Where_Clause"/>
  </select>
  <sql id="Example_Where_Clause">
    <trim prefix="where" prefixOverrides="and|or">
      <if test="hospitalId != null and hospitalId != ''">
        and HOSPITAL_ID = #{hospitalId,jdbcType=VARCHAR}
      </if>
      <if test="operatorType != null and operatorType != ''">
        and OPERATOR_TYPE = #{operatorType,jdbcType=VARCHAR}
      </if>
      <if test="acctScope != null and acctScope != ''">
        and ACCT_SCOPE = #{acctScope,jdbcType=VARCHAR}
      </if>     
    </trim>
  </sql>
</mapper>

3.@Configuration 注解的主要作用是指示Spring容器,该类是一个配置类,它包含了一个或多个Spring容器所需的bean定义。

这意味着@Configuration 注解用于定义Spring应用程序的配置信息,通常在Java类中进行配置而不是在XML文件中

@Configuration 注解的作用包括

①.定义Bean :在@Configuration 注解的类中,您可以使用@Bean 注解来定义一个或多个bean。这些bean将被Spring容器托管,可以在应用程序中使用

②.组织配置@Configuration 注解使您能够将应用程序的配置集中到一个或多个Java类中,以便更好地组织和管理配置信息。这有助于提高代码的可维护性和可读性

③.替代XML配置@Configuration 注解可以用来替代传统的XML配置文件。通过使用Java类来配置应用程序,您可以获得更强的类型安全性,并且可以利用Java的特性,如条件化配置、注解等

④.支持自动装配:@Configuration 注解通常与@Autowired 注解一起使用,以实现自动装配(依赖注入)。当一个配置类被Spring容器扫描到时,其中的@Bean 方法将被调用,创建bean实例,并自动装配它们的依赖关系。

⑤.集成外部配置:通过@Configuration 注解,您可以将外部配置(如属性文件或环境变量)与应用程序的配置进行集成。Spring提供了@PropertySource 注解来加载外部属性文件,以及@Value 注解来注入属性值

复制代码
@Configuration
public class ApplicationConfig {
    
    @Bean
    public UserService userService() {
        return new UserServiceImpl();
    } 
}
相关推荐
96771 小时前
理解IOC控制反转和spring容器,@Autowired的参数的作用
java·sql·spring
SY_FC1 小时前
实现一个父组件引入了子组件,跳转到其他页面,其他页面返回回来重新加载子组件函数
java·前端·javascript
啵啵鱼爱吃小猫咪1 小时前
机械臂阻抗控制github项目-mujoco仿真
开发语言·人工智能·python·机器人
似水明俊德1 小时前
02-C#
开发语言·c#
耀耀_很无聊1 小时前
09_Jenkins安装JDK环境
java·运维·jenkins
ノBye~1 小时前
Centos7.6 Docker安装redis(带密码 + 持久化)
java·redis·docker
黑臂麒麟1 小时前
openYuanrong:多语言运行时独立部署以库集成简化 Serverless 架构 & 拓扑感知调度:提升函数运行时性能
java·架构·serverless·openyuanrong
oem1101 小时前
C++中的享元模式实战
开发语言·c++·算法
XiaoLeisj1 小时前
Android Jetpack 页面架构实战:从 LiveData、ViewModel 到 DataBinding 的生命周期管理与数据绑定
android·java·架构·android jetpack·livedata·viewmodel·databinding
⑩-1 小时前
为什么要用消息队列?使用场景?
java·rabbitmq