IOC 操作 Bean 管理(基于注解方式)

121 阅读2分钟

1、什么是注解
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值..)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化 xml 配置
2、Spring 针对 Bean 管理中创建对象提供注解

  • @Component
    • :spring 容器中普通的注解
  • @Service
    • :一般用于Service层
  • @Controller
    • :一般用于web层
  • @Repository
    • :一般用于DAO层上
  • 上面四个注解功能是一样的,都可以用来创建 bean 实例
  • 实际开发中混用也可以。

3、属性注入。

  • @Autowired:根据属性类型进行自动装配
  • @Qualifier:根据名称进行注入 这个@Qualifier 注解的使用,和上面@Autowired 一起使用
  • @Resource:可以根据类型注入,可以根据名称注入
  • @Value:注入普通类型属性.
package com.itheima.annotation.dao;

import org.springframework.stereotype.Repository;

/**
 * @version 1.0
 * @Author kevin
 * @create 2022/7/14
 */

@Repository
public class UserDaoImp1 implements UseDao {
    @Override
    public void add() {
        System.out.println("UserDaoImp1 add--");
    }
}
package com.itheima.annotation.service;
import com.itheima.annotation.dao.UserDaoImp1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @version 1.0
 * @Author kevin
 * @create 2022/7/14
 *  本类用来演示通过注解的方式管理bean
 *  及属性注入;这个@Qualifier 注解的使用,和上面@Autowired 一起使用
 */
 /* 在注解里面 value 属性值可以省略不写,
    默认值是类名称,首字母小写
    UserService -- userService*/
@Service(value = "userService") //<bean id="userService" class=".."/>
public class UserService {
    //注入普通属性 value
    @Value(value = "KevinMiller")
    private String name;
    @Value(value = "13")
    private int age;
    
    //不需要set方法,底层以封装
        @Autowired //根据类型中注入
        @Qualifier(value = "userDaoImp1")//根据名称进行注入
        private  UserDaoImp1 userDaoImp1;

//javax.annotation包中的,  spring推荐使用Autowired+Qualifier使用
//    @Resource(name = "userDaoImp1")
//    private  UserDaoImp1 userDaoImp1;

    @Override
    public String toString() {
        return "UserService{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", userDaoImp1=" + userDaoImp1 +
                '}';
    }
    public void add(){
       System.out.println("UserService add ----");
       userDaoImp1.add();
   }
}
<?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">
<!--本配置文件用来配置 IOC 操作 Bean 管理(基于注解方式实现)-->

<!--    1 ,引入context名称空间-->
<!--    2,开启主件扫描。 及扫描那个包中的那个类存在 注解 -->
    <context:component-scan base-package="com.itheima.annotation"></context:component-scan>

<!--    3,创建类,添加注解-->
    
<!--    4,包扫描的一些细节-->
<!--    use-default-filters="false" 当配置这行就意味着spring 不会按照默认的方式来扫描(全扫)-->
<!--    include-filter type="annotation" 会扫含有Component注解的类-->
<!--    exclude-filter type="annotation" 不会 扫含有Service注解的类-->

<!--    <context:component-scan base-package="com.itheima.annotation" use-default-filters="false">-->
<!--       <context:include-filter type="annotation"-->
<!--                               expression="org.springframework.stereotype.Component"/>-->
<!--        -->
<!--    </context:component-scan>-->
<!--    <context:component-scan base-package="com.itheima.annotation" >-->
<!--        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>-->
<!--    </context:component-scan>-->


</beans>
//该方法用来测试通过注解的方式来管理bean
@Test
public void test1(){
    //加载 配置文件,用来扫描 对应包下的注解。
    ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
    UserService userService = context.getBean("userService", UserService.class);
    System.out.println(userService);
    userService.add();
}
  • 使用纯注解开发则简单许多 UserService 和UserDaoImp类不变创建config类即可
package com.itheima.config;

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

/**
 * @version 1.0
 * @Author kevin
 * @create 2022/7/14
 *
 * 本类使用纯注解开发,编写SpringConfig目的就是替换XML配置文件。
 */
@Configuration//作为配置类,替代 xml 配置文件
@ComponentScan(basePackages = "com.itheima.annotation")//包扫描
public class SpringConfig {
}
//该方法通过加载配置类来纯注解开发
@Test
public void test2(){
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    UserService userService = context.getBean("userService", UserService.class);
    System.out.println(userService);
    userService.add();
}