刚刚开始接触Spring,将学到的重点整理记录,方便以后复习。
首先使用Spring需要一个配置文件默认文件名一般取applicationContext.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:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/tx/spring-jms-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/tx/spring-lang-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/tx/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
</beans>
这段代码可以理解为不变的,和之后要用到的标签相对应,当然这是我的理解,有兴趣的可以自己去查阅相关资料
下面是通过配置文件来在类中实例化对象
<!-- 使用无参构造器创建对象
class属性要写累的的全名(加包名)
-->
<bean id="stu1" class="first.Student">
</bean>
<bean id="date1" class="java.util.Date">
</bean>
<!-- 使用静态工厂方法创建对象
factory-method属性:指定一个静态方法
Spring容器会调用这个静态方法来创建对象。
-->
<bean id="cal1" class="java.util.Calendar" factory-method="getInstance"></bean>
<!--使用实例工厂方法创建对象
factory-bean属性:制定一个bean的id
factory-method属性:指定一个方法
spring 容器会调用这个bean的对应方法来创建对象。
-->
<bean id="time1" factory-bean="cal1" factory-method="getTime"></bean>
JAVA中的内容
package first;
import java.util.Calendar;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FirstSpring {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
/*System.out.println(ac);*/
//获得对象
Student stu=ac.getBean("stu1",Student.class);
System.out.println(stu);
Date date=ac.getBean("date1",Date.class);
System.out.println(date);
Calendar cal1=ac.getBean("cal1",Calendar.class);
System.out.println(cal1);
Date time1=ac.getBean("time1",Date.class);
System.out.println(time1);
}
}
Student类中的内容
package first;
public class Student {
public Student() {
System.out.println("Student()");
}
}
一般步骤1.启动Spring容器。2,获取bean元素。前提先配好配置文件。