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