Spring 之 IOC(三)

基于注解方式

1、什么是注解

  • 注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
  • 注解可以坐拥在类上面,方法上面,属性上面
  • 使用注解的目的:简化 xml 配置

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

  • @Component
  • @Service
  • @Controller
  • @Repository
    这四个注解功能是一样的,都可以用来创建 bean 实例。

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

(1)引入依赖:spring-aop-5.2.6.RELEASE.jar
(2)开启组件扫描

<!--开启组件扫描
        1、如果扫描多个包,多个包使用逗号隔开
        2、扫描包上层目录
    -->
<context:component-scan base-package="at.company"></context:component-scan>

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

package at.company.service;

import org.springframework.stereotype.Service;


// 在注解里面,value 的属性值可以省略补写,默认值是类名称,首字母小写,即UserService --> userService
@Service(value="userService")   // <bean id="userService" class="at.company.service.UserService"/>
public class UserService {
    public void add(){
        System.out.println("service add......");
    }
}

4、开启组件扫描细节配置
<?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">
    

    <!--示例一:
    use-default-filters="false" 表示现在不使用默认的 filter,自己配置 filter
    context:include-filter,设置扫描哪些内容
    扫描 at.company 包下的带有 org.springframework.stereotype.Controller 注解的类
    -->
    <context:component-scan base-package="at.company" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    
    <!--示例二
    context:exclude-filter,设置哪些内容不扫描
    不扫描 at.company 包下的带有 org.springframework.stereotype.Controller 注解的类
    -->
    <context:component-scan base-package="at.company" use-default-filters="false">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    
</beans>
5、基于注解方式实现属性注入

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

  • 创建 Service 和 Dao 类,在 Service 和 Dao 类上添加创建对象注解
  • 在 Service 中注入 Dao 对象,在 Service 类中添加 Dao 类型属性,在属性上面使用注解
    UserDao 接口:
package at.company.dao;

public interface UserDao {
    void add();
}

UserDaoImpl 实现类:

package at.company.dao.impl;

import at.company.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("dao add......");
    }
}

UserService 类:

package at.company.service;

import at.company.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value="userService")   // <bean id="userService" class="at.company.service.UserService"/>
public class UserService {

    // 定义 dao 类型的属性,不需要添加 set 方法
    // 添加注入属性注解
    @Autowired // 根据类型注入
    private UserDao userDao;

    public void add(){
        System.out.println("service add......");
        userDao.add();
    }
}

测试类:

package at.company.dao;

import at.company.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

    @Test
    public void testAdd(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();

    }
}

测试结果:

service add......
dao add......

(2)@Qualifier:根据名称进行注入,与 @Autowired 配合使用
eg:假设 UserDao 接口有多个实现类,而在 UserService 中注入 UserDao 时,Spring 会不确定注入哪一个实现类。而 @Qualifier 可以解决这个问题。在 UserDaoImpl 实现类的注解 @Repository 后面添加 value 属性,即如下:

package at.company.dao.impl;

import at.company.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("dao add......");
    }
}

在 UserService 中的 UserDao 属性上添加 @Qualifier(value = “userDaoImpl1”),从而唯一确定 UserDaoImpl 实现类。

package at.company.service;

import at.company.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


// 在注解里面,value 的属性值可以省略补写,默认值是类名称,首字母小写,即UserService --> userService
@Service(value="userService")   // <bean id="userService" class="at.company.service.UserService"/>
public class UserService {

    // 定义 dao 类型的属性,不需要添加 set 方法
    // 添加注入属性注解
    @Autowired // 根据类型注入
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("service add......");
        userDao.add();
    }
}

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

//    @Resource // 根据类型注入
    @Resource(name = "userDaoImpl1") // 根据名称注入
    private UserDao userDao;

(4)@Value:注入普通类型属性
为 name 属性注入值 abc

@Value(value = "abc")
    private String name;
6、完全注解开发

(1)创建配置类,替代 xml 配置文件
在这里插入图片描述
配置类:

package at.company;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"at.company"})
public class SpringConfig {
}

(2)测试代码:
注意:创建 ApplicationContext 对象时,不再调用 ClassPathXmlApplicationContext 接口,而是改为调用 AnnotationConfigApplicationContext 接口

 @Test
    public void testSpringConfig(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值