当前位置: 首页 > news >正文

广州做手机网站咨询网站建设补充协议模板

广州做手机网站咨询,网站建设补充协议模板,网站要怎么做才能获得市场份额,网站建设兼职工资文章目录 IoC控制反转依赖注入set注入注入外部Bean注入内部Bean注入简单类型通过注入方式实现javax.sql.DateSource接口测试简单类型 级联属性赋值#xff08;了解#xff09;注入数组注入List集合注入Set集合注入Map集合注入Properties注入null和空字符串不给属性赋值使用 注… 文章目录 IoC控制反转依赖注入set注入注入外部Bean注入内部Bean注入简单类型通过注入方式实现javax.sql.DateSource接口测试简单类型 级联属性赋值了解注入数组注入List集合注入Set集合注入Map集合注入Properties注入null和空字符串不给属性赋值使用 注入的值中含有特殊符号转义字符CDATE 构造注入 p命名空间注入c命名空间注入util命名空间基于XML的自动装配根据名称自动装配根据类型自动装配 Spring引入外部属性配置文件 IoC控制反转 控制反转是一种思想控制反转是为了降低程序耦合度提高程序扩展力达到OCP原则达到DIP原则。控制反转反转的是什么 将对象的创建权力交出去交给第三方容器负责将对象和对象之间关系的维护权交出去交给第三方容器负责 控制反转思想的实现方式依赖注入DI 依赖注入 依赖注入实现了控制反转思想 Spring通过依赖注入的方式来完成Bean管理的 Bean管理说的是Bean对象的创建以及Bean对象中属性的赋值或者叫做Bean对象之间关系的维护 依赖注入 依赖指的是对象和对象之间的关联关系。注入指的是一种数据传递行为通过注入行为来让对象和对象产生关系。 依赖注入常见的实现方式包括两种 第一种ser注入第二种构造注入 set注入 set注入基于set方法实现的底层会通过反射机制调用属性对应的set方法然后给属性赋值。这种方式要求属性必须对外提供set方法。 //UserDao package com.powernode.spring6.dao;public class UserDao {public void insert(){System.out.println(正在保存用户数据。);} }//UserService package com.powernode.spring6.service;import com.powernode.spring6.dao.UserDao;public class UserService {private UserDao userDao;// 使用set方式注入必须提供set方法。// 反射机制要调用这个方法给属性赋值的。public void setUserDao(UserDao userDao) {this.userDao userDao;}public void save(){userDao.insert();} }!-- spring.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iduserDaoBean classcom.powernode.spring6.dao.UserDao/bean iduserServiceBean classcom.powernode.spring6.service.UserServiceproperty nameuserDao refuserDaoBean//bean/beans//test public class DITest {Testpublic void testSetDI(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);UserService userService applicationContext.getBean(userServiceBean, UserService.class);userService.save();} }实现原理 通过property标签获取到属性名userDao 通过属性名推断出set方法名setUserDao 通过反射机制调用setUserDao方法给属性赋值 通过·property标签的name是属性名。 property标签的ref是要注入的bean对象的id通过ref属性来完成bean的装配这是bean最简单的一种装配方式。装配指的是创建系统组件之间关联的动作 底层时机调用了setUserDao方法所以需要确保这个方法存在 property标签的name是setUserDao方法名演变得到的。 演变规律 setUsername演变为usernamesetPassword演变为passwordsetUserDao演变为userDaosetUserService演变为userService 注入外部Bean ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iduserDaoBean classcom.powernode.spring6.dao.UserDao/bean iduserServiceBean classcom.powernode.spring6.service.UserServiceproperty nameuserDao refuserDaoBean//bean/beans外部Bean的特点bean定义到外面在property标签中使用ref属性进行注入。 好像没什么区别 注入内部Bean 内部注入Bean的方式在Bean标签中嵌套bean标签。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iduserServiceBean classcom.powernode.spring6.service.UserServiceproperty nameuserDaobean classcom.powernode.spring6.dao.UserDao//property/bean /beansTest public void testInnerBean(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-inner-bean.xml);UserService userService applicationContext.getBean(userServiceBean, UserService.class);userService.save(); }了解了解用的不多 注入简单类型 我们在注入的时候对象的属性是另一个对象但是并没有涉及简单类型 package com.powernode.spring6.beans;public class User {private int age;public void setAge(int age) {this.age age;}Overridepublic String toString() {return User{ age age };} } ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iduserBean classcom.powernode.spring6.beans.User!--如果像这种int类型的属性我们称为简单类型这种简单类型在注入的时候要使用value属性不能使用ref--!--property nameage value20/--property nameagevalue20/value/property/bean /beans//test Test public void testSimpleType(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-simple-type.xml);User user applicationContext.getBean(userBean, User.class);System.out.println(user); }给简单类型注入只要用value标签或者value属性给它赋上值就行了 注意不是refref译为参考后面加的是个类名参考这个类就是它最后生成的样子就是这个类参考这个类注入。 简单类型包括 基本数据类型基本数据类型对应的包装类String或其他的CharSequence子类Number子类Date子类这玩意不好用格式很复杂正常人记不住Enum子类URIURLTemporal子类LocalClass另外还包括以上简单类型对应的数组类型。 通过注入方式实现javax.sql.DateSource接口 //MyDataSource.java package com.powernode.spring6.beans;import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.logging.Logger;public class MyDataSource implements DataSource {private String driver;private String url;private String username;private String password;public void setDriver(String driver) {this.driver driver;}public void setUrl(String url) {this.url url;}public void setUsername(String username) {this.username username;}public void setPassword(String password) {this.password password;}Overridepublic String toString() {return MyDataSource{ driver driver \ , url url \ , username username \ , password password \ };}Overridepublic Connection getConnection() throws SQLException {return null;}Overridepublic Connection getConnection(String username, String password) throws SQLException {return null;}Overridepublic PrintWriter getLogWriter() throws SQLException {return null;}Overridepublic void setLogWriter(PrintWriter out) throws SQLException {}Overridepublic void setLoginTimeout(int seconds) throws SQLException {}Overridepublic int getLoginTimeout() throws SQLException {return 0;}Overridepublic Logger getParentLogger() throws SQLFeatureNotSupportedException {return null;}Overridepublic T T unwrap(ClassT iface) throws SQLException {return null;}Overridepublic boolean isWrapperFor(Class? iface) throws SQLException {return false;} }!-- spring-datasource.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iddataSource classcom.powernode.spring6.beans.MyDataSourceproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/spring/property nameusername valueroot/property namepassword value123456//bean /beans//test Test public void testDataSource(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-datasource.xml);MyDataSource dataSource applicationContext.getBean(dataSource, MyDataSource.class);System.out.println(dataSource); }测试简单类型 //类A package com.powernode.spring6.beans;import java.net.URI; import java.net.URL; import java.time.LocalDate; import java.util.Date; import java.util.Locale;public class A {private byte b;private short s;private int i;private long l;private float f;private double d;private boolean flag;private char c;private Byte b1;private Short s1;private Integer i1;private Long l1;private Float f1;private Double d1;private Boolean flag1;private Character c1;private String str;private Date date;private Season season;private URI uri;private URL url;private LocalDate localDate;private Locale locale;private Class clazz;// 生成setter方法// 生成toString方法 }enum Season {SPRING, SUMMER, AUTUMN, WINTER }!-- spring-all-simple-type.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean ida classcom.powernode.spring6.beans.Aproperty nameb value1/property names value1/property namei value1/property namel value1/property namef value1/property named value1/property nameflag valuefalse/property namec valuea/property nameb1 value2/property names1 value2/property namei1 value2/property namel1 value2/property namef1 value2/property named1 value2/property nameflag1 valuetrue/property namec1 valuea/property namestr valuezhangsan/!--注意value后面的日期字符串格式不能随便写必须是Date对象toString()方法执行的结果。--!--如果想使用其他格式的日期字符串就需要进行特殊处理了。具体怎么处理可以看后面的课程--property namedate valueFri Sep 30 15:26:38 CST 2022/property nameseason valueWINTER/property nameuri value/save.do/!--spring6之后会自动检查url是否有效如果无效会报错。--property nameurl valuehttp://www.baidu.com/property namelocalDate valueEPOCH/!--java.util.Locale 主要在软件的本地化时使用。它本身没有什么功能更多的是作为一个参数辅助其他方法完成输出的本地化。--property namelocale valueCHINESE/property nameclazz valuejava.lang.String//bean /beans//test Test public void testAllSimpleType(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-all-simple-type.xml);A a applicationContext.getBean(a, A.class);System.out.println(a); }需要注意的是 如果把Date当作简单类型的话日期字符串格式不能随便写随便写的话会报错这是个字符串类型不是date类型。格式必须符合Date的toString方法格式长这个臭样”Fri Sep 30 15:26:38 CST 2022“。如果我们提供一个这样的日期字符串2010-10-10在这里无法赋值给Date类型的属性的。 spring6之后当注入的是URL那么这个url字符串是会进行有效性检测的。如果是一个存在的url那就没问题。如果不存在就会报错 级联属性赋值了解 这个名字让我想起了mybatis里的级联赋值帮助完成一对多多对一映射的关联 直接看一下如何在spring配置bean文件里的代码我的评价是不如constructor-arg标签好用 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idclazzBean classcom.powernode.spring6.beans.Clazz/bean idstudent classcom.powernode.spring6.beans.Studentproperty namename value张三/!--要点1以下两行配置的顺序不能颠倒--property nameclazz refclazzBean/!--要点2clazz属性必须有getter方法--property nameclazz.name value高三一班//bean /beans这种方式注意 在spring配置文件中注意顺序在spring配置文件中clazz属性必须提供getter方法。 注入数组 当数组的元素是简单类型 //Person.java package com.powernode.spring6.beans;import java.util.Arrays;public class Person {private String[] favariteFoods;public void setFavariteFoods(String[] favariteFoods) {this.favariteFoods favariteFoods;}Overridepublic String toString() {return Person{ favariteFoods Arrays.toString(favariteFoods) };} }!-- spring-array-simple.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idperson classcom.powernode.spring6.beans.Personproperty namefavariteFoodsarrayvalue鸡排/valuevalue汉堡/valuevalue鹅肝/value/array/property/bean /beans//test Test public void testArraySimple(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-array-simple.xml);Person person applicationContext.getBean(person, Person.class);System.out.println(person); }当注入的元素是非简单类型一个订单中包含多个商品 //Goods.java package com.powernode.spring6.beans;public class Goods {private String name;public Goods() {}public Goods(String name) {this.name name;}public String getName() {return name;}public void setName(String name) {this.name name;}Overridepublic String toString() {return Goods{ name name \ };} }//Order.java package com.powernode.spring6.beans;import java.util.Arrays;public class Order {// 一个订单中有多个商品private Goods[] goods;public Order() {}public Order(Goods[] goods) {this.goods goods;}public void setGoods(Goods[] goods) {this.goods goods;}Overridepublic String toString() {return Order{ goods Arrays.toString(goods) };} }!-- spring-array.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idgoods1 classcom.powernode.spring6.beans.Goodsproperty namename value西瓜//beanbean idgoods2 classcom.powernode.spring6.beans.Goodsproperty namename value苹果//beanbean idorder classcom.powernode.spring6.beans.Orderproperty namegoodsarray!--这里使用ref标签即可--ref beangoods1/ref beangoods2//array/property/bean/beans//test Test public void testArray(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-array.xml);Order order applicationContext.getBean(order, Order.class);System.out.println(order); }要点 如果数组中的简单类型使用value标签直接赋值。如果数组是非简单类型使用ref标签赋值。 注入List集合 List集合的特性有序可重复 package com.powernode.spring6.beans;import java.util.List;public class People {// 一个人有多个名字private ListString names;public void setNames(ListString names) {this.names names;}Overridepublic String toString() {return People{ names names };} }?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idpeopleBean classcom.powernode.spring6.beans.Peopleproperty namenameslistvalue张三/valuevalue李四/valuevalue王五/valuevalue赵六/valuevalue钱七/valuevalue勾八/valuevalue勾八/valuevalue勾八/value/list/property/bean /beans//test Test public void testCollection(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-collection.xml);People peopleBean applicationContext.getBean(peopleBean, People.class);System.out.println(peopleBean); }注意 注入List集合的时候使用list标签如果List集合中是简单类型使用value标签反之使用ref标签 注入Set集合 Set集合的特性无序不可重复 //People.java package com.powernode.spring6.beans;import java.util.List; import java.util.Set;public class People {// 一个人有多个电话private SetString phones;public void setPhones(SetString phones) {this.phones phones;}//......Overridepublic String toString() {return People{ phones phones , names names };} } ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idpeopleBean classcom.powernode.spring6.beans.Peopleproperty namephonesset!--非简单类型可以使用ref简单类型使用value--value110/valuevalue110/valuevalue120/valuevalue120/valuevalue119/valuevalue119/value/set/property/bean /beans注意 使用set标签 set集合中元素是简单类型的使用value标签反之使用ref标签 注入Map集合 //People.java package com.powernode.spring6.beans;import java.util.List; import java.util.Map; import java.util.Set;public class People {// 一个人有多个住址private MapInteger, String addrs;public void setAddrs(MapInteger, String addrs) {this.addrs addrs;}//......Overridepublic String toString() {return People{ addrs addrs , phones phones , names names };}}!-- spring-coolection.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idpeopleBean classcom.powernode.spring6.beans.Peopleproperty nameaddrsmap!--如果key不是简单类型使用 key-ref 属性--!--如果value不是简单类型使用 value-ref 属性--entry key1 value北京大兴区/entry key2 value上海浦东区/entry key3 value深圳宝安区//map/property/bean /beans要点 使用map标签 如果key是简单类型使用key属性反之使用key-ref属性 如果value是简单类型使用value属性反之使用value-ref属性 注入Properties java.util.Properties继承java.uril.Hashtable所以Properties也是一个Map集合。 //People.java package com.powernode.spring6.beans;import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set;public class People {private Properties properties;public void setProperties(Properties properties) {this.properties properties;}//......Overridepublic String toString() {return People{ properties properties , addrs addrs , phones phones , names names };} }!-- spring-collection.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idpeopleBean classcom.powernode.spring6.beans.Peopleproperty namepropertiespropsprop keydrivercom.mysql.cj.jdbc.Driver/propprop keyurljdbc:mysql://localhost:3306/spring/propprop keyusernameroot/propprop keypassword123456/prop/props/property/bean /beans要点 使用props标签嵌套prop标签完成。 注入null和空字符串 注入空字符串使用value/或者value注入null使用null或者不为该属性赋值 //Vip.java package com.powernode.spring6.beans;public class Vip {private String email;public void setEmail(String email) {this.email email;}Overridepublic String toString() {return Vip{ email email \ };} }!-- spring-null,xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idvipBean classcom.powernode.spring6.beans.Vip!--空串的第一种方式--!--property nameemail value/--!--空串的第二种方式--property nameemailvalue//property/bean /beans//test Test public void testNull(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-null.xml);Vip vipBean applicationContext.getBean(vipBean, Vip.class);System.out.println(vipBean); }如何注入null呢 不给属性赋值 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idvipBean classcom.powernode.spring6.beans.Vip //beans使用null/ ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idvipBean classcom.powernode.spring6.beans.Vipproperty nameemailnull//property/bean/beans注入的值中含有特殊符号 XML中有5个特殊字符分别是、、、、 以上五个特殊符号在XML中会被特殊对待会被当作XML语法的一部分进行解析如果这些特殊符号直接出现在注入的字符串当中会报错 解决方案包括两种 第一种特殊符号使用转义字符代替第二种将含有特殊符号的字符串放到![CDATE[]]当中。因为放在CDATE区中的数据不会被XML文件解析器解析。 转义字符 //Math.java package com.powernode.spring6.beans;public class Math {private String result;public void setResult(String result) {this.result result;}Overridepublic String toString() {return Math{ result result \ };} }!-- spring-special.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idmathBean classcom.powernode.spring6.beans.Mathproperty nameresult value2 lt; 3//bean /beans//test Test public void testSpecial(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-special.xml);Math mathBean applicationContext.getBean(mathBean, Math.class);System.out.println(mathBean); }CDATE ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idmathBean classcom.powernode.spring6.beans.Mathproperty nameresult!--只能使用value标签--value![CDATA[2 3]]/value/property/bean/beans注意 使用CDATE时不能使用value属性只能使用value标签 构造注入 核心原理通过调用构造方法来给属性赋值。 //OrderDao.java package com.powernode.spring6.dao;public class OrderDao {public void deleteById(){System.out.println(正在删除订单。。。);} }//OrderService.java package com.powernode.spring6.service;import com.powernode.spring6.dao.OrderDao;public class OrderService {private OrderDao orderDao;// 通过反射机制调用构造方法给属性赋值public OrderService(OrderDao orderDao) {this.orderDao orderDao;}public void delete(){orderDao.deleteById();} }!-- spring.xml -- bean idorderDaoBean classcom.powernode.spring6.dao.OrderDao/ bean idorderServiceBean classcom.powernode.spring6.service.OrderService!--index0表示构造方法的第一个参数将orderDaoBean对象传递给构造方法的第一个参数。--constructor-arg index0 reforderDaoBean/ /bean//test Test public void testConstructorDI(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);OrderService orderServiceBean applicationContext.getBean(orderServiceBean, OrderService.class);orderServiceBean.delete(); }如果构造参数有两个那就需要将两个参数都加入到bean中利用constructor-arg子标签将构造的参数补齐 index属性名补齐参数对应位置用01方式对应参数的位置name属性名补齐参数对应位置用对应上构造参数值的名字赋值进去空也可以不指定参数的位置利用spring强大的功能让他自己对应进去位置可以不对应ref属性名指向要映射成的类 p命名空间注入 目的简化配置。 使用p命名空间注入的前提条件包括两个 第一在XML头部信息中添加p命名空间的配置信息 xmlns:phttp://www.springframework.org/schema/p第二p命名空间注入是基于setter方法的所以需要对应的属性提供setter方法。 //Customer.java package com.powernode.spring6.beans;public class Customer {private String name;private int age;public void setName(String name) {this.name name;}public void setAge(int age) {this.age age;}Overridepublic String toString() {return Customer{ name name \ , age age };} }!-- spring-p.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:phttp://www.springframework.org/schema/pxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idcustomerBean classcom.powernode.spring6.beans.Customer p:namezhangsan p:age20//beans//test Test public void testP(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-p.xml);Customer customerBean applicationContext.getBean(customerBean, Customer.class);System.out.println(customerBean); }p命名空间实际上是对set注入的简化 c命名空间注入 c命名空间是简化构造方法注入的 使用c命名空间的两个前提条件 第一需要在xml配置信息文件头部添加信息 xmlns:chttp://www.springframework.org/schema/c第二需要提供构造方法 //MyTime.java package com.powernode.spring6.beans;public class MyTime {private int year;private int month;private int day;public MyTime(int year, int month, int day) {this.year year;this.month month;this.day day;}Overridepublic String toString() {return MyTime{ year year , month month , day day };} }!-- spring-c.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:chttp://www.springframework.org/schema/cxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--bean idmyTimeBean classcom.powernode.spring6.beans.MyTime c:year1970 c:month1 c:day1/--bean idmyTimeBean classcom.powernode.spring6.beans.MyTime c:_02008 c:_18 c:_28//beans//test Test public void testC(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-c.xml);MyTime myTimeBean applicationContext.getBean(myTimeBean, MyTime.class);System.out.println(myTimeBean); }不管是p命名空间还是c命名空间注入的时候都可以注入简单类型以及非简单类型 util命名空间 使用util命名空间可以让配置复用 使用util命名空间的前提是在spring配置头文件添加配置信息 //MyDataSource1.java package com.powernode.spring6.beans;import java.util.Properties;public class MyDataSource1 {private Properties properties;public void setProperties(Properties properties) {this.properties properties;}Overridepublic String toString() {return MyDataSource1{ properties properties };} }//MyDataSource2.java package com.powernode.spring6.beans;import java.util.Properties;public class MyDataSource2 {private Properties properties;public void setProperties(Properties properties) {this.properties properties;}Overridepublic String toString() {return MyDataSource2{ properties properties };} }!-- spring-util.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdutil:properties idpropprop keydrivercom.mysql.cj.jdbc.Driver/propprop keyurljdbc:mysql://localhost:3306/spring/propprop keyusernameroot/propprop keypassword123456/prop/util:propertiesbean iddataSource1 classcom.powernode.spring6.beans.MyDataSource1property nameproperties refprop//beanbean iddataSource2 classcom.powernode.spring6.beans.MyDataSource2property nameproperties refprop//bean /beans//test Test public void testUtil(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-util.xml);MyDataSource1 dataSource1 applicationContext.getBean(dataSource1, MyDataSource1.class);System.out.println(dataSource1);MyDataSource2 dataSource2 applicationContext.getBean(dataSource2, MyDataSource2.class);System.out.println(dataSource2); }基于XML的自动装配 Spring还可以完成自动化的注入自动化注入又被称为自动装配。它可以根据名字进行自动装配也可以根据类型进行自动装配。 根据名称自动装配 //UserDao.java package com.powernode.spring6.dao;public class UserDao {public void insert(){System.out.println(正在保存用户数据。);} }package com.powernode.spring6.service;import com.powernode.spring6.dao.UserDao;public class UserService {private UserDao aaa;// 这个set方法非常关键public void setAaa(UserDao aaa) {this.aaa aaa;}public void save(){aaa.insert();} }!-- spring-autowire.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iduserService classcom.powernode.spring6.service.UserService autowirebyName/bean idaaa classcom.powernode.spring6.dao.UserDao//beans这个配置起到关键作用 UserService Bean中需要添加autowire“byName”表示通过名称进行装配。UserService类中有一个UserDao属性而UserDao属性的名字是aaa对应的set方法是setAaa正好和UserDao Bean的id1是一样的。这就是根据名称自动装配。 //test Test public void testAutowireByName(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-autowire.xml);UserService userService applicationContext.getBean(userService, UserService.class);userService.save(); }如果根据名称装配byName底层会调用set方法进行注入。 例如setAge对应的名字是agesetPassword对应的名字是passwordsetEmail对应的名字是email。 根据类型自动装配 //AccountDao.java package com.powernode.spring6.dao;public class AccountDao {public void insert(){System.out.println(正在保存账户信息);} }//AccountService.java package com.powernode.spring6.service;import com.powernode.spring6.dao.AccountDao;/*** author 动力节点* version 1.0* className AccountService* since 1.0**/ public class AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao accountDao;}public void save(){accountDao.insert();} }!-- spring-autowire.xml -- ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--byType表示根据类型自动装配--bean idaccountService classcom.powernode.spring6.service.AccountService autowirebyType/bean classcom.powernode.spring6.dao.AccountDao//beansTest public void testAutowireByType(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-autowire.xml);AccountService accountService applicationContext.getBean(accountService, AccountService.class);accountService.save(); }无论是byName还是byType在装配的时候都是基于set方法的。所以set方法是必须要提供的。提供构造方法是不行的。 当byType进行自动装配的时候配置文件中某种类型的Bean必须是唯一的不能出现多个。 Spring引入外部属性配置文件 mybatis可以将配置文件的信息单独写到一个文件中Spring也可以 第一步写一个数据源类提供相关属性 package com.powernode.spring6.beans;import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.logging.Logger;public class MyDataSource implements DataSource {Overridepublic String toString() {return MyDataSource{ driver driver \ , url url \ , username username \ , password password \ };}private String driver;private String url;private String username;private String password;public void setDriver(String driver) {this.driver driver;}public void setUrl(String url) {this.url url;}public void setUsername(String username) {this.username username;}public void setPassword(String password) {this.password password;}//...... }第二步在类路径下新创建jdbc.propertirs文件并配置信息 drivercom.mysql.cj.jdbc.Driver urljdbc:mysql://localhost:3306/数据库名 username账号 password密码在spring配置文件中引入context命名空间 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd/beans在spring中配置使用jdbc.properties文件 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdcontext:property-placeholder locationjdbc.properties/bean iddataSource classcom.powernode.spring6.beans.MyDataSourceproperty namedriver value${driver}/property nameurl value${url}/property nameusername value${username}/property namepassword value${password}//bean /beans//test Test public void testProperties(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-properties.xml);MyDataSource dataSource applicationContext.getBean(dataSource, MyDataSource.class);System.out.println(dataSource); }注意一个事情就是properties文件中的username在注入的时候会先查询windows本地的所以在调试的时候会发现username注入的值不是我们提供的值而是本地的一个值 建议properties文件加个前缀jdbc啥的
http://www.yutouwan.com/news/391928/

