【Java】Spring Framework 核心笔记

参考了 Spring Framework - Core,方便 Java 开发者快速入门 Spring Framework

前置知识:IoC, DI, AOP, Java EE (Jakarta EE)

IoC container and Bean

基于 IoC 和 DI 的思想,springframework 为你提供了 ApplicationContext (IoC container) 来产生和管理你需要的 Bean (Java Object),简单来说你不需要使用 new 一个 Object

MyObject myobject = new MyObject();

只需要从 IoC container 那儿获取

MyObject myobject = IoCcontainer.get(MyObject.class);

XML configuration

当然,springframework 不会让你这么写,它会让你写配置文件(configuration file),一般为 .xml 文件,ApplicationContext 会读取这些配置来产生 Bean。
在这里插入图片描述

比如下面这个配置文件

<?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
		https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="..." class="...">  
		<!-- collaborators and configuration for this bean go here -->
	</bean>

	<bean id="..." class="...">
		<!-- collaborators and configuration for this bean go here -->
	</bean>

	<!-- more bean definitions go here -->

</beans>

在这个文件中

	<bean id="..." class="...">  
		<!-- collaborators and configuration for this bean go here -->
	</bean>

称之为 Bean 定义(Bean Definition)

这样 ApplicationContext 就可以读配置

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

当然,如果你想知道配置文件开头的一大段内容,建议去看 XML 规范和 Spring 文档。

Bean

Bean 在 ApplicationContext 中也有各种各样的 property。

PropertyExplained in…
ClassInstantiating Beans
NameNaming Beans
ScopeBean Scopes
Constructor argumentsDependency Injection
PropertiesDependency Injection
Autowiring modeAutowiring Collaborators
Lazy initialization modeLazy-initialized Beans
Initialization methodInitialization Callbacks
Destruction methodDestruction Callbacks

当然你想用这些 property,你得在 XML 中填写对应的 attribute

XML 中的 attribute
在这里插入图片描述

如果你要用一个 Bean,那么 IDClass 必须写。

Dependency Injection

假如你有一个 MessageProcessor 想用(依赖)MessageService,那么你就需要使用 DI,他有两种方式,Constructor arguments (constructor-arg) 或者 Properties (property),前者是 Constructor-based DI,后者是 setter-based DI,看你喜欢用哪个,我推荐 Constructor arguments,下面举例:

public class MessageProcessor {
    private MessageService messageService;

    public MessageProcessor(MessageService messageService) {
        this.messageService = messageService;
    }

    public void processMessage(String message) {
        messageService.sendMessage(message);
    }
}

在配置文件中

<bean id="emailService" class="com.example.EmailService"></bean>
<bean id="messageProcessor" class="com.example.MessageProcessor">
    <constructor-arg ref="emailService" />
</bean>

Properties 就不多说了,自己看官方文档。

Property: Autowiring mode

Bean 有个 property 叫做 Autowiring mode,它可以设置 DI 的模式,一共有三种模式 nobyNamebyType,具体看官方文档。

Annotation-based Container Configuration

当然,用 XML 配置依赖属实有点难受,写的我头昏,spring framework 就提供了一个更好的配置方法,基于 Java annotation。

当然,你要用这个东西,你得更改你的 XML 文件来开启 annotation 配置,根据下面所写的内容,如果你的 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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config/>

</beans>

现在我们进行 DI 就不用写 XML 啦,直接在 constructor 上写 @Autowired annotation,舒服麻了。

public class MessageProcessor {
    private MessageService messageService;
    
	@Autowired
    public MessageProcessor(MessageService messageService) {
        this.messageService = messageService;
    }
	
    public void processMessage(String message) {
        messageService.sendMessage(message);
    }
}

配置文件中:

<bean id="emailService" class="com.example.EmailService"></bean>
<bean id="messageProcessor" class="com.example.MessageProcessor"></bean>

The @Autowired, @Inject, @Value, and @Resource annotations are handled by Spring BeanPostProcessor implementations.

@Autowired and @Resource

Standard Annotation

@Autowired 由 Spring annotation 提供,DI 模式默认 byType

@Resource 由 Jakarta annotation 提供,DI 模式默认为 byName

Classpath Scanning and Managed Components

如果你要用一个 Bean 需要在 XML 中定义,但是现在 spring framework 根据 MVC 结构还提供了下面的 annotation

  • @Component
  • @Service
  • @Controller
  • @Repository

这些 annotation 就能直接定义 Bean,太裤辣。

@Service
public class MessageProcessor {
    private MessageService messageService;
    
	@Autowired
    public MessageProcessor(MessageService messageService) {
        this.messageService = messageService;
    }
	
    public void processMessage(String message) {
        messageService.sendMessage(message);
    }
}

XML 配置可以直接替换成以下内容,注意 org.example 换成自己的!

<?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
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="org.example"/>

</beans>

The use of <context:component-scan> implicitly enables the functionality of <context:annotation-config>. There is usually no need to include the <context:annotation-config> element when using <context:component-scan>.

End

spring framework core 到这里就结束了,其他功能看看文档就行了。

如果想继续去学 Web,可以去看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值