spring根据名称获取bean_Spring入门学习笔记

95b639971b336cecb3d943e299e3fd39.png

在IT学习的过程中,很多的学习方向大致都是相似的。首先知道是什么,然后知道怎么用,再去知道为什么需要这样用,其次就是根据自己的理解对其进行总结;

学习Spring就是一个很好的例子,Spring框架上手整体是比较简单的,Spring即程序员的春天,那么Spring具体都做了哪些事情,分别是怎么去做到的,一起了解和学习一下;

1、 Spring中Bean的创建方式

2、 Spring中Bean创建时的属性

3、 Spring中Bean注入的方式

首先,我们创建一个Maven工程,然后我们在maven中添加Spring的依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.20.RELEASE</version>
</dependency>

我们在工程中创建一个Customer实体类,在后面的过程中,我们通过这个实体类来进行演示

package com.example.bean;

import lombok.Data;

@Data
public class Customer {
    /**
     * 姓名
     */
    private String userName;
    /**
     * 年龄
     */
    private int age;

    public Customer() {
    }

    public Customer(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }
}

我们先来看一下Spring中创建Bean的方式有哪些;

一:Spring中Bean的创建方式

1.1:Spring中基于XML的方式创建Bean

首先,我们在创建好的工程中创建配置文件mySpring.xml,配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="customer" class="com.example.bean.Customer" >
        <property name="userName" value="张楚岚"></property>
        <property name="age" value="21"></property>
    </bean>

</beans>

然后我们通过ClassPathXmlApplicationContext来读取配置文件进行Bean的创建

public class Main {
    /**
     * 测试xml的方式进行Bean的创建
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("mySpring.xml");
        Customer customer = (Customer) context.getBean("customer");
        System.out.println(customer);
    }

}

输出结果如下: Customer(userName=张楚岚, age=21)

1.2:基于@Configuration和@Bean的形式创建Bean实体类

代码如下:

@Configuration
public class MyConfiguration {

    @Bean
    public Customer myCustomer(){
        return new Customer("冯宝宝", 120);
    }

}

我们创建一个测试类来看一下从Spring容器中获取对象

public class Main {
    /**
     * 测试通过 @Configuration 和 @Bean的形式注入Bean
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
        Customer customer = (Customer) context.getBean("myCustomer");
        System.out.println(customer);
    }

}

输出结果如下:Customer(userName=冯宝宝, age=120)

扩展:Configuration 指定当前类为配置类,其效果类似与xml中的<beans/>

1.3:基于@Configuration 和@Componet、@Service、@Controller、@Repository注解方式

编写代码如下:

package com.example.bean.componentScanh;

import com.example.bean.Customer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
 * 定义一个类,将这个类交给Spring管理
 * @Component @Controller @Service @Repository
 * 使用的效果是一样的,都会在扫描包时,将当前类交给Spring管理
 */
@Component
//@Controller
//@Service
//@Repository
public class MyConf {

    @Bean
    public Customer myCsutomer(){
        return new Customer("无根生", 150);
    }

}

然后我们再创建一个配置类

@Configuration
@ComponentScan(basePackages = "com.example.bean.componentScanh")
public class MyComponentScanh {

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值