【springboot学习】注册bean三种方式

装配Bean的三种方式
自动化装配Bean

自动化装配依赖组件扫描@ComponentScan,该注解默认会扫描当前包以及当前包下的所有组件,装配到spring容器中。

可以扫描哪些组件

@Component @Service @Controller

指定扫描的位置

默认会扫描当前包以及这个包下的所有子包。也可以指定扫描的基础包

@ComponentScan("com.bike")或者@ComponentScan(basePackages="com.bike")

为组件扫描的Bean命名

spring应用上下文中所有的bean都会给定一ID.如果没有明确的给Bean设置ID,那么spring会根据类名为其指定一个ID(类名的第一个字母变为小写)。

如果想为Bean设置不同的ID,那么可以把期望的ID值传递给@component注解。@Componet("name1")

​ 尽管自动化装配很方便,但如果我们想将第三方的组件装配到spring容器,那么自动化装配就行不通了,因为没有办法在它的类上添加@Component.在这种情况下就需要采用显示装配,有两种可选方案:java和XML.

显示配置Bean–java方式

声明简单Bean

通过@Configuration创建配置类,然后在配置类中编写一个方法,该方法会创建所需类型的实例,然后给这个方法添加@Bean注解,如下所示

@Configuration
public class Config{
	@Bean
	public Student student(){
		return new Student();
	}
}

@Bean注解告诉spring这个方法将会返回一个对象,该对象要注册到Spring容器中。

默认情况下,通过这种方式配置的Bean的ID和方法名一样。上例中的bean的ID就是student。如果你想为其设置一个不同的名字,那么可以重命名该方法,或者通过name属性指定Bean的ID

@Configuration
public class Config{
	@Bean(name="happyStudent")
	public Student student(){
		return new Student();
	}
}

JavaConfig依赖注入

这种声明的bean很简单,他自身没有其他依赖。但如果我们要声明的Bean依赖于其他对象,比如Student对象依赖于Book对象,如何通过JavaConfig实现注入呢?

显示配置Bean–XML方式

首先要明确,学习XML方式只是为了帮助维护已有的XML配置,在进行新的Spring工作时,首选的还是自动化配置和JavaConfig。现在我们有一个Student类如下,那我们怎么通过xml方式将Bean加载到spring容器中呢?

public class Student {

	private Book book;

	private String name;

	private List<String> friends;

	public Student() {
	};

	public Student(final Book book) {
		this.book = book;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(final Book book) {
		this.book = book;
	}

	public String getName() {
		return name;
	}

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

	public List<String> getFriends() {
		return friends;
	}

	public void setFriends(final List<String> friends) {
		this.friends = friends;
	}

}

第一步,创建一个标准的xml配置文件,该配置文件的顶部声明多个xml模式(xsd)文件,这些文件定义了配置Spring的xml元素。通过将book声明为一个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" 
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
	<!--创建Book实例,该Bean的id是book -->	
	<bean id="book" class="com.study.FirstSpringBoot.dto.Book">
        <!--借助构造器注入字面值 -->
		<constructor-arg value="三国"/>
	</bean>
		
	<bean id="Student"
		class="com.study.FirstSpringBoot.dto.Student">
		<!--借助构造器注入依赖Bean ref引用的是依赖bean的ID-->
		<constructor-arg ref="book"/>
        <!--属性注入字面值 -->
		<property name="name" value="zhangsan" />
        <!--装配集合 -->
		<property name="friends">
			<list>
				<value>lisi</value>
				<value>wangwu</value>
			</list>
		</property>
	</bean>
</beans>

接下来如何让spring发现这个xml文件,并把其中的bean实例化到容器中呢?

答案就是@ImportResource注解,将该注解添加到配置类上,如下所示:

@ImportResource("classpath:Student.xml")
@Configuration
public class StudentConfig {

}

如此,xml配置的bean就加载到了spring容器中了。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值