大数据正式27
Spring
- 先来张图简单看一下
- spring框架的特点
- 轻量级(减少侵入性、对容器没有依赖性、无须修改代码)
- 整合其他主流框架
-
Spring的IOC
- 旧版
Person p=new Person();
- 新版
Person p=Spring容器.get***();
- 通过Spring容器来管理对象更加简单,不用关心对象的创建对象----解耦
- Inversion of Control
- 控制反转
-
IOC创建对象
- 导jar包
-
创建对象类
package com.peng.pojo; public class Person { private String name; private Integer age; private Person() { super(); } private 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 name+":"+age; } }
-
编辑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.0.xsd"> <bean id="person" class="com.peng.pojo.Person"></bean> </beans>
- 创建Spring容器
//创建Spring容器 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//相对于src目录下
-
通过容器创建对象,调用对象的方法
//通过容器获取对象 Person p=(Person)ac.getBean("person"); p.setAge(1); p.setName("aaa"); System.out.println(p);
- 导jar包
- 简单图示
- 注:项目中包名相关:entitye,domain,pojo,beans----实体类
- 旧版
- Spring配置文件的创建方式
- 通过约束文件配置
- 需要先导入约束文件【提前写好的约束文件】
- 过程
- 模板代码【把重复的代码作为模板放入特定的位置,通过快捷方式获取】
- 配置好相应的代码----通过快捷键快速导入
- 过程
- 用的时候springsimple(alt+/快捷键)
- 复制已有配置文件
- 通过约束文件配置
- 获取对象的方法
- id方式【原理:】
- 任何地方都可以
- class方式【原理:】
- 单个对象可以,重复类型的话----报错(found n:n1,n2...)
- id方式【原理:】
- 标签纸alias
<alias name="和id一致" alias=""/> //此标签
-
Spring创建对象的方式
- 之前
- 构造方法
- 静态获取方法获取对象(eg:单例模式)
- 成员函数方法获取对象
-
现在
- 构造方法---要求类必须有无参构造方法
-
静态工厂【id class factory-merthod】
package com.peng.factory; import java.util.Date; public class CalanderFactory { private static Date c = new Date(); public static Date getCalander() { return c; } }
@Test public void test2() { //获取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取数据 System.out.println(ac.getBean("date")); }
<bean id="date" class="com.peng.factory.CalanderFactory" factory-method="getCalander"></bean>
-
实例工厂(动态工厂)【id(工厂的id) factory-bean factory-method】
package com.peng.factory; import java.util.Date; public class CalanderFactory { private Date c = new Date(); public Date getCalander() { return c; } }
@Test public void test2() { //获取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取数据 System.out.println(ac.getBean("date")); }
<bean id="CalanderFactory" class="com.peng.factory.CalanderFactory"></bean> <bean id="date" factory-bean="CalanderFactory" factory-method="getCalander"></bean>
- Spring工厂【实现FactoryBean接口】
- 之前
-
bean的scope属性
- 默认是单例【饿汉式的创建方式】
- 可以通过该属性来决定对象的创建是单例还是多例
- 创建对象的时机
- 单例--饿汉式
- 多例--懒汉式
- 创建对象的时机
- singleton单例【获取容器的时候就创建出来了】
- prototype多例【获取容器时候还没有创建--获取具体实例的时候才创建出来对象】
- request
- session
- 例
<bean scope=""></bean>
-
Lazy-init懒加载【配合单例来使用:单例+懒加载】
-
default(取决于 default-lazy-init="")
<?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.0.xsd" default-lazy-init=""> <bean lazy-init=""></bean> </beans>
- true
- false
- 例
<bean lazy-init=""></bean>
-
- spring容器管理对象的生命周期
- spring管理的对象:单例对象
- 对象创建
- 对象初始化【init-method】
- 对象销毁【init-method】
- 例
<bean id="person" class="com.peng.pojo.Person" init-method="" destroy-method=""></bean>
DI(Dependency Injection)
-
依赖注入(属性注入)
-
set方式:必须要有setter方法
- property属性-----具体的属性名
- ref对象-----bean的id
package com.peng.pojo; public class Person { private String name; private Integer age; private Person() { super(); } private 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 name+":"+age; } }
package com.peng.pojo; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Info { private String name; private Integer age; private List<String> list; private Properties prop; private Map<String, String> map; private Set<String> set; private Person person; // 无参构造 private Info() { super(); } // 含参构造 private Info(String name, Integer age, List<String> list, Properties prop, Map<String, String> map, Set<String> set, Person person) { super(); this.name = name; this.age = age; this.list = list; this.prop = prop; this.map = map; this.set = set; this.person = person; } // setter/getter方法 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; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Properties getProp() { return prop; } public void setProp(Properties prop) { this.prop = prop; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
<?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.0.xsd"> <bean id="person" class="com.peng.pojo.Person"> <property name="name" value="人"></property> <property name="age" value="28"></property> </bean> <bean id="info" class="com.peng.pojo.Info"> <property name="name" value="信息"></property> <property name="age" value="11"></property> <property name="list"> <list> <value>1</value> <value>2</value> <value>3</value> <value>3</value> </list> </property> <property name="prop"> <props> <prop key="a">a</prop> <prop key="b">b</prop> </props> </property> <property name="map"> <map> <entry key="A" value="A"></entry> <entry key="B" value="B"></entry> </map> </property> <property name="set"> <set> <value>11</value> <value>22</value> </set> </property> <property name="person" ref="person"></property> </bean> </beans>
@Test public void test3() { // 获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml"); // 获取数据 System.out.println(((Info) ac.getBean("info")).getName()); System.out.println(((Info) ac.getBean("info")).getAge()); System.out.println(((Info) ac.getBean("info")).getList()); System.out.println(((Info) ac.getBean("info")).getMap()); System.out.println(((Info) ac.getBean("info")).getProp()); System.out.println(((Info) ac.getBean("info")).getSet()); System.out.println(((Info) ac.getBean("info")).getPerson()); }
- 构造方法:必须有相应的构造方法【注意:注入几个属性--提供相同数量的属性的构造函数】【index:从零开始】
// 含参构造 private Info(String name, Integer age, List<String> list, Properties prop, Map<String, String> map, Set<String> set, Person person) { super(); this.name = name; this.age = age; this.list = list; this.prop = prop; this.map = map; this.set = set; this.person = person; }
<constructor-arg name="" value=""></constructor-arg> <constructor-arg name="" ref=""></constructor-arg>
<constructor-arg index="" value=""></constructor-arg> <constructor-arg index="" ref=""></constructor-arg>
-
经典三层架构spring版--工厂用Spring代替
-
dao
-
PersonDao
package com.peng.dao; import com.peng.pojo.Person; public interface PersonDao { void savePerson(Person p); }
-
PersonDaoImpl
package com.peng.dao; import com.peng.pojo.Person; public class PersonDaoImpl implements PersonDao { @Override public void savePerson(Person p) { System.out.println("保存了:" + p); } }
-
-
pojo
-
Person
package com.peng.pojo; public class Person { private String name; private Integer age; private Person() { super(); } private 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; } }
-
-
service
-
PersonService
package com.peng.service; import com.peng.pojo.Person; public interface PersonService { void savePerson(Person p); }
-
PersonServiceImpl
package com.peng.service; import com.peng.dao.PersonDao; import com.peng.pojo.Person; public class PersonServiceImpl implements PersonService { private PersonDao dao; public PersonDao getDao() { return dao; } public void setDao(PersonDao dao) { this.dao = dao; } @Override public void savePerson(Person p) { if (null != dao) { dao.savePerson(p); } } }
-
-
test
-
Test
package com.peng.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.peng.pojo.Person; import com.peng.web.PersonServlet; public class Test { @org.junit.Test public void trest1() { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml"); Person person = (Person) ac.getBean("person"); PersonServlet servlet = (PersonServlet) ac.getBean("servlet"); servlet.doSomething(person); } }
-
-
web
-
PersonServlet
package com.peng.web; import com.peng.pojo.Person; import com.peng.service.PersonService; public class PersonServlet { private PersonService service; public PersonService getService() { return service; } public void setService(PersonService service) { this.service = service; } public void doSomething(Person p) { service.savePerson(p); } }
-
-
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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- pojo对象 --> <bean id="person" class="com.peng.pojo.Person"> <property name="name" value="zhangsan"></property> <property name="age" value="11"></property> </bean> <!-- dao层 --> <bean id="dao" class="com.peng.dao.PersonDaoImpl"></bean> <!-- service层 --> <bean id="service" class="com.peng.service.PersonServiceImpl"> <property name="dao" ref="dao"></property> </bean> <!-- servlet层 --> <bean id="servlet" class="com.peng.web.PersonServlet"> <property name="service" ref="service"></property> </bean> </beans>
Xml约束:规范xml的具体规则
1. Scheme
2. DTD