通过注解装配Bean

    在Spring中,提供两种方式来让Spring IoC容器发现Bean。

    (1)组件扫描

    注解@Component代表Spring IOC会把这个类扫描生成Bean实例,而其中的value属性代表这个类在Spring中的ID,这就相当于XML方式定义的Bean的id。

    通过定义资源的方式,让Spring IoC容器扫描对应的包,从而把Bean装配进来。

    <context:component-scan base-package="com.shw"></context:component-scan>  

    注解@Resource默认按照Bean实例名称进行装配,@Resource包括name和type两个属性。name属性解析为Bean实例的名称,type属性解析为Bean实例的类型。

一、配置spring配置文件
<?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:component-scan base-package="com.shw"></context:component-scan>  
</beans>
二、创建相关类

User类

package com.shw;
import org.springframework.stereotype.Component;
@Component(value="user")	//组件扫描装配Bean
public class User {
	public void show(){
		System.out.println("User.show() ........");
	}
}

Manger类

package com.shw;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value="manger")	//组件扫描装配Bean	
public class Manger {
	@Resource(name="user")	//按照Bean实例名进行装配
	private User us;
	@Value("管理员")			//值注入
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void show(){
		System.out.println("Manger.show() ........");
		us.show();
	}
}

测试类

package com.shw;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
	public static void main(String[] args) {
		//加载applicationContext.xml
		ApplicationContext context=new ClassPathXmlApplicationContext("spring-cfg.xml");
		//获取实例
		Manger manger=(Manger) context.getBean("manger");
		//调用方法
		manger.show();
		System.out.println(manger.getName());
	}
}

运行效果:


(2)自动装配:通过注解定义,使得一些依赖关系可以通过注解完成。

    注解@Autowired默认按照Bean类型进行装配

    修改Manger类的User类型属性us为自动装配

package com.shw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value="manger")	//组件扫描装配Bean	
public class Manger {
	@Autowired				//按照Bean类型进行装配
	private User us;
	@Value("管理员")			//值注入
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void show(){
		System.out.println("Manger.show() ........");
		us.show();
	}
}

运行结果:


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云淡风轻58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值