首页 > 学院 > 开发设计 > 正文

Spring(二、实际操作)

2019-11-06 07:38:36
字体:
来源:转载
供稿:网友

SPRing的IOC入门 1、导入Spring jar包 这里写图片描述 一共是20个jar包,其中包括一个日志包(logging) 也可以只导入6个包: 这里写图片描述(此图片来自其它截图)

IOC底层原理 IOC: 把对象的创建交给Spring管理,不需要手动创建对象 原理图: 这里写图片描述 2.创建配置文件 在src下面创建配置文件,名称没有固定的要求,一般是applicationContext.xml 引入约束,之前在Struts2或者Hibernate里面引入约束是dtd约束,在spring里面的配置文件映入的是schema约束,可以在文件中找到该约束 路径:spring-framework-4.3.6.RELEASE-dist/spring-framework-4.3.6.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--第四步 在配置文件中配置哪个类进行对象创建--> <bean id="person" class="com.iotek.daoImpl.PersonDaoImpl"> </bean></beans>

3.创建类,为类添加属性;同时生成set方法

public class PersonDaoImpl implements PersonDao { private String name; private int age; private String hobby; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } @Override public void printMessage() { System.out.println("名字:"+name+"年龄:"+age+"爱好:"+hobby); System.out.println("这里是PersonDaoImpl"); }}

4.在配置创建对象的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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--IOC入门--><bean id="person" class="com.iotek.daoImpl.PersonDaoImpl"><!-- 依赖注入 --> <property name="name" value="gaougozhen"></property> <property name="age" value="24"></property> <property name="hobby" value="nohobby"></property></bean></beans>

5.创建测试类

