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 注解的介绍,见spring框架——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=雅阁]]