1.零配置:是指减少配置数量以加快开发速度,并不是说没有配置
它有两种实现方式:1.1注解配置方式(以@方式放在方法或者类之上以表示该类的bean用途,spring就是用这个配置方式) 1.2约定优于配置(像struts2的action是早就约定好规范的,更像是内置的一样)
2.零配置使用环境
2.1.前提是你的xml配置是使用xml schema方式的
2.2该xml schema配置上配置以下这几个红体文件
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="one"/><!--这个指定了spring将会把one包下所有的java类当做spring bean来处理,并为它提供对应的Bean实例 -->
</beans>
3.贴上我的零配置全文配置的代码
3.1
public class test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
System.out.println(java.util.Arrays.toString(ac.getBeanDefinitionNames()));
}
}
3.2
import org.springframework.stereotype.Component;
@Component
public class Chinese implements Person {
public String name;
public String age;
public Axe axe;
public Axe getAxe() {
return axe;
}
public void setName(String name) {
this.name = name;
}
public void setAxe(Axe axe) {
this.axe = axe;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Chinese() {
System.out.println("正在初始化chinese类");
}
@Override
public void usechop() {
System.out.println(name+"在用");
axe.chop();
}
}
3.3
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="one"/>
</beans>
4.运行结果