public class TestDemo { public static void main(String[] args) { //加载配置文件,创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //根据bean里面的id值获取创建的对象 PersonDao person = (PersonDao) context.getBean("person"); person.printMessage(); }}

Spring的工厂类 ApplicationContext

ClassPathXmlApplicationContext :解析类路径下的XML的. FileSystemXmlApplicationContext :解析本地磁盘上的XML的.

这里写图片描述 BeanFactory和ApplicationContext都是Spring的工厂 BeanFactory是Spring工厂的老版本类:第一次调用getBean方法的时候实例化 ApplicationContext是Spring新版本的工厂类:在加载核心配置文件的时候,将所有的类实例化

——————————————————————————————————————————————————–

Spring的bean管理(xml配置–非注解) bean管理:创建对象的过程,创建对象的过程中设置属性值

Bean的实例化的三种方式(Spring创建对象的三种方式) 第一种:使用无参的构造方法创建

package com.iotek.daoImpl;public class Bean1 { public Bean1() { } public void b1() { System.out.println("bean1................"); }}

第二步:在配置文件application.xml中配置

<bean id="bean1" class="com.iotek.daoImpl.Bean1" scope="prototype"></bean>

第三步:创建测试类

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Bean1 bean1=context.getBean("bean1",Bean1.class); bean1.b1();

第二种:使用静态工厂创建 静态工厂:这个工厂类的里面的方法是static的,可以通过类名直接调用 实态工厂:这个工厂类的里面的方法不是static的,创建工厂的对象,再调用里面的方法 第一步:创建类

public class Bean2 { public void b2() { System.out.println("b2..............."); }}

第二步:创建静态工厂类

package com.iotek.daoImpl;/** * 使用静态工厂创建对象 */public class Bean2Factory { //静态方法 public static Bean2 getBean2() { return new Bean2(); }}

第三步:在配置文件application.xml中配置

<!-- 使用静态工厂创建对象 class: 工厂类的全路径 factory-method: 工厂里面静态的方法 --><bean id="bean2Factory" class="com.iotek.daoImpl.Bean2Factory" factory-method="getBean2"></bean>

第四步:创建测试类

Bean2 bean2=context.getBean("bean2Factory",Bean2.class); System.out.println(bean2); bean2.b2();

第三种:使用实例工厂创建 第一步:创建类

public class Bean3 { public void b3() { System.out.println("b3..............."); }}

第二步:创建静态工厂类

public class Bean3Factory { public Bean3 getBean3() { return new Bean3(); }}

第三步:在配置文件application.xml中配置

<bean id="bean3Factory" class="com.iotek.daoImpl.Bean3Factory"></bean><bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>

第四步:创建测试类

Bean3 bean3=context.getBean("bean3",Bean3.class); System.out.println(bean3); bean3.b3();

Spring的bean的常用配置 标签的id和name属性 id和name有什么区别? id:使用xml中id约束,不可以出现特殊字符 name:出现特殊字符,如果使用了name没有id,那么name可以作为id使用 Spring整合Struts1: 上生命周期的配置

@Test/*** Bean的生命周期的相关配置:* * init-method* * destory-method :只能针对单例对象有效.必须在工厂关闭之后才会销毁对象.*/public void demo1(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); EmployeeService employeeService = (EmployeeService) applicationContex.getBean("employeeService"); employeeService.save(); applicationContext.close();}

上的作用范围的配置 scope属性: singleton :单例的.(默认) prototype :多例的. request :WEB项目中,创建一个对象,保存到request域中. session :WEB项目中,创建一个对象,保存到session域中. globalsession :WEB项目中,特殊环境.分布式开发环境.如果没有分布式环境,相当于session.

Bean的生命周期 Spring的实例化bean过程中总共完成11步骤 1. instantiate bean对象实例化 2. populate properties 封装属性 3. 如果Bean实现BeanNameAware执行setBeanName 4. 如果Bean实现BeanFactoryAware或者ApplicationContextAware设置工厂setBeanFactory或者上下文对象setApplicationContext 5. 如果Bean存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization 6. 如果Bean实现InitializingBean执行afterPropertiesSet 7.调用/ 指定初始化方法init 8. 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization 9. 执行业务处理 10.如果Bean实现 DisposableBean 执行 destroy 11. 调用/ 指定销毁方法 customerDestroy

java里三种方式注入Bean属性 第一种:使用set方法注入

public class User() { private String username; public void setUsername(String username) { this.username = username; } }User user = new User();user.setUsername("东方不败");

第二种:使用有参数的构造方法注入

public class Person{ private int age; public Person(int age) { this.age = age; }}Person p = new Person("岳不群");

第三种:使用接口注入

public interface Dao{ public void run(String passWord);}public class UserDaoImpl implements Dao { private String password; public void run(String password) { this.passwordd = password; }}

Spring注入bean属性的两种方式 Spring使用有参构造方法注入 第一步:创建类 创建类,声明属性,创建类有参数的构造方法

public class PersonImpl implements Person{ private int id; private String name; public PersonImpl(int id,String name){ this.id=id; this.name=name; } @Override public void wirte() { // TODO Auto-generated method stub System.out.println("我的名字是:"+name+"我的ID是:"+id); }}

第二步:在配置文件里面配置 在配置文件里面进行配置,可以参数名称或者参数的位置配置值,applicationContext.xml

<!-- ====================属性的注入方式 ========================--> <!-- 使用有参数构造方法注入属性 --> <bean id="field1" class="cn.itcast.field.ConstructorField"> <!--配置有参数构造设置值 name: 属性名称 value: 属性值 --> <!--根据属性名称注入--> <constructor-arg name="id" value="11"></constructor-arg> <constructor-arg name="name" value="zhangsan"></constructor-arg> <!--根据属性位置注入 第一个参数从 0 开始的 --> <constructor-arg index="0" value="11"></constructor-arg> <constructor-arg index="1" value="zhangsan"></constructor-arg> </bean>

第三步:创建测试类

public class Demo { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Person p=context.getBean("person", PersonImpl.class); p.wirte(); }}

Spring使用set方法注入 第一步:创建类

public class PersonImpl implements Person{ private int id; private String name; @Override public void wirte() { // TODO Auto-generated method stub System.out.println("我的名字是:"+name+"我的ID是:"+id); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}

第二步:在配置文件里面配置 在配置文件里面配置application.xml

<property name="id" value="22"></property> <property name="name" value="lisi"></property>

第三步:创建测试类 省略……….

Spring复杂注入,使用/引入其他bean 第一步:创建两个类

package com.iotek.daoImpl;public class Card { private String num; private String color; public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getColor() { return color; } public void setColor(String color) { this.color = color; }}public class PersonImpl implements Person{ private int id; private String name; private Card card; @Override public void wirte() { // TODO Auto-generated method stub System.out.println("我的名字是:"+name+"我的ID是:"+id); System.out.println("我的Card是:"+card.getNum()+"颜色是:"+card.getColor()); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; }}

第二步:在配置文件里面配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="card" class="com.iotek.daoImpl.Card"> <property name="num" value="130725"></property> <property name="color" value="yellow"></property> </bean> <bean id="person" class="com.iotek.daoImpl.PersonImpl"> <property name="id" value="22"></property> <property name="name" value="lisi"></property> <property name="card" ref="card"></property> </bean></beans>

第三步:创建测试类

package com.iotek.demo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.iotek.dao.Person;import com.iotek.daoImpl.PersonImpl;public class Demo { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Person p=context.getBean("person", PersonImpl.class); p.wirte(); }}

Spring3.0的SpEL的属性注入(SpEL:Spring Expression Language)

SpEL语法:语法:#{SpEL} SpEL用法:

<bean name="card" class="com.iotek.bean.Card"> <property name="id" value="#{11}"></property> <property name="cardNum" value="#{'43542'}"></property> </bean>

Spring的2.5支持p名称空间注入属性(会用)

为了简化XML文件配置,Spring从2.5开始引入一个新的p名称空间 第一步:创建类 创建类,声明属性,生成set方法

package com.iotek.daoImpl;public class Card { private String num; private String color; public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getColor() { return color; } public void setColor(String color) { this.color = color; }}

第二步:在配置文件里面进行配置

声明空间和配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- 声明名称空间 p --> xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- p名称空间注入 --> <bean id="card" class="com.iotek.daoImpl.Card" p:num="333" p:color="blue"></bean>

第三步:创建测试类 省略…….

数组集合属性注入 第一步:创建类

public class Student { private List<String> list = new ArrayList<String>(); private String [] arr; private Set<String> set; private Map<String,Integer> map; public Student(){} public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public String[] getArr() { return arr; } public void setArr(String[] arr) { this.arr = arr; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, Integer> getMap() { return map; } public void setMap(Map<String, Integer> map) { this.map = map; } public void printMes(){ System.out.println("数组是:"+Arrays.toString(arr)); for(String value:list){ System.out.println("list的值是:"+value); } for(String value:set){ System.out.println("set的值是:"+value); } System.out.println("map的值是"+map); }}

第二步:在配置文件里面配置

<bean name="student" class="com.iotek.bean.Student"> <property name="list"> <list> <value>123</value> <value>456</value> <value>789</value> </list> </property> <property name="arr"> <list> <value>xiaoming</value> <value>xiaogao</value> <value>zhangsan</value> <value>lisi</value> </list> </property> <property name="set"> <set> <value>zhangsan</value> <value>lisi</value> <value>wangwu</value> <value>zhoaliu</value> </set> </property> <property name="map"> <map> <entry key="年龄" value="20"></entry> <entry key="nianling" value="21"></entry> </map> </property> </bean>

第三步:创建测试类 省略……………….

Properties属性注入 在配置文件里面配置(其他步骤省略)

<property name="pro"> <props > <prop key="hobby">run</prop> <prop key="height">1.78</prop> </props> </property>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表