栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

(4)基于注解的方式管理Bean

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(4)基于注解的方式管理Bean

目录

1 创建Bean

1 使用注解标识组件

2 组件扫描

 2 给Bean的属性注入值——自动装配 

 3 完全注解开发

 4 Spring集成Junit


1 创建Bean

 1 使用注解标识组件

@Component

标识一个受Spring IOC容器管理的组件

@Repository

标识一个受Spring IOC容器管理的持久化层组件

@Service

标识一个受Spring IOC容器管理的业务逻辑层组件

@Controller

标识一个受Spring IOC容器管理的表述层控制器组件

组件命名规则

    默认情况:使用组件的简单类名首字母小写后得到的字符串作为bean的id我们可以使用组件注解的value属性指定bean的id,value属性名可以省略

 注意:事实上Spring并没有能力识别一个组件到底是不是它所标记的类型,即使将   @Respository注解用在一个业务逻辑层组件上面也不会产生任何错误,所以@Respository、@Service、@Controller这几个注解仅仅是为了让开发人员自己明确当前的组件扮演的角色。

2 组件扫描

      组件被上述注解标识后还需要通过Spring进行扫描才能够侦测到。

    指定被扫描的包
    
    
    
        
         

自动装配(注入)的过程:

根据属性的类型实现自动装配。 如果IOC容器中该属性的类型的bean存在多个,则以属性名作为id从IOC容器中寻找以实现自动装配,找到则装配成功;找不到则抛出异常。如果IOC容器中该属性的类型的bean存在多个,并且以属性名作为id从IOC容器中寻找也无法实现自动装配,还可以通过@Qualifier注解指定bean的名称实现自动装配。@Qualifiter注解也可以标注在方法的形参前面。

 3 完全注解开发

@Configuration

  • 添加了该注解的类将是一个配置类,用来代替xml配置文件@ComponentScan
  • 配置要扫描的包
  • package com.spring.annotation.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    @ComponentScan(basePackages = "com.spring.annotation")
    @Configuration//标识当前类是一个配置类,用来代替xml文件
    public class SpringConfigFiguration {
    }

    测试
  • 测试时创建IOC容器的类AnnotationConfigApplicationContext
  • package com.spring.test;
    
    import com.spring.annotation.config.SpringConfigFiguration;
    import com.spring.annotation.service.UserService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    public class noXMLTest {
        
        @Test
        public void testNoXml(){
            //创建IOC容器对象
            ApplicationContext ioc=new AnnotationConfigApplicationContext(SpringConfigFiguration.class);
            //从IOC容器中获取UserService对象
            UserService userService= (UserService) ioc.getBean("userService");
            //调用UserService添加用户信息的方法
            userService.addUser();
        }
    }

    4 Spring集成Junit

          因为Spring提供了对Junit的集成,所以可以在测试类中直接注入IOC容器中的对象,使用此功能需要导入spring-test-5.3.1.jar

    @ContextConfiguration
  • 指定Spring的配置文件的路径

    @RunWith
  • 指定Spring环境下运行Junit4的运行器

  • package com.spring.test;
    
    import com.spring.annotation.service.UserService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @ContextConfiguration(locations = "classpath:beans-annotation.xml")//指定Spring的配置文件的路径
    @RunWith(SpringJUnit4ClassRunner.class)
    public class SpringJunitTest {
        @Autowired
        private UserService userService;
        
        @Test
        public void testUserService(){
            userService.addUser();
        }
    }
    

  • 转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/770295.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号