Spring提供
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.li.spring.autowire">context:component-scan>
beans>
默认情况下,
@Component——通用型的构造型注解,表示该类为Spring组件
@Controller——标识将该类定义为Spring MVC controller
@Repository——将该类标识为数据仓库
@Service——标识将该类定义为服务
使用@Component标注的任意自定义注解
自动注解其Bean的id为首字母小写的类名,当然也可以通过注解的属性为其指定名称。
通过
<context:component-scan base-package="com.li.spring.autowire">
<context:include-filter type="assignable" expression="com.li.spring.chineseidol.Instrument"/>
context:component-scan>
过滤组件扫描
Filter Type | Description |
---|---|
annotation | 过滤器扫描使用指定注解所标注的类,使用的是express属性指定所要扫描的注解 |
assignable | 过滤器扫描派生于expression属性所指定类的那些类 |
aspectj | 过滤器扫描与expression属性所指定的aspectj表达式所匹配的那些类 |
custom | 使用自定义的 org.springframeword.core.type.TypeFilter 实现类,此类由expression属性来指定 |
regex | 过滤器扫描类的名称与expression表达式正则匹配的那些类 |
使用五种过滤器类型的任意一种来自定义组件扫描方式,还可以使用
导弹接口,具备发射功能
package com.li.autowire;
/*导弹接口*/
public interface IMissile {
void fire();
}
导弹接口的实现类,需要标注为Spring Bean
package com.li.autowire;
import org.springframework.stereotype.Component;
/*导弹类,实现导弹接口*/
@Component
public class Missile implements IMissile {
@Override
public void fire() {
System.out.println("The missile is fired...");
}
}
具有发射导弹的无人机类,也需要注解为Spring Bean
package com.li.autowire;
import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
/*无人机类*/
@Component
public class Uav {
/*无人机装配有导弹*/
/*使用注解注入,则getter和setter方法可以不需要*/
@Autowired
private IMissile missile;
@Value("B-2")
private String type;
// 读取方法可以不需要
// public IMissile getMissile() {
// return missile;
// }
//
// public void setMissile(IMissile missile) {
// this.missile = missile;
// }
public Uav(IMissile missile) {
super();
this.missile = missile;
}
public Uav() {
super();
}
public void attack() {
missile.fire();
}
@Override
public String toString() {
return "Uav [type=" + type + "]";
}
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("com"+File.separator+"li"+File.separator+"autowire"+File.separator+"uav.xml");
Uav uav=(Uav)ctx.getBean("uav");
uav.attack();
System.out.println(uav.toString());;
}
}
使用自动检测并无意味着我们就可以完全脱离XML配置文件了,我们还需要在XML配置文件中开启自动检测,但是已经无需在这里面配置Bean了(注意配置的base-package)。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.li.autowire">context:component-scan>
beans>