Spring注解驱动开发----->容器day01

1.回顾旧的方式(xml去开发)!此处省略pom.xml文件!

Person实体类:

package com.atguigu.bean;

public class Person {
    private String name;
    private Integer age;
    public Person() {
        super();
    }    
    public Person(String name, Integer age) {
        super();
        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 + "]";
    }
}

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

</beans>                        


Test类:

package com.yikuan.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.bean.Person;

public class TestAnnotation {
    ClassPathXmlApplicationContext cc;

    @Before
    public void init() {
        cc = new ClassPathXmlApplicationContext("beans.xml");
    }

    @Test
    public void test01(){
        Person bean = cc.getBean("person",Person.class);
        System.out.println(bean);
    }

    @After
    public void destory() {
        cc.close();
    }
}


结果:
Person [name=张三, age=18]

2.使用注解去开发 (以前配置文件的方式---->配置类的方式)

1.实体类同上叙述
2.配置类

package com.atguigu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.atguigu.bean.Person;

/**
 * 配置类==old配置文件
 * @author Administrator
 *
 */
@Configuration    //告诉spring这是一个配置类
public class MainConfig {
    /**
     * person方法相当于原来配置文件中的组件
     * @return
     */
    @Bean("yikuan")    //给容器中注册一个Bean(id默认使用方法名,类型为返回值的类型)
    public Person person(){
        return new Person("李四",20);
    }
}


3.测试类

package com.yikuan.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.bean.Person;
import com.atguigu.config.MainConfig;

public class TestAnnotation {
    ClassPathXmlApplicationContext cc;

    @Before
    public void init() {
        cc = new ClassPathXmlApplicationContext("beans.xml");
    }

    @Test
    public void test01(){
        Person bean = cc.getBean("person",Person.class);
        System.out.println(bean);
    }
    
    @Test
    public void test02(){
        AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(MainConfig.class);
        Person bean = aa.getBean(Person.class);
        System.out.println(bean);
        //查看id的值
        String[] beanNamesForType = aa.getBeanNamesForType(Person.class);
        for (String name : beanNamesForType) {
            System.out.println(name);
            //此时的默认id名字为【person】
            //使用@Bean("yikuan")就可以修改bean对象id的名字
            //注意!spring-context版本问题,低版本有些不可以在@Bean后面加()修改名字
        }
    }

    @After
    public void destory() {
        cc.close();
    }
}


结果:
Person [name=李四, age=20]
yikuan

 

转载于:https://www.cnblogs.com/yikuan-919/p/9716544.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值