2021-09-14 spring基础二

本文介绍了Spring框架中的注解配置,包括@Component、@Controller、@Service、@Repository以及@Autowired和@Resource的使用。通过注解模式,可以在不使用XML配置的情况下实现对象的自动装配。同时,展示了如何通过<context:component-scan>开启组件扫描,自动创建Bean对象。在测试类中,展示了如何通过ApplicationContext获取Bean并进行调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,分别创建Bean1,Bean2,Bean3类

package com.entor.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Component 注解作用在类上,作用:相当于配置文件中一个bean标签,默认的id属性名称是该类名的首字母小写格式,也可以指定名称
 * @Controller
 * @Service
 * @Repository
 * 以上四个注解都是用在类上面,作用类似,主要区别是用来标识不同类型的对象,
 * @Controller 注解用来标识处理客户端请求类
 * @Service 注解用来标识业务处理类
 * @Repository 注解用来标识数据访问层处理类
 * @Component 注解用来标识除了上面三个标识以外其他组件
 */
@Component("bean1")
public class Bean1 {
    /**
     * @Resource 注解用在属性上面,作用:根据属性名称去配置文件中获取对应id名称的bean对象,找到之后赋值给该属性,
     * 如果没有找到,则根据属性类型去配置文件查找,如果找到多个则报错,使用注解后可以省略属性的get/set方法
     *
     * @Autowired 注解用在属性上面,作用:根据属性类型去配置文件查找对象,如果找到多个,再根据属性名称去查找,如果找不到则报错
     * 可以配合@Qualifier注解使用,指定名称去查找,如果找不到则报错,不会再根据属性名称去查找
     */
    @Autowired
    private Bean2 bean2;
   @Resource
    private Bean3 bean3;

    @Override
    public String toString() {
        return "Bean1{" +
                "bean2=" + bean2 +
                ", bean3=" + bean3 +
                '}';
    }
}
package com.entor.entity;

import org.springframework.stereotype.Controller;

@Controller
public class Bean2 {

    private int id;
    private String name;
    private Bean3 bean3;

    @Override
    public String toString() {
        return "Bean2{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", bean3=" + bean3 +
                '}';
    }
}
package com.entor.entity;

import org.springframework.stereotype.Controller;

@Controller
public class Bean3 {
    private int id;
    private String name;
    private String password;

    @Override
    public String toString() {
        return "Bean3{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

2,使用注解模式

三种属性注解:

@Autowired  :根据属性名称去配置文件中获取对应的id名称的bean对象,找到后赋值给该属性
如果没有找到,则根据属性类型class去配置文件查找,如果找到多个则报错,使用注解后可以省略属性的get/set方法
@Resource
 注解用在属性上面,作用:根据属性类型去配置文件查找对象,如果找到多个,再根据属性名称去查找,如果找不到则报错
@Qualifier:注解使用,指定名称去查找,如果找不到则报错,不会再根据属性名称去查找

 3.2,4种类注解,作用:相当于一个bean标签,默认id属性名称是该类名的首字母小写格式

 @Controller 注解用来标识处理客户端请求类
 @Service 注解用来标识业务处理类
 @Repository 注解用来标识数据访问层处理类
 @Component 注解用来标识除了上面三个标识以外其他组件

 4,allcationContext资源文件

<?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"

       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd

		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

  <!--  &lt;!&ndash;如果使用xml配置方法,需要写set,get方法才可使用&ndash;&gt;
    <bean id="bean1" class="com.entor.entity.Bean1">
        <property name="bean2" ref="bean2"/>
        <property name="bean3" ref="bean3"/>
    </bean>
    <bean id="bean2" class="com.entor.entity.Bean2">
        <property name="id" value="1"/>
        <property name="name" value="张三"/>
        <property name="bean3" ref="bean3"/>
    </bean>
    <bean id="bean3" class="com.entor.entity.Bean3">
        <property name="id" value="2"/>
        <property name="name" value="李四"/>
        <property name="age" value="20"/>
    </bean>-->

    <!--注解模式-->
    <!--开启注解模式-->
    <context:annotation-config/>

    <!--配置组件包扫描指定基本的包名称,自动扫描该包以及子包下的所有类,
    查找类上有注解@Component,@Controller,@Service,@Repository自动创建对象-->
    <context:component-scan base-package="com.entor.entity"/>
</beans>

5,测试类

package com.entor.text;

import com.entor.entity.Bean1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {
    public static void main(String[] args) {
        //加载spring配置文件
        ApplicationContext context =  new ClassPathXmlApplicationContext("/applicationContext-bean.xml");
        Bean1 bean1 = context.getBean(Bean1.class);
        System.out.println(bean1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值