1.使用xml配置文件装配
Spring成功启动的三大要件分别是:Bean定义信息、Bean实现类以及Spring本身。
在xml文件中配置bean,然后使用ClassPathXmlApplicationContext得到Application从而得到bean
首先定义一个bean类:Student
package com.bean; public class Students { String name; String id; String major; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
在xml文件中配置这个bean: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-3.1.xsd"> <bean id="students" class="com.bean.Students"> <!--1--> <property name="name" value="ironman"></property><!--2-->
<property name="id" value="111111"></property>
<property name="major" value="software"></property>
</bean>
</beans>
(1)处定义了bean的name和class。
(2)处定义了bean的初始值。
我们可以用一个函数来测试一下这个函数
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Students s =(Students) context.getBean("students"); System.out.println(s.name+"\n"+s.id+"\n"+s.major); } }
得到Students的一个对象
2.使用注解配置bean
不管是XML还是注解,他们都是表达bean定义的载体,其实质都是为Spring容器提供bean的定义信息,表现形式上是将xml定义的东西通过类注解进行描述。
我们从新定义一个Bean并且注入Students哪个Bean
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component("myClass")//这里通过Component来定义了一个Bean,括号里面的是自己定义的Bean的名字相当于xml中的id public class MyClass { @Autowired //这里自动注入Students的Bean private Students students; private String classId; private String className; private String teacherName; public String getClassId() { return classId; } public void setClassId(String classId) { this.classId = classId; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public Students getStudents() { return students; } public void setStudents(Students students) { this.students = students; } }
还需要在xml文件配置
Spring在2.5之后提供了一个context命名空间,它提供了通过扫描应用类包以应用注解定义bean的方式。
<?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"
<!--声明context命名空间--> 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean id="students" class="com.bean.Students"> <property name="name" value="litao"></property> <property name="id" value="1110321124"></property> <property name="major" value="software"></property> </bean> <context:component-scan base-package="com.bean"></context:component-scan><!--使用componet-scan的base-package属性定义要扫描的类包--> </beans>
我们用一个Main函数来测试一下这个定义
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainForComponent { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); MyClass myClass =(MyClass) context.getBean("myClass"); myClass.setClassId("software engineering"); System.out.println(myClass.getClassId()+"\n"+myClass.getClassName()+"\n"+myClass.getStudents().getId()); } }
3.基于java类的配置
我们可以不再用xml配置文件来定义bean了
定义一个Class为Bean的配置类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.bean.MyClass; import com.bean.Students; @Configuration //将一个pojo标注为定义bean的配置类 public class AppConf { @Bean //将一个pojo定义为一个bean public Students myStudents(){ return new Students(); } @Bean public MyClass myClass(){ MyClass myClass = new MyClass(); myClass.setStudents(myStudents());//将Students注入MyClass return myClass(); } }
同样的我们来写一个Main来测试一下这个bean的配置函数
import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.bean.MyClass; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new AnnotationConfigApplicationContext(AppConf.class); MyClass myClass = context.getBean(MyClass.class); } }