spring的零配置-----注解规约配置

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.运行结果




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值