Spring学习笔记一、Xml 注解与自动注解的区别

一、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 https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 包扫描 只要标注了 @Controller @Service @ Repository @Component 就会被加入到容器中-->
<!--    <context:component-scan base-package="com.hao"></context:component-scan>-->

    <bean id="person" class="com.hao.bean.Person">
        <property name="age" value="18"></property>
        <property name="name" value="张三"></property>
    </bean>

</beans>

二、自动注解的方式

package com.hao.config;

import com.hao.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * @author haoxiansheng
 * @date 2020-05-28
 */
@Configuration // 告诉spring 这是一个配置类 // 排除那些
@ComponentScan(value = "com.hao",includeFilters ={@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}, useDefaultFilters = false)
//useDefaultFilters 禁用默认扫描属性 默认全部扫描
//includeFilters 指定包含那些组件
//excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})} Filter[]: 指定扫描按照什么规则排除那些组件
public class MainConfig {

    /**
     * 1、给容器注册一个Bean 类型为返回值的类型,id 默认是用方法名作为id
     * 2、给Bean 指定名字可以在bean 中指定value的值
     *
     * @return
     */
    @Bean(value = "person")
    public Person person01() {
        return new Person("李四", 20);
    }
}

三、公共代码块
1、Person为例


package com.hao.bean;

/**
 * @author haoxiansheng
 * @date 2020-05-28
 */
public class Person {
    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2、测试类


package com.hao;

import com.hao.bean.Person;
import com.hao.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



/**
 * @author haoxiansheng
 * @date 2020-05-28
 */
public class MainTest {
    public static void main(String[] args) {

          // Xml注解测试
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//        Person person = (Person) applicationContext.getBean("person");
//        System.out.println(person);

        // 自动注解测试
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person=(Person)applicationContext.getBean("person");
        System.out.println(person);

        String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name :namesForType) {
            System.out.println(name);
        }
    }
}


四、总结
1、可以看出自动注解省去了大量的配置,比较方便。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值