Spring5 IOC容器(注解方式)

目录

 

1、什么是注解 

2、Spring针对Bean管理中创建对象提供注解

3、基于注解方式创建对象

4、基于注解方式注入属性

5、完全注解开发


1、什么是注解 

(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值..)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化xml配置

2、Spring针对Bean管理中创建对象提供注解

(1)@Component

(2)@Service

(3)@Controller

(4)@Repository

说明:上面四个注解功能是一样的,都可以用来创建bean实例,只是我们习惯将不同的注解用到不同的地方,方便区分

3、基于注解方式创建对象

(1)引入依赖 (maven解决)

     将spring-aop-x.x.xRELEASE.jar 添加到项目lib中

(2)开启组件扫描

使用context:component-scan标签的 base-package属性

说明:需要添加context名称空间,上一节演示过。

base-package属性:扫描包

注意:

如果扫描多个包,多个包使用逗号隔开。如果包实在太多了,可以干脆直接扫描包的上层目录

语法:

<context:component-scan base-package="com.atguigu"></context:component-scan>

//这里就是扫描com.atguigu文件夹下的所有包

<?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:p="http://www.springframework.org/schema/p"
             xmlns:util="http://www.springframework.org/schema/util"
             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/util  http://www.springframework.org/schema/util/spring-util.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启组件扫描,这里就会扫描atguigu下面的所有包-->
    <context:component-scan base-package="com.atguigu"></context:component-scan>

扩展:关于开启组件扫描的过滤项

① context:component-scan  标签默认用的filter,会扫描base-package属性值包里面的所有内容,如果包里面东西很多,有的组件不需要扫描,可以用子标签context:exclude-filter,去除掉不想扫描的内容。

context:exclude-filter:选择不扫描的组件,该标签的type属性,代表过滤依据。比如type="annotation" 表示以注解为依据。expression属性,表示具体的注解名称。

<context:component-scan base-package="com.atguigu">

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

//表示扫描com.atguigu下面的所有包的组件,除了注解为Controller的组件不扫描

② context:component-scan  标签默认用的filter,会扫描base-package属性值包里面的所有内容,但如果不喜欢这个filter(不想扫描所有内容,只扫描指定内容),可以自行设置过滤条件。只需要设置该标签的属性use-default-filters="false" ,表示现在不使用默认filter,自己配置filter。

context:include-filter子标签 ,设置扫描哪些内容。该标签的type属性,代表过滤依据。比如type="annotation" 表示以注解为依据。expression属性,表示具体的注解名称。

<context:component-scan base-package="com.atguigu"  use-default-filters="false">

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

//表示扫描com.atguigu下面的注解为Controller的组件,其他都不扫描

(3)创建类,在类上面添加创建对象的注解

创建一个service文件夹,com.atguigu.spring5.service包

package com.atguigu.spring5.service;

@Component(value="userService") //相当于<bean id="userService" class="...">
public class UserService{
  public void add(){
    System.out.println("service add...");
  }
}

说明:在注解里面value属性值有默认值,默认值是类名称,首字母小写 //UserService -- userService。如果不写value属性值,就会用默认值

(4)编写测试类

在testdemo文件夹里面 编写测试类

package com.atguigu.spring5.testdemo;

public class TestSpring5Demo1{
  @Test
  public void testService(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    UserService userService = context.getBean("userService",UserService.class);
    System.out.println(userService);
    userService.add();
  }
}

4、基于注解方式注入属性

(1)@Autowired:根据属性类型进行自动装配

(2)@Qualifier:根据名称进行注入,需要搭配@Autowired

(3)@Resource:可以根据类型注入,也可以根据名称注入

以上三个是注入对象类型

(4)@Value:注入普通属性类型

4.1 @Autowired 注入演示:要求在UserService类中注入dao对象

step1: 创建UserService对象 和 UserDao接口实例(通过注解创建)

在com.atguigu.spring5下面再创建一个文件夹(包com.atguigu.spring5.dao),在下面写一个UserDao接口

package com.atguigu.spring5.dao;

public interface UserDao{
  public void add();
}

在dao下面再写一个该接口的实现类

在该实现类上添加创建对象的注解@Repository,即完成UserDaoImpl类对象创建,由于没写value,默认对象id为userDaoImpl

package com.atguigu.spring5.dao;

