0
点赞
收藏
分享

微信扫一扫

Spring(存储Bean对象五大类注解,获取Bean对象三种注入方式)

独孤凌雪 2022-12-04 阅读 217

目录

1. 先进行配置扫描路径

2. 存储 Bean 对象 ( 添加注解存储 Bean 对象)

2.1 @Controller [控制器存储]

2.2 @Service [服务存储]

2.3 @Repository [仓库存储]

2.4 @Configuration [配置存储]

2.5 @Component [组件存储]

2.6 五大类注解的作用和关系

2.7 Bean的命名规则

2.8 使用方法注解 @Bean

2.8.1 重命名 Bean

3. 获取 Bean 对象 (对象装配) 

3.1 属性注入

3.2 构造方法注入

3.3 Setter注入

3.4 面试题: 三种注入优缺点分析

3.5 @Resource:另一种注入关键字

面试题: @Autowired 和 @Resource 的区别

3.6 @Bean 将一个类型的对象注入多次的问题:


在上一篇中,可以看到,创建Spring需要三步,存 bean 需要三步, 取 bean 需要三步,感觉这个读取和存储对象的步骤并没有想象的那么 "简单" 啊

所以本篇将学习通过 "使用注解" 来更简单的存储和读取对象


前面在存储 Bean 时,需要在 spring-config 中添加一行 bean 注册内容,这个XML文件有一点不好的是调试,而且XML文件报错了是不影响项目运行的,即使报错了,也可能发现不了.

1. 先进行配置扫描路径

<?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:content="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 https://www.springframework.org/schema/context/spring-context.xsd">
    <content:component-scan base-package="com.bit.service"></content:component-scan>
</beans>

在 spring 配置文件中设置 bean 的扫描根路径


2. 存储 Bean 对象 ( 添加注解存储 Bean 对象)

使用 5 大类注解实现,就不用在spring-config 中添加一行 bean 注册内容了

2.1 @Controller [控制器存储]

package com.beans;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    public void sayHi() {
        System.out.println("你好,UserController!");
    }
}
import com.beans.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        //1. 先得到上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        //2. 得到 bean
        UserController controller = context.getBean("userController",UserController.class);

        //3. 使用 bean
        controller.sayHi();
    }
}

2.2 @Service [服务存储]

import com.beans.UserController;
import com.beans.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserService service = context.getBean("userService",UserService.class);
        service.sayHi();
    }
}
package com.beans;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void sayHi() {
        System.out.println("你好,Service!");
    }
}

2.3 @Repository [仓库存储]

import com.beans.UserRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserRepository repository = context.getBean("userRepository",UserRepository.class);
        repository.sayHi();

    }
}
package com.beans;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
    public void sayHi() {
        System.out.println("你好,Repository!");
    }
}

2.4 @Configuration [配置存储]

package com.beans;

import org.springframework.context.annotation.Configuration;

@Configuration
public class UserConfiguration {
    public void sayHi() {
        System.out.println("你好,Configuration!");
    }
}
import com.beans.UserConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        //1. 先得到上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");        UserConfiguration configuration = context.getBean("userConfiguration",UserConfiguration.class);
        configuration.sayHi();
    }
}

2.5 @Component [组件存储]

package com.beans;

import org.springframework.stereotype.Component;

@Component
public class UserComponent {
    public void sayHi() {
        System.out.println("你好,Component!");
    }
}
import com.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserComponent component = context.getBean("userComponent",UserComponent.class);
        component.sayHi();
    }
}

2.6 五大类注解的作用和关系

(1) 作用:

程序的最基础⼯程分层,调⽤流程如下

 (2) 关系:

下面看一下源码

2.7 Bean的命名规则

在前按照规范"大驼峰"取类名,然后getBean按照规范"小驼峰"是可以运行成功的

 而写如果写一个这样的类

 但是如果,这样写就可以运行成功

 那么 Bean 的命名规则到底应该是什么样的


2.8 使用方法注解 @Bean

package com.beans;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class UserBeans {
   @Bean  //[只使用一个 @Bean 是无法将对象存储到容器中]
    public User user1() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user;
    }
}
import com.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        //1. 先得到上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        User user = context.getBean("user1",User.class);
//        User user = context.getBean(User.class);

        System.out.println(user);

    }
}
package com.beans;

public class User {
    private int id;
    private String name;

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2.8.1 重命名 Bean

package com.beans;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class UserBeans {
   @Bean(name = "userinfo")  //[只使用一个 @Bean 是无法将对象存储到容器中]
    public User user1() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user;
    }
}


3. 获取 Bean 对象 (对象装配) 

3.1 属性注入

package com.beans;

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

@Controller
public class UserController2 {
    // 对象注入1: 属性注入
    @Autowired
    private UserService userService;

    public void sayHi() {
        userService.sayHi();
    }
}
import com.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        //1. 先得到上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserController2 controller2 = context.getBean(UserController2.class);
        controller2.sayHi();
    }
}

3.2 构造方法注入

package com.beans;

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

@Controller
public class UserController2 {

    //使用构造方法实现 bean 注入(官方推荐写法)
    private UserService userService;

    @Autowired
    public UserController2(UserService userService) {
//        userService = new UserService(); //传统的写法
        this.userService = userService;
    }

    public void saHi() {
        userService.sayHi();
    }
}

3.3 Setter注入

package com.beans;

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

@Controller
public class UserController2 {
    //3. 使用 Setter 注入
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void sayHi() {
        userService.sayHi();
    }
}

3.4 面试题: 三种注入优缺点分析


3.5 @Resource:另一种注入关键字

面试题: @Autowired 和 @Resource 的区别


3.6 @Bean 将一个类型的对象注入多次的问题:

 

 解决方法

package com.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    @Autowired //[属性注入/字段注入]
    private User user2;

    public void sayHi() {
        System.out.println("User -> " + user2);
    }
}
import com.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        //1. 先得到上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserController controller = context.getBean(UserController.class);
        controller.sayHi();
    }
}
package com.beans;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class UserBeans {
   @Bean(name = "userinfo")  //[只使用一个 @Bean 是无法将对象存储到容器中]
    public User user1() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user;
    }

    @Bean
    public User user2() {
        User user = new User();
        user.setId(2);
        user.setName("李四");
        return user;
    }
}

 


举报

相关推荐

0 条评论