Spring的注解IOC

Java注解方式出来很长时间了,迟迟偷懒,对此不曾多加理会。今晚大致看了一下Spring的注解实现,果然省略不少往日Spring XML文件的编写和维护的工作。

 

  使用注解定义Boss类,名称:boss,范围:prototype

 

@Component("boss")
@Scope("prototype")
public class Boss {
	@Autowired
	private Car car;

	@Autowired
	private Office office;

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public Office getOffice() {
		return office;
	}

	public void setOffice(Office office) {
		this.office = office;
	}

	@Override
	public String toString() {
		return "car:" + car + "\n" + "office:" + office;
	}

	@PostConstruct
	public void postConstruct1() {
		System.out.println("postConstruct1");
	}

	@PreDestroy
	public void preDestroy1() {
		System.out.println("preDestroy1");
	}

}

 

   在Spring XML文件中声明如下内容:用于Spring容器从哪里扫描Bean对象(提供regex、aspectj两种表达式)

<context:component-scan base-package="ioc.annotation.example6">
		<context:include-filter type="regex"
			expression="com\.annotation\.example6\..*" />
		<context:exclude-filter type="aspectj"
			expression="ioc.annotation.example6.service..*" />
</context:component-scan>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值