【spring框架】通过注解配置bean

1 前言

        通过在类名前添加如下注解,并在 xml 文件中配置 context:component-scan 属性,指定待扫描的包(此类在该包下),IOC 容器将自动创建该类的一个 bean,其 id 为该类的类名(首字母转小写),可以通过在注解后添加 value 属性,指定创建的 bean 的 id,如:@Component(value="customized_bean_id")。

  • @Component:普通组件,标识一个受 IOC 容器管理的组件
  • @Repository:持久化层组件,标识一个受IOC容器管理的持久化层组件
  • @Service:业务逻辑层组件,标识一个受IOC容器管理的业务逻辑层组件
  • @Controller:表述层控制器组件,标识一个受IOC容器管理的表述层控制器组件

        以上注解不能加在接口和抽象类上,因为它们不能实例化。配合 @Autowire 注解,可以实现更灵活的自动装配 bean。

        在实验之前,需要导入如下  jar 包:

spring-aop-4.0.0.RELEASE.jar

2 案例一

2.1 注解配置 bean 的基本应用

        Person.java

package com.test;

import org.springframework.stereotype.Component;

@Component
public class Person {	
	public Person() {
		System.out.println("创建 Person 对象");
	}
}

        注意:Person 类前加了 @Component 标注 

        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-4.0.xsd">			
	<context:component-scan base-package="com.test"></context:component-scan>
</beans>

        注意:Person 类在 com.test 包中 

        Test.java

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p=ac.getBean("person",Person.class);
		System.out.println(p);
	}
}

        运行结果:

创建 Person 对象
com.test.Person@ea1a8d5

        由以上输出结果知:在 IOC 容器中创建了一个 id 为 person 的 bean。 

2.2 自定义 beanId

        通过在注解后添加 value 属性,指定创建的 bean 的 id。xml 文件2.1节。

        Person.java

package com.test;
 
import org.springframework.stereotype.Component;
 
@Component(value="ps")
public class Person {	
	public Person() {
		System.out.println("创建 Person 对象");
	}
}

        Test.java

package com.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
 
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p=ac.getBean("ps",Person.class);
		System.out.println(p);
	}
}

        运行结果:

创建 Person 对象
com.test.Person@ea1a8d5

        由以上输出结果知:在 IOC 容器中创建了一个 id 为 ps 的 bean。 

2.3 注意事项

        (1)在 xml 文件中,若需要对多个包进行扫描,可以将它们一起放在 base-package 中,并用逗号隔开,如下:

<context:component-scan base-package="com.test,com.demo"></context:component-scan>

        (2)在一个包内,若只需扫描指定的注解或类,可以使用 include-filter 属性,如下:

        只扫描指定的注解:

<context:component-scan base-package="com.test" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>

        只扫描指定的类:

<context:component-scan base-package="com.test" use-default-filters="false">
	<context:include-filter type="assignable" expression="com.test.Person"/>
</context:component-scan>

        (3)在一个包内,若不扫描指定的注解或类,可以使用 exclude-filter 属性,如下:

        不扫描指定的注解:

<context:component-scan base-package="com.test" use-default-filters="true">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

        不扫描指定的类:

<context:component-scan base-package="com.test" use-default-filters="true">
	<context:exclude-filter type="assignable" expression="com.test.Car"/>
</context:component-scan>

3 案例二(配合 @Autowire注解)

        关于 @Autowire 注解的介绍,见\tospring框架——bean的自动装配

        Car.java

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Car {
	private String cbrand="奥迪";
	
	@Autowired
	private Engine engine;
	
	public Car() {
		System.out.println("创建 Car 对象");
	}

	@Override
	public String toString() {
		return "Car [cbrand=" + cbrand + ", engine=" + engine + "]";
	}
}

        Engine.java

package com.test;

import org.springframework.stereotype.Component;

@Component
public class Engine {
	private String ebrand="雅阁";

	@Override
	public String toString() {
		return "Engine [ebrand=" + ebrand + "]";
	}
}

        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-4.0.xsd">			
	<context:component-scan base-package="com.test"></context:component-scan>
</beans>

        Test.java

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		Car c=ac.getBean("car",Car.class);
		System.out.println(c);
	}
}

        运行结果:

创建 Car 对象
Car [cbrand=奥迪, engine=Engine [ebrand=雅阁]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

little_fat_sheep

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

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

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

打赏作者

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

抵扣说明:

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

余额充值