问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Spring框架中基于注解的Bean管理和属性注入详解

创作时间:
作者:
@小白创作中心

Spring框架中基于注解的Bean管理和属性注入详解

引用
CSDN
1.
https://blog.csdn.net/m0_74977981/article/details/144626708

引入

什么是注解

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

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

  • @Component:普通的类
  • @Controller:表现层
  • @Service:业务层
  • @Repository:持久层

上边四个功能一样,都可以用来创建bean实例

用注解的方式创建对象

①:编写接口和实现类

package com.qcby.testanno;
public interface UserService {
    public void hello();
}

②:在需要管理的类上添加@Component注解(上边四个都可以)

package com.qcby.testanno;
import org.springframework.stereotype.Component;
/* <bean id="us" class="UserServiceImpl"/> */
/**
 * 组件,作用:把当前类使用IOC容器进行管理,如果没有指定名称,默认使用类名,首字母是小写。
 * userServiceImpl。或者自己指定名称
 **/
@Controller(value="us")
public class UserServiceImpl implements UserService {
    public void hello() {
        System.out.println("使用注解,方便吧!");
    }
}

③:编写配置文件,重点是开启注解扫描

<?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">
   
    <!--开启注解扫描 com.qcby所有的包中的所有的类-->
    <context:component-scan base-package="com.qcby"/>
</beans>

④编写测试方法

package com.qcby.test;
import com.qcby.testanno.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
    @Test
    public void run1(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContextanno.xml");
        UserService us = (UserService) ac.getBean("us");
        us.hello();
    }
}

用注解的方实现属性注入

  • @Value:用于注入普通类型(String,int,double等类型)--比较麻烦,某些情况下不如直接赋值。
  • @Autowired:默认按类型进行自动装配(引用类型)---掌握这个
  • @Qualifier:不能单独使用必须和@Autowired一起使用,强制使用名称注入---不如直接使用Autowired
  • @Resource:Java提供的注解,也被支持。使用name属性,按名称注入

具体的代码如下:

// 默认当前类名就是ID名称,首字母小写
@Component(value = "c")
// @Controller
// @Service(value = "c")
// @Repository(valu = "c")
public class Car {
    // 注解注入值,属性set方法是可以省略不写的。
    // 只有一个属性,属性的名称是value,value是可以省略不写的
    @Value("大奔2")
    private String cname;
    @Value(value = "400000")
    private Double money;
    // 也不用提供set方法
    // 按类型自动装配的注解,和id名称没有关系
    @Autowired //一定要学会(用于注入对象)
    // 按id的名称注入,Qualifier不能单独使用,需要Autowired一起使用。
    // @Qualifier(value = "person")
    // @Resource Java提供的注解,按名称注入对象,属性名称是name
    // @Resource(name = "person")
    private Person person;
    @Override
    public String toString() {
        return "Car{" +
                "cname='" + cname + '\'' +
                ", money=" + money +
                ", person=" + person +
                '}';
    }
}
@Controller
//@Component(value = "person")
public class Person {
    @Value("张三")
    private String pname;
    @Override
    public String toString() {
        return "Person{" +
                "pname='" + pname + '\'' +
                '}';
    }
}
@Test
public void run1(){
    // 工厂
    ApplicationContext ac = new
            ClassPathXmlApplicationContext("applicationContext.xml");
    // 获取对象
    Car car = (Car) ac.getBean("c");
    System.out.println(car);
}

纯注解的形式实现IOC

纯注解的方式是微服务架构开发的主要方式,所以也是非常的重要。纯注解的目的是替换掉所有的配置文件。但是需要编写配置类。

常用的注解总结

  • @Configuration:声明是配置类
  • @ComponentScan:扫描具体包结构的

编写实体类

@Component
public class Order {
    @Value("北京")
    private String address;
    @Override
    public String toString() {
        return "Order{" +
                "address='" + address + '\'' +
                '}';
    }
}

编写配置类,替换掉applicationContext.xml配置文件

@Configuration
// 扫描指定的包结构
@ComponentScan(value = "com.qcby")
public class SpringConfig {
}

测试方法的编写

package com.qcby.test;
import com.qcby.demo4.Order;
import com.qcby.demo4.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Demo4 {
    @Test
    public void run(){
        // 创建工厂,加载配置类
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
        // 获取到对象
        Order order = (Order) ac.getBean("order");
        System.out.println(order);
    }
}
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号