相关文章:

  • 沈阳网站建设设计网络公司网站
  • 常州网站建设基本流程定制网站与模板建站维护
  • 毕业设计代做网站哪个网站可以做3d
  • 网站的开发工具和运行环境做付费推广哪个网站好
  • 怎样用阿里云建设网站乐山市建设局官方网站
  • 备案网站的规则如何有效的推广宣传
  • 网站怎样做 文件签收网站上的logo怎么做
  • 深圳微商城网站制作公司生态建筑建设公司网站
  • 天津 网站 备案使用 ahrefs 进行 seo 分析
  • ui设计师做网站广州微型网站建设
  • 宜宾网站网站建设国外网站如何做推广
  • 长沙高新区住房和建设管理局网站室内设计师接私活的平台
  • 论文网站建设与运营南通优化网站排名
  • 邯郸做网站服务商wordpress多用途主题推荐
  • 如何通过后台管理在网站的logo后台上加链接网络服务商怎么联系
  • win7 搭建iss网站娄底高端网站建设
  • 成品ppt的网站免费直播有哪些深圳画册设计品牌
  • 网站建设流程文字稿wordpress 明星
  • 手机怎样建设网站昆明网站建设多少钱
  • 网站建设详细方案聊城网站建设培训班
  • 合浦网站建设售后软件网站开发
  • 织梦软件展示网站新手怎么优化网站
  • 腾讯公司做的购物网站网站左侧的导航是怎么做的
  • 门户网站建设探究wordpress 小说连载
  • 哪个网站可以做临时工建筑公司年度工作总结报告
  • 百度电脑版登录网站安顺北京网站建设
  • 网站的登录注册怎么做公关策划书模板范文
  • 做购物网站最开始没人怎么办东平县住房和建设局网站
  • 在哪建企业网站好鞍山黄殿满
  • 杭州高端品牌网站建设如何做好网站开发项目需求分析