网站文章要求,外国人做的甲骨文网站,广州平面设计培训班哪里好,免费的英文电子外贸网站建设百度网盘#xff1a; #x1f449; Spring学习书籍链接 Spring学习 1 Spring框架概述2 Spring容器3 基于XML方式创建对象4 基于XML方式注入属性4.1 通过set方法注入属性4.2 通过构造器注入属性4.3 使用p命名空间注入属性4.4 注入bean与自动装配4.5 注入集合4.6 注入外部属性… 百度网盘 Spring学习书籍链接 Spring学习 1 Spring框架概述2 Spring容器3 基于XML方式创建对象4 基于XML方式注入属性4.1 通过set方法注入属性4.2 通过构造器注入属性4.3 使用p命名空间注入属性4.4 注入bean与自动装配4.5 注入集合4.6 注入外部属性文件4.7 注入属性的全部代码 1 Spring框架概述
Spring是轻量级的开源的JavaEE框架提供了多个模块Spring可以解决企业应用开发的复杂性Spring有两个核心部分IOC和Aop 1IOC控制反转把创建对象过程交给Spring进行管理 2Aop面向切面不修改源代码进行功能增强Spring特点 1方便解耦简化开发 2Aop编程支持 3方便程序测试 4方便和其他框架进行整合 5方便进行事务操作 6降低API开发难度
2 Spring容器
Spring提供了两种容器分别是BeanFactory和ApplicationConetxt BeanFactory BeanFactory是bean的实例化工厂主要负责bean的解析、实现和保存化操作不提供给开发人员使用
ApplicationContext ApplicationContext继承于BeanFactory提供更多更强大的功能一般由开发人员进行使用
ApplicationContext context new ClassPathXmlApplicationContext(xml路径);
或
ApplicationContext context new FileSystemXmlApplicationContext(xml路径);3 基于XML方式创建对象
使用Spring需要的基础包百度网盘
定义一个User类
package springstudy;//自己的包名public class User {public void add() {System.out.println(add...);}
}创建一个XML文件注意XML文件路径 其中bean id“user” class“springstudy.User”/bean 的 id是唯一标识class是某类的全类名即包名.类名
?xml version1.0 encodingUTF-8 ?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!--配置User对象创建--bean iduser classspringstudy.User/bean
/beans在Test类使用User类注意ClassPathXmlApplicationContext(“bean1.xml”)的路径是./src/bean1.xml其他位置需要使用ClassPathXmlApplicationContext(“file:xml文件绝对路径”)
package springstudy;//自己的包名
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {//加载Spring配置文件ApplicationContext context new ClassPathXmlApplicationContext(bean1.xml);User user context.getBean(user, User.class);System.out.println(user);user.add();}
}运行结果
设置单实例还是多实例 bean 标签里面有属性scope用于设置单实例还是多实例
scope“singleton”表示是单实例对象是默认值scope“prototype”表示是多实例对象
设置 scope 值是 singleton 时加载 spring 配置文件时就会创建单实例对象设置 scope 值是 prototype 时不是在加载 spring 配置文件时创建对象而是在调用getBean 方法时候创建多实例对象
4 基于XML方式注入属性
DI(Dependency Injection)依赖注入就是注入属性 控制反转是通过依赖注入实现的其实它们是同一个概念的不同角度描述。通俗来说就是IoC是设计思想DI是实现方式。 4.1 通过set方法注入属性
在User类中定义set方法 //属性private String name;private int age;private String address;private String degree;public User(int height, int weight) {this.height height;this.weight weight;}//set方法public void setName(String name) {this.name name;}public void setAge(int age) {this.age age;}public void setAddress(String address) {this.address address;}public void setDegree(String degree) {this.degree degree;}在bean1.xml中使用 property 完成属性注入name类里面属性名称 value向属性注入的值property标签可以加 value![CDATA[内容]]/value 或者null/表示null property namename value西施/propertyproperty nameagevalue18/value/propertyproperty nameaddressnull//property4.2 通过构造器注入属性
在User类中定义构造器 //属性private int height;private int weight;//构造器public User(int height, int weight) {this.height height;this.weight weight;}在bean1.xml中使用constructor-arg标签注入属性
constructor-arg nameheight value168/constructor-arg
constructor-arg nameweight value90/constructor-arg4.3 使用p命名空间注入属性
注使用p命名空间注入属性该属性必须定义set方法 在bean1.xml文件中添加p命名空间在bean标签中添加p:属性名“属性值”
xmlns:phttp://www.springframework.org/schema/p
bean iduser classspringstudy.User p:degree本科4.4 注入bean与自动装配
创建Card类
package springstudy;public class Card {private int id;private double money;public void setId(int id) {this.id id;}public void setMoney(double money) {this.money money;}public double getMoney() {return money;}
}在User类定义Card属性 private Card card;public void setCard(Card card) {this.card card;}public Card getCard() {return card;}在bean1.xml添加如下代码如果通过property name“card.money” value“999”/property修改属性必须在User类中定义getCard方法 bean iduser classspringstudy.User p:degree本科!--注入bean方式1--property namecardbean idcard classspringstudy.Cardproperty nameid value1/propertyproperty namemoney value1000/property/bean/property!--注入bean方式2--property namecard refcard/propertyproperty namecard.money value999/property/beanbean idcard classspringstudy.Cardproperty nameid value1/propertyproperty namemoney value1000/property/bean自动装配 自动装配是自动注入相关联的bean到另一个bean通过bean标签的autowire属性实现
autowire“byType”根据class类型自动装配 修改注入Bean方式1设置autowire“byType”在byType(类型模式中Spring容器会基于反射查看bean定义的类然后找到依赖类型相同的bean注入到另外的bean中这个过程需要set方法来完成需要在User类中定义setCard方法如果存在多个类型相同的bean会注入失败这时需要通过在不需要注入的bean中添加autowire-candidate“false”来解决id的属性值可以不和类中定义的属性相同如User类中定义private Card card但是在bean中id可以为card1 bean iduser classspringstudy.User p:degree本科 autowritebyType/beanbean idcard classspringstudy.Cardproperty nameid value1/propertyproperty namemoney value1000/property/beanbean idcard1 classspringstudy.Card autowire-candidate“false”property nameid value1/propertyproperty namemoney value10000/property/beanautowire“byName”根据id属性值自动装配 设置autowire“byName”Spring会尝试将属性名和bean中的id进行匹配如果找到的话就注入依赖中没有找到该属性就为null如User类中定义private Card card需要bean中的id为card才能注入 bean iduser classspringstudy.User p:degree本科 autowirebyName/beanbean idcard classspringstudy.Cardproperty nameid value1/propertyproperty namemoney value1000/property/bean除了通过xml方式自动装配外还可以通过注解自动装配
4.5 注入集合
在User类中定义集合的set方法 //数组private String[] costumes;//list集合private ListString list;private ListString testlist;//map集合private MapString,String maps;//set集合private SetString sets;public void setSets(SetString sets) {this.sets sets;}public void setCostumes(String[] costumes) {this.costumes costumes;}public void setList(ListString list) {this.list list;}public void setTestlist(ListString testlist) {this.testlist testlist;}public void setMaps(MapString, String maps) {this.maps maps;}在bean1.xml注入集合属性除了通过arrayvalue值/value/array或 mapentry key“值” value“值”/entry/map注入属性之外还可以通过util命名空间注入属性不过需要引入util的命令空间以及util的xsd文件
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsdbean iduser classspringstudy.User p:degree本科!--注入集合属性--!--数组类型属性注入--property namecostumesarrayvalue裙子/valuevalue汉服/value/array/property!--list 类型属性注入--property namelistlistvalue张三/valuevalue小三/value/list/propertyproperty nametestlist refbookList/property!--map 类型属性注入--property namemapsmapentry keyJAVA valuejava/entryentry keyPHP valuephp/entry/map/property!--set 类型属性注入--property namesetssetvalueMySQL/valuevalueRedis/value/set/property/beanutil:list idbookListvalue易筋经/valuevalue九阴真经/valuevalue九阳神功/value/util:list
/beans此外还可以将bean注入集合
4.6 注入外部属性文件
Spring提供了读取外部properties文件的机制可以将读到数据为bean的属性赋值
在src目录下创建user.properties配置文件
test.name小乔
user.age21
age18在bean1.xml文件中加入content命名空间及其xsd文件通过property-placeholder加载properties文件放在bean标签的外面其中location“classpath:user.properties” 的地址实际为 ./src/user.propertiesfile-encoding设置文件编码格式避免中文乱码。如果设置file-encodingUTF-8出现中文为问号请在编辑器中设置properties配置文件的格式 在IDEA打开Settings–Editor–File Encodings
!--加入content命令空间及其xsd文件--
beans xmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdcontext:property-placeholder locationclasspath:user.properties file-encodingUTF-8/
beans在bean中添加属性
property namename value${test.name}/property
property nameage value${user.age}/property发现个有意思的东西设置value“${user.name}”user.name是电脑的用户名不知道其他人会不会这样
4.7 注入属性的全部代码 User类
package springstudy;import java.util.List;
import java.util.Map;
import java.util.Set;public class User {//属性private String name;private int age;private int height;private int weight;private String address;private String degree;private Card card;//数组private String[] costumes;//list集合private ListString list;private ListString testlist;//map集合private MapString,String maps;//set集合private SetString sets;public User(int height, int weight) {this.height height;this.weight weight;}//set方法public void setName(String name) {this.name name;}public void setAge(int age) {this.age age;}public void setAddress(String address) {this.address address;}public void setDegree(String degree) {this.degree degree;}public void setCard(Card card) {this.card card;}public Card getCard() {return card;}public void setSets(SetString sets) {this.sets sets;}public void setCostumes(String[] costumes) {this.costumes costumes;}public void setList(ListString list) {this.list list;}public void setTestlist(ListString testlist) {this.testlist testlist;}public void setMaps(MapString, String maps) {this.maps maps;}public void add() {System.out.println(add...);}Overridepublic String toString() {return User{ name name \ , age age , height height , weight weight , address address \ , degree degree \ , card.money card.getMoney() \ };}//集合输出public void print() {System.out.println(---数组---);for (String i : costumes) {System.out.println(i);}System.out.println(---list---);for (String i : list) {System.out.println(i);}System.out.println(---sets---);for (String i : sets) {System.out.println(i);}System.out.println(---maps---);for (String key : maps.keySet()){String value (String) maps.get(key);System.out.println(key value);}System.out.println(---testlist---);for (String i : testlist) {System.out.println(i);}}
}bean1.xml文件
?xml version1.0 encodingUTF-8 ?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:utilhttp://www.springframework.org/schema/utilxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdcontext:property-placeholder locationclasspath:user.properties file-encodingUTF-8/!--配置User对象创建--bean iduser classspringstudy.User p:degree本科 autowirebyName!--通过set方法注入属性--
!-- property namename value西施/property--
!-- property nameagevalue18/value/property--property nameaddressnull//property!--通过构造器方法注入属性--constructor-arg nameheight value168/constructor-argconstructor-arg nameweight value90/constructor-arg!--注入bean--
!-- property namecard--
!-- bean idcard classspringstudy.Card--
!-- property nameid value1/property--
!-- property namemoney value1000/property--
!-- /bean--
!-- /property--
!-- property namecard refcard/property--
!-- property namecard.money value999/property--!--注入集合属性--!--数组类型属性注入--property namecostumesarrayvalue裙子/valuevalue汉服/value/array/property!--list 类型属性注入--property namelistlistvalue张三/valuevalue小三/value/list/propertyproperty nametestlist refbookList/property!--map 类型属性注入--property namemapsmapentry keyJAVA valuejava/entryentry keyPHP valuephp/entry/map/property!--set 类型属性注入--property namesetssetvalueMySQL/valuevalueRedis/value/set/propertyproperty namename value${test.name}/propertyproperty nameage value${user.age}/property/beanbean idcard classspringstudy.Card autowire-candidatefalseproperty nameid value1/propertyproperty namemoney value1000/property/beanbean idcard1 classspringstudy.Cardproperty nameid value1/propertyproperty namemoney value10000/property/bean!--list 集合类型属性注入--util:list idbookListvalue易筋经/valuevalue九阴真经/valuevalue九阳神功/value/util:list
/beansCard类
package springstudy;public class Card {private int id;private double money;public void setId(int id) {this.id id;}public void setMoney(double money) {this.money money;}public double getMoney() {return money;}
}
Test类其中System.out.println(user);会自动调用User类的toString方法
package springstudy; //自己的包
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {//加载Spring配置文件ApplicationContext context new ClassPathXmlApplicationContext(bean1.xml);User user context.getBean(user, User.class);System.out.println(user);user.print();}
}user.properties文件
test.name小乔
user.age21
age18不想创建那么多文件看起来太乱。。。