Spring基于注解的零配置支持

一、搜索Bean类
为了不再使用Spring的配置文件来配置任何bean实例,Spring自动搜索某些路径下的Java类,并将这些java类注册成Bean实例
Spring提供了如下几个Annotation来标注Spring Bean,目的让Spring知道应该把哪些Java类当成Bean类处理。

  1. @Conponent:标注一个普通的Spring Bean 类
  2. @Controller:标注一个控制器组件类
  3. @Service:标注一个业务逻辑组件类
  4. @Repository:标注一个Dao组件类
    二、使用实例
package com.home.bean;
/**
 * 定义一个接口
 */
public interface Axe {
   public String chop();
}

package com.home.bean;
import org.springframework.stereotype.Component;
@Component
public class SteelAxe implements Axe{

    public String chop() {
        // TODO Auto-generated method stub
        return "钢斧砍柴真的很快";
    }
}

package com.home.bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//指定该bean实例的作用域为prototype,不指定默认为singleton
@Scope("prototype")
//指定该类作为spring Bean,Bean实例名为axe
@Component("stoneAxe")
public class StoneAxe implements Axe{
    public String chop() {
        // TODO Auto-generated method stub
        return "石斧砍柴真的很慢";
    }
}

配置文件:
    <context:annotation-config/>
    <!-- 自动扫描指定包及其子包下的所有bean类 -->
    <context:component-scan base-package="com.home.bean">
    <!-- 只是以Axe结尾的类当成Spring容器中的bean -->
    <!-- type:指定过滤器类型
         1.annotation:Annotation过滤器,该过滤器需要指定一个Annotation名,如lee.AnnotationTest
         2.assignable:类过滤器,该过滤器直接指定一个java类
         3.regex,正则表达式过滤器,指定过滤规则,使用如下
         4.aspectJ:AspectJ过滤器,如org.example.*Service+
     -->
    <context:include-filter type="regex" expression=".*Axe"/>
    </context:component-scan>
测试类:
package spring;
import java.util.Arrays;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanTest {
    public static void main(String[] args) {
        //创建spring容器
         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
         System.out.println("Bean实例名称:"+Arrays.toString(ctx.getBeanDefinitionNames()));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring中,我们可以使用注解配置和装配Bean,这可以使我们的代码更加简洁和易于维护。下面是关于如何基于注解配置和装配Bean的一些简要介绍: 1. 基于注解配置Bean 在Spring中,我们可以使用以下注解配置Bean: - @Component:表示该类是一个Spring Bean,需要被Spring容器管理。 - @Service:表示该类是一个服务层的Bean。 - @Controller:表示该类是一个控制层的Bean。 - @Repository:表示该类是一个数据访问层的Bean。 这些注解都是基于@Component注解的衍生注解,它们的作用是更加明确地表示Bean的角色。我们可以在Bean类上添加这些注解,告诉Spring容器该类需要被管理。例如: ``` @Service public class UserService { // ... } ``` 2. 基于注解装配Bean 在Spring中,我们可以使用以下注解来装配Bean: - @Autowired:自动装配Bean。 - @Qualifier:指定具体的Bean名称进行装配。 - @Resource:指定具体的Bean名称进行装配,与@Qualifier类似。 - @Value:注入一个具体的值。 使用@Autowired注解进行自动装配时,Spring会自动在容器中寻找与该类型匹配的Bean,并将其注入到类的属性中。例如: ``` @Service public class UserService { @Autowired private UserDao userDao; // ... } ``` 使用@Qualifier或@Resource注解可以指定具体的Bean名称进行装配。例如: ``` @Service public class UserService { @Autowired @Qualifier("userDaoImpl") private UserDao userDao; // ... } ``` 使用@Value注解可以注入一个具体的值。例如: ``` @Service public class UserService { @Value("10") private int maxCount; // ... } ``` 以上就是关于Spring中基于注解配置和装配Bean的简要介绍,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为你写诗_xue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值