开始学习java中的一个Spring框架,其作用是用来简化程序的开发
spring其本质是管理软件中的对象,对其进行创建对象,维护对象之间的关系,强调低耦合性
spring中任何的java类和javaBean都会被当做bean来处理,这些bean通过容器来进行管理和应用,通过ioc和aop机制来简化bean对象的创建和bean对象之间的解耦
spring容器的实例化,创建对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
加载工程classPath下就是这样配置的。
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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">
</beans>
需要在此文件中添加bean定义
<bean id="标识符" class=“bean类型”>,然后在创建ApplicationContext后,就可以通过getBean()获取对象
bean对象的实例化一般都是用构造器来进行实例化:如下
<bean id="pe" class="com.su.bean.People" ></bean>
此对象,class里面的内容是自建的一个包名+包下class文件名
id可以认为是这个class属性的一个别名,会根据这个id来创建对象,id属性具有唯一性,不能使用特殊字符
IOC(inversion of control)控制反转。创建对象由原先的new转变为引入第三方来创建,注入对象,降低对象间的耦合度。
IOC一般都是进行依赖注入DI,,此DI注入有两种方式setter注入和构造器注入,如下
第一种是setter注入,写个man类实现Serializable,
public class Man implements Serializable {
private String hobby;
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
添加bean定义,使用setter注入。name就是man的hobby属性,value就是设置属性值
<bean id="m" class="com.su.bean.Man">
<property name="hobby" value="playfootball"></property>
</bean>
第二种是构造器注入,写个woman类实现Serializable,
public class Woman implements Serializable {
private String hobby;
public Woman(String hobby) {
super();
this.hobby = hobby;
}
public String getHobby() {
return hobby;
}
}
添加bean定义,使用构造器注入,index就是woman的第一个属性,index从0开始,value就是设置属性值
<bean id="w" class="com.su.bean.Woman">
<constructor-arg index="0" value="shopping"></constructor-arg>
</bean>
最后就可以可以获取man类的hobby的属性值,woman类的获取方法相同
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Man man=ac.getBean("m",Man.class);
System.out.println(man.getHobby());
IOC可以进行自动装配,自动装配可以减少xml的注入配置
写一个People类
public class People implements Serializable {
private Man m;
private Woman w;
public Man getM() {
return m;
}
public Woman getW() {
return w;
}
public void setM(Man m) {
this.m = m;
}
public void setW(Woman w) {
this.w = w;
}
}
此people类中有两个类型,分别是man类型和woman类型
添加bean定义
<bean id="pe" class="com.su.bean.People" autowire="byType"></bean>
这里的autowire是制定自动装配的原则
bytype的意思是如果容器中存在一个与制定属性相同的bean,那么将与该属性自动装配。
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
People p=ac.getBean("pe",People.class);
System.out.println(p.getM().getHobby());
System.out.println(p.getW().getHobby());
在这里为什么每个class都要事先serializable?
serializable是序列化,就是将数据结构转化成字节,然后可以进行持久化操作。