@Repository//没写value,id默认为userDaoImpl
public class UserDaoImpl implements UserDao{
  @Override
  public void add(){
    System.out.println("dao add...");
  }
}

同样的,用@Service注解创建好UserService类的对象,用@Service是为了方便区分,别的注解也可以

Step2 向Service注入dao对象,在属性上面使用注解 @Autowired

package com.atguigu.spring5.service;

@Service(value="userService") //相当于<bean id="userService" class="...">
public class UserService{

  //定义dao类型属性
  //不需要添加set方法,spring也通过反射给你写了
  //添加注入属性注解
  @Autowired
  private UserDao userDao;
  
  public void add(){
    System.out.println("service add...");
    userDao.add();
  }
}

step3 编写测试类进行测试

package com.atguigu.spring5.testdemo

public class TestSpring5Demo1{
  @Test
  public void testService(){
    ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
    UserService userService=context.getBean("userService",UserService.class);
    System.out.println(userService);
    userService.add();
  }
}

(2)根据名称进行注解

@Qualifier注解的使用,和上面@Autowired一起使用

Step1:依然是用@Repository创建UserDaoImpl对象,取个名字 userDaoImpl1

package com.atguigu.spring5.dao;

@Repository(value="userDaoImpl1")//为了看得方便,专门取个名字
public class UserDaoImpl implements UserDao{
  @Override
  public void add(){
    System.out.println("dao add...");
  }
}

Step2 用@Service创建UserService对象,同时用@Qualifier和@Autowired 注入属性,@Qualifier可以根据属性名注入,属性名就是上面的userDaoImpl1

package com.atguigu.spring5.service;

@Service(value="userService") //相当于<bean id="userService" class="...">
public class UserService{

  //定义dao类型属性
  //不需要添加set方法,spring也通过反射给你写了
  //添加注入属性注解
  @Autowired
  @Qualifier(value="userDaoImpl1")
  private UserDao userDao;
  
  public void add(){
    System.out.println("service add...");
    userDao.add();
  }
}

4.3 使用@Resource注入属性

@Resource既可以根据类型注入,又可以根据名称注入

//语法

@Resource    根据类型进行注入

@Resource(name = "userDaoImpl1")   根据名称进行注入 

package com.atguigu.spring5.service;

@Service(value="userService") //相当于<bean id="userService" class="...">
public class UserService{
  @Resource//根据类型进行注入
  private UserDao userDao;
  
  public void add(){
    System.out.println("service add...");
    userDao.add();
  }
}

注意:虽然Resource注解更全能,但是该注解时java扩展包中的,并非spring包中的,为了更好的使用spring框架,spring官方建议用前面两个注解。

4.4 @Value属性

  该注解用于注入普通类型属性

语法: @Value(value =值)

@Value(value = "abc")

private String name;

package com.atguigu.spring5.service;

@Service(value="userService") //相当于<bean id="userService" class="...">
public class UserService{
  
  @Value(Value="abc")
  private String name;
  
  @Resource//根据类型进行注入
  private UserDao userDao;
  
  public void add(){
    System.out.println("service add..."+name);
    userDao.add();
  }
}

5、完全注解开发

        前面多少还是写了一点xml文件(开启组件扫描),为了进一步简化开发,spring可以做到纯注解开发

(1)创建配置类,替代xml文件

        由于要替代配置文件,因此需要创建配置类,在com.atguigu.spring5下面再创建一个文件夹config(包com.atguigu.spring5.config),在包下创建SpringConfig类。此时spring并不知道这个类是配置类,要替代xml配置文件,因此需要在该类上面加入@Configuration,告诉spring这是一个配置类。

package com.atguigu.spring5.config;

@Configuration //作为配置类,替代xml配置文件 
@ComponentScan(basePackages = {"com.atguigu"})//数组,所以大括号,扫描com.atguigu文件夹
public class SpringConfig{

}

(2)编写测试文件

        此时已经没有xml配置文件啦。因此用 AnnotationConfigApplicationContext类的构造器AnnotationConfigApplicationContext(配置类.class)

package com.atguigu.spring5.testdemo

public class TestSpring5Demo1{
  @Test
  public void testService2(){
    //加载配置类
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);   
    UserService userService=context.getBean("userService",UserService.class);
    System.out.println(userService);
    userService.add();
  }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值