spring注解第01课 @Configuration、@Bean

一、原始的 xml配置方式

1.Spring pom 依赖 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.2.RELEASE</version>
</dependency>

2.JavaBean

public class Person {
    private String name;
    
    private Integer age;
 
    public Person() {
    }
 
    public Person(String name, Integer age) {
        this.name = name;
        this.age = 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;
    }
 
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}

3.beans.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 
    <bean id="person" class="com.atguigu.bean.Person">
        <property name="name" value="张三" />
        <property name="age" value="10" />
    </bean>    
    
</beans>

4.测试类

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.atguigu.bean.Person;
 
public class Test {
 
    public static void main(String[] args) {
        ClassPathXmlApplicationContext beans = new ClassPathXmlApplicationContext("beans.xml");
        Person p = (Person)beans.getBean("person");
        System.out.println(p);
 
    }
 
}

二、注解的形式

1. @Configuration替代beans.xml,@Bean 替代<bean>

package com.atguigu.config;
 
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.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
 
import com.atguigu.bean.Person;
 
//配置类==配置文件
@Configuration  //告诉Spring这是一个配置类
public class MainConfig {
    //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
    @Bean("person")
    public Person person01(){
        return new Person("lisi", 20);
    }
 
}

2.测试用例

package com.atguigu;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.atguigu.bean.Person;
import com.atguigu.config.MainConfig;
 
public class MainTest {
    
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
        
        String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name : namesForType) {
            System.out.println(name);
        }
    }
}

 

posted @ 2018-09-22 22:26  超轶绝尘  阅读(228)  评论(0编辑  收藏  举报