Spring 基于纯注解装配Bean与纯 JavaConfig配置实现 Bean容器的装载(六)

一、基本注解认识与学习
1、@Component : 扫描组件 ,代表被Spring接管了!

相当于这样配置:

在这里插入图片描述

2、属性如何注入

@Value(“勒布朗-詹姆斯”) 代表:显示的属性上进行赋值操作。

package com.ZQQQ.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component //组件
@Scope(“prototype”)
public class User {
private String name;
public String getName() {
return name;
}
@Value(“勒布朗-詹姆斯”)
public void setName(String name) {
this.name = name;
}
}

3、@Component //组件
public class User {
private String name;

public String getName() {
	return name;
}
@Value("勒布朗-詹姆斯")
public void setName(String name) {
	this.name = name;
}
}
4、衍生的注解:
@Component 有几个衍生注解,我们在web开发中,会按照mvc 三层
	dao 【@Repository】 // 仓库
	Service 【@Service】//组件
	controller【@Controller】	//加上此注解代表被Spring 托管了。
	
这四个注解功能都是一样的,都是代表将某个类注册到spring容器中,装配Bean。
5、@Component //组件

@Scope(“prototype”) //单例模式中的原型模式

6、applicationcontext.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
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启基于注解自动装配的支持 -->
<context:annotation-config/>

<bean id="User" class="com.ZQQQ.pojo.User"/>
<!--指定要扫描的包,这个包下的注解就会生效 -->
<context:component-scan base-package="com.ZQQQ"/>

</beans>
7、小结

在这里插入图片描述

二、JavaConfig实现配置
  • 这种方式好处省去了在resources下创建Spring容器的上下文也就是(Applicationcontext.xml)文件

  • 完全基于注解进行实现装载Bean容器

  • 完整版案例
pojo类:
package com.ZQQQ.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Component
public class User {
private String name;
private int age;
private String sex;

public String getName() {
	return name;
}
@Value("科比-布莱恩特")
public void setName(String name) {
	this.name = name;
}

public int getAge() {
	return age;
}
@Value("30")
public void setAge(int age) {
	this.age = age;
}

public String getSex() {
	return sex;
}
@Value("男")
public void setSex(String sex) {
	this.sex = sex;
}
}
Config类:也就相当于Applicationcontext.xml 文件
package com.ZQQQ.config;
import com.ZQQQ.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;


@Configuration			//第一步:等价于 resources 下的ApplicationContext.xml
@Component("com.ZQQQ.pojo")
@Import(ZqConfig02.class)
public class ZqConfig {
@Bean				//第三步:注册一个bean,就相当于我们之前写的一个bean标签。
 public User getUser(){
	/**getUser 相当于bean 标签中的id属性
	 * 这个方法的返回值,就相当于bean 标签中的class属性
	 */
return  new User();	//第二步:写成方法的方式 返回需要返回的对象。
}
}
测试类:
import com.ZQQQ.config.ZqConfig;
import com.ZQQQ.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
	public static void main(String[] args) {
	// 如果完全使用了配置类的方式去做,我们只能通过AnnotationConfig 上下文来获取容器,通过配	置类的class对象加载。

	AnnotationConfigApplicationContext context = new 	AnnotationConfigApplicationContext(ZqConfig.class);
	User user = (User) context.getBean("getUser");
	System.out.println(user.getName());
	System.out.println(user.getAge());
	System.out.println(user.getSex());
}
}
控制台输出:

在这里插入图片描述

涉及到的注解总结:
注解含义
@Component这个注解的含义:就是说明这个类被Spring接管了,注册到了容器中。
@Value(“科比-布莱恩特”)这个注解的含义:显示的注入值
@Configuration代表这是一个配置类。加上此注解等价于 resources 下的ApplicationContext.xml
@Bean封装成一个Bean容器文件 就是说明这个类被Spring接管了,注册到容器中。因为它本就是@Component
@Import(ZqConfig02.class)引入为一个类,这是涉及到多个Bean也就是类时所用

骏马是跑出来的,强兵是打出来的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值