Spring —— Spring简单的读取和存储对象Ⅰ

JavaEE传送门

JavaEE

Spring —— 初学 Spring, 理解控制反转

Spring —— Spring 的创建与使用



Spring 简单的读取和存储对象

存储 Bean 对象

配置扫描路径

spring-config.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"
       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>


添加注解存储 Bean 对象

  1. 类注解:@Controller@Service@Repository@Component@Configuration

  2. ⽅法注解:@Bean


@Controller (控制器存储)

使⽤ @Controller 存储 bean :

@Controller
public class ArticleController {
    public String Hello() {
        return "Hello Controller";
    }
}

获取 Bean 对象:

public class App {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        System.out.println("App之后");
        ArticleController articleController = applicationContext.getBean("articleController", ArticleController.class);
        System.out.println(articleController.Hello());
    }
}

img src="C:\Users\gujiu\AppData\Roaming\Typora\typora-user-images\image-20230131150008853.png" alt="image-20230131150008853" style="zoom:80%;" />

获取 Bean 对象:

通常情况下, 通过将类首字母小写的方式来获取 Bean 对象.

特殊情况:

# 类名首字母是小写:

和类名首字母大写是一样的, 都是将类名首字母小写的方式来获取对象. (也就是使用原类名)

# 类名首字母和第二个字母都是大写:

使用原类名, 获取 Bean 对象.

总结:

当使用五大类注解时, 默认情况下获取 Bean 对象, 只需要将类名首字母小写即可, 然而, 当对象首字母和第二个字母都是大写时, 此时需要使用原类名才能正确的获取到 Bean 对象

# 项目中没有目录, 所有的类都写在 java 根目录下, 如何存取 Bean 对象?

spring-config.xml 中添加

<content:component-scan base-package="**"></content:component-scan>
<!-- **表示根目录 -->

# 注意 # 最佳方案: 给项目创建合适的目录


@Service(服务存储)

使⽤ @Service 存储 bean :

@Service
public class UserService {
	public void sayHi(String name) {
		System.out.println("Hi," + name);
	}
}

获取 Bean 对象:

class App {
	public static void main(String[] args) {
 		// 1.得到 spring 上下⽂
 		ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
 		// 2.得到 bean
 		UserService userService = (UserService) context.getBean("userService", UserService.class);
 		// 3.调⽤ bean ⽅法
 		userService.sayHi("gujiu");
 	}
}

@Repository(仓库存储)

使⽤ @Repository 存储 bean :

@Repository
public class UserRepository {
    public String sayHi() {
        return  "hi, Repository";
    }
}

@Component(组件存储)

使⽤ @Component 存储 bean :

@Component
public class UserComponent {
    public String sayHi() {
        return  "hi, component";
    }
}

@Configuration(配置存储)

使⽤ @Configuration 存储 bean :

@Configuration
public class UserConfiguration {
    public String sayHi() {
        return  "hi, Configuration";
    }
}


为什么需要五个类注解呢?

  1. 通过类注解, 可以直接了解当前类的用途.
  2. 功能有细微的不同, 后续文章会详细展开 (Spring MVC/ Spring Boot)

五大类注解的用途(重要)
  • @Controller (控制器):表示的是业务逻辑层, 用来控制用户的行为, 它用来检查用户参数的有效性.
  • @Servie (服务):归属于服务层, 调用持久化类实现响应的功能. (不直接和数据库交互, 它类似于控制中心).
  • @Repository (仓库):归属于持久层, 是直接和数据库进行交互的. 通过每一个表都会对应一个 @Repository.
  • @Configuration (配置):归属于配置层, 是用来配置当前项目的一些信息.
  • @Component: 归属于公共工具类, 提供某些公共方法.

程序的⼯程分层,调⽤流程如下:


五大类注解之间的关系

我们查看 @Controller 注释的源码, 我们发现里面有一个 @Component 注解

我们再查看 @Service / @Repository / @Configuration 注解的源码发现, 里面都有一个 @Component 注解

小结: @Component 是其他四个类的父类


方法注解 @Bean

使用 @Bean 方法注解, 将返回的对象存储到 Spring 中

获取 Bean 对象:

ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("spring-config.xml");
Student student = applicationContext.getBean("student", Student.class);
System.out.println(student.toString());

# 注意事项 #

  • @Bean 注解一定要配合五大类使用, 否则是无效的.

  • @Bean 默认情况下, Bean name = 方法名.

  • 当给 @Bean 设置了 name 属性之后, 使用原方法名就不能获取到对象了, 只能使用设置的名称才能获取.


🌷(( ◞•̀д•́)◞⚔◟(•̀д•́◟ ))🌷

以上就是今天要讲的内容了,希望对大家有所帮助,如果有问题欢迎评论指出,会积极改正!!

在这里插入图片描述
在这里插入图片描述

这里是Gujiu吖!!感谢你看到这里🌬
祝今天的你也
开心满怀,笑容常在。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值