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

聊城有什么网站制作公司asp网站开发模板

聊城有什么网站制作公司,asp网站开发模板,四川省住房和城镇建设官方网站,wordpress用户名的要求前言#xff1a;上一篇记录了通过XML文件来定义Bean对象#xff0c;这一篇将记录通过注解和配置类的方式来定义Bean对象。 核心注解#xff1a; 定义对象#xff1a;Component,Service,Repository,Controller 依赖注入#xff1a; 按类型#xff1a;Autowired 按名称上一篇记录了通过XML文件来定义Bean对象这一篇将记录通过注解和配置类的方式来定义Bean对象。 核心注解 定义对象Component,Service,Repository,Controller 依赖注入 按类型Autowired 按名称Resource或者使用AutowiredQualifier Resource需要导入下面的依赖因为从JDK9-17移除了javax的包 dependency groupIdjavax.annotation/groupId artifactIdjavax.annotation-api/artifactId version1.3.2/version /dependency 作用域Scope 生命周期PostConstruct,PreDestroy 一、注解方式定义Bean对象 定义Bean对象的注解有4个分别是ComponentServiceRepositoryController这四个注解的功能都是一样的唯一的区别就是名字不从。 这几个注解一般按照这种方式使用 Component 用于实体类的Bean对象定义 Service 用于接口实现类的Bean对象定义 Repository 用于读取数据库的DAO Bean对象定义 Controller 用于控制层的Bean对象定义 此外对于不同的分层使用不同的注解一方面可以使得层级更加分明另一方面后续Spring可以依据注解的名称进行灵活操作。 定义Bean注入 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;import javax.annotation.Resource; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Set;Component PropertySource(test.properties) public class Employee {Value(karry)private String name;Value(0)private Integer gender;//0女 1男Value(10000.0)private Double salary;Autowiredprivate Car car;//开的什么车Resourceprivate Car car2;Resource(name car)private Car car3;Qualifier(car)Autowiredprivate Car car4;Autowiredprivate ListCar carList;Autowiredprivate SetCar carSet;Value(#{${my.map}})private HashMapString, String strMap;Value(#{${my.set}.split(,)})private SetString strSet;Value(#{${my.set}})private SetString strSet2;Value(#{${my.str}.split(,)})private ListString strList;Value(${my.str})private ListString strList2;Value(${my.str})private String[] strArr;public void showInfo(){System.out.println(name: name gender: gender salary: salary);System.out.println( car: car);System.out.println( car2: car2);System.out.println( car3: car3);System.out.println( car4: car4);System.out.println(carList: carList size: carList.size());System.out.println(carSet: carSet size: carSet.size());System.out.println(strMap: strMap size: strMap.size());System.out.println(strSet: strSet size: strSet.size());System.out.println(strSet2: strSet2 size: strSet2.size());System.out.println(strList: strList size: strList.size());System.out.println(strList2: strList2 size: strList2.size());System.out.println(strArr: Arrays.toString(strArr) size: strArr.length);}} import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;Component public class Car {Value(red)private String color;Value(保时捷)private String name;Value(600)private Integer speed;public Car() {}public void setColor(String color) {this.color color;}public void setName(String name) {this.name name;}public void setSpeed(Integer speed) {this.speed speed;}Overridepublic String toString() {return Car{ color color \ , name name \ , speed speed };}public void showInfo(){System.out.println(color: color name: name speed: speed);} } 测试类 package com.xlb;import com.xlb.bean.Employee; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean {public static void main(String[] args) {test1();}public static void test1(){ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);Employee emp ctx.getBean(employee, Employee.class);emp.showInfo();} } 配置文件 ?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdcontext:component-scan base-packagecom.xxx.bean/ /beanstest.propertiest文件 my.setfoo,bar my.listfoo,bar my.map{foo: bar,foo2: bar2} my.strfoo,bar输出结果 从输出结果我们可以看出以下几点 1.通过Component成功定义了Bean对象也可以使用ServiceRepositoryController等注解来定义Bean对象具体使用哪个可以根据当前的业务层级来确定。 2.对于普通类型包装类型或String的属性我们通过Value注解进行依赖注入。 3.对于引用类型的属性如Car我们通过AutoWired注解进行注入。 4.对于数组类型的属性数组里的元素为String或者其他包装类型通过Value注解并且各元素间使用逗号分隔即可以成功将数据注入到数组中。 4.1 对于集合类型的属性集合里的元素为String或者其他包装类型通过Value注解并且各元素间使用逗号分隔此外需要利用SPEL表达式即在后面加split(‘,’)来切分元素【注其中切分的符号不一定是逗号和注入元素间的符号统一即可】 5.使用注解注入Bean对象时我们需要在配置文件中添加注解的扫描路径。即 context:component-scan base-package“com.xxx.bean”/这句话来标识我们包扫描的路径 6.在注入引用类型的对象时我们可以使用AutowiredAutowiredQualifier(“car”)ResourceResource(name “car”)其中 6.1 Autowired为按类型注入 6.2 AutowiredQualifier(“car”)为按名称注入名称即为Qualifier(“car”)中指定的名称这里名称为car 6.3 Resource为按名称注入名称为注解内name的值如果不写默认是该注解所注解的变量的名称 6.4 Resource(name “car”)为按名称注入名称即为name指定的名称 6.5 AutowiredQualifier(“car”) Resource(name “car”) 7.注入map类型的属性时不需要使用split进行切分。 二、配置类方式定义Bean对象 2.1 环境准备 bean对象 package com.xlb.bean;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;import javax.annotation.Resource; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Set;Component(employee2) PropertySource(test.properties) public class Employee2 {Value(karry)private String name;Value(0)private Integer gender;//0女 1男Value(10000.0)private Double salary;Resourceprivate Car2 car2;Resource(name car2)private Car2 car3;Qualifier(car2)Autowiredprivate Car2 car4;Autowiredprivate ListCar2 carList;Autowiredprivate SetCar2 carSet;Value(#{${my.map}})private HashMapString, String strMap;Value(#{${my.set}.split(,)})private SetString strSet;Value(#{${my.set}})private SetString strSet2;Value(#{${my.str}.split(,)})private ListString strList;Value(${my.str})private ListString strList2;Value(${my.str})private String[] strArr;public void showInfo(){System.out.println(name: name gender: gender salary: salary);System.out.println( car2: car2);System.out.println( car3: car3);System.out.println( car4: car4);System.out.println(carList: carList size: carList.size());System.out.println(carSet: carSet size: carSet.size());System.out.println(strMap: strMap size: strMap.size());System.out.println(strSet: strSet size: strSet.size());System.out.println(strSet2: strSet2 size: strSet2.size());System.out.println(strList: strList size: strList.size());System.out.println(strList2: strList2 size: strList2.size());System.out.println(strArr: Arrays.toString(strArr) size: strArr.length);}} package com.xlb.bean;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;Component(car2) public class Car2 {Value(red)private String color;Value(保时捷)private String name;Value(600)private Integer speed;public Car2() {}public void setColor(String color) {this.color color;}public void setName(String name) {this.name name;}public void setSpeed(Integer speed) {this.speed speed;}Overridepublic String toString() {return Car{ color color \ , name name \ , speed speed };}public void showInfo(){System.out.println(color: color name: name speed: speed);} } 配置类 package com.xlb.config;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;Configuration ComponentScan(com.xlb.bean) public class SpringConfig {} test.properties文件 my.setfoo,bar my.listfoo,bar my.map{foo: bar,foo2: bar2} my.strfoo,bar测试类 package com.xlb;import com.xlb.bean.Employee; import com.xlb.bean.Employee2; import com.xlb.config.SpringConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean2 {public static void main(String[] args) {test1();}public static void test1(){ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);Employee2 emp ctx.getBean(employee2, Employee2.class);emp.showInfo();} } 测试结果 从输出结果可以看到可以正常输出这个和上面介绍的通过注解实现的方式基本一样唯一的区别就是在测试类启动时我们是通过配置类启动的。 2.2 配置类中通过Bean注解定义Bean对象 首先注释掉通过Component注解创建的对象 然后在SpringConfig配置类中添加返回Bean对象 的代码 package com.xlb.config;import com.xlb.bean.Car2; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;Configuration ComponentScan({com.xlb.bean, com.xlb.config}) public class SpringConfig {Bean(car2)public Car2 buildCar(){Car2 car new Car2();car.setColor(blue);car.setName(梅赛德斯-迈巴赫);car.setSpeed(600);return car;} } 测试结果 可以看到在SpringConfig配置类里定义的Bean对象成功输出了。
http://www.pierceye.com/news/709140/

相关文章:

  • h5模板下载有哪些网站南京高端网站制作公司
  • 户外旅游网站模板佛山网络优化推广公司
  • 海南住房和城乡建设网站技术支持 重庆网站
  • 网站图片展示代码怎样给响应式网站提速
  • 学校 网站建设 招标广而告之微信推广平台
  • 企业如何通过地方网站宣传网站中国建设银行招聘官网
  • 上海品牌网站建设公网站的开发与建设项目
  • 做网站的艰辛电子商务网站建设与维护概述
  • 织梦网站做关键词网站开发到上线的过程
  • 威海千淼网站建设北京知名广告公司有哪些
  • wordpress多站点可视化wordpress主题flarum
  • 网站免费虚拟主机申请成华区微信网站建设公
  • 机械制造设备类企业网站织梦模板网站模板 博客
  • js跳转网站怎么做网络营销方式单一的原因
  • 做网站的职责做章的网站
  • 万网建设网站wordpress的ftp設置
  • 网站建设a云世家宋南南电子商务网站的开发方式
  • 水利工程建设监理网站美食网站建设总结
  • 中化建工北京建设投资有限公司网站南沙网站建设方案
  • 东莞网站制作网站死链是什么
  • 网站开发哪种语言更安全seopc流量排名官网
  • 中国站长之家域名查询深圳html5网站推广价格
  • 商业网站建设案例视频上海猎头公司哪家好
  • 如何开个人网站seo诊断站长
  • wordpress rss 订阅乐陵seo推广
  • 公司做一个网站企业建设3D网站
  • 自己做的网站别人打不开网络系统建设与运维职业技能等级证书
  • 签名能留链接的网站数据库网站建设公司
  • 网站权重不够高 导致创建网站销售产品
  • 中国建设报网站做网站如何文字链接文字