文章目录

  • Spring5-Spring的基本配置
    • 三、Spring的基本配置
      • 1、别名
      • 2、Bean的配置
      • 3、import
    • 四、依赖注入
      • 1、构造器注入
      • 2、set方式注入(重点)
        • 依赖注入
        • 环境搭建
      • 3、扩展方式注入
      • 4、bean作用域
        • 单例模式(Spring默认机制)
        • 原型模式(每次从容器中get的时候都会产生一个新对象)
        • 其他模式

Spring5-Spring的基本配置

三、Spring的基本配置

1、别名

    <!--别名,起了别名可以通过别名来获取对象--><alias name="user" alias="userName"/>

2、Bean的配置

    <!--id:bean的唯一标识符,也就是相当于我们的对象名class: bean 对象所对应的全限定名,包名+类型name: 别名,相较于alias更高级:可以取多个别名,使用“ ”和“,”来分割--><bean id="user" class="com.lengzher.pojo.User" name="userName u1"><constructor-arg name="name" value="芥子"/></bean>

3、import

一般用于团队开发中使用,用来将多个配置文件合并为一个;

  • 在applicationContext.xml中import两个配置文件,这两个配置文件的配置就可以合并成一个了:
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><import resource="bean2.xml"></import><import resource="beans.xml"></import>
</beans>
  • 引用合并的配置文件,两个子级配置文件的配置都得以应用
public class MyTest {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");User user = (User)context.getBean("u1");user.show();}
}

四、依赖注入

1、构造器注入

    <bean id="user" class="com.lengzher.pojo.User"><constructor-arg name="name" value="芥子"/></bean>

2、set方式注入(重点)

依赖注入

依赖:bean对象的创建依赖于容器

bean对象中的所有属性,由容器来注入

环境搭建
  • 复杂类型
public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
  • 真实测试对象
public class Student {private String name;private Address address;private String[] books;private List<String> bobbies;private Map<String,String> card;private Set<String> games;private String wife;private Properties info;}
  • beans.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="com.lengzher.pojo.Address"></bean><bean id="student" class="com.lengzher.pojo.Student"><!--第一种,普通注入--><property name="name" value="芥子"></property><!--第二种,Bean注入,ref--><property name="address" ref="address"></property><!--第三种,数组--><property name="books"><array><value>金瓶梅</value><value>水浒传</value><value>三国演义</value></array></property><!--第四种,List--><property name="bobbies"><list><value>李逵</value><value>林冲</value><value>武松</value></list></property><!--第五种,Map--><property name="card"><map><entry key="身份证" value="1232132132132"/><entry key="电话" value="12321321323"/></map></property><!--第六种,Set--><property name="games"><set><value>守望先锋</value><value>lol</value><value>堡垒之夜</value></set></property><!--第七种,null--><property name="wife"><null></null></property><!--第八种,Properties--><property name="info"><props><prop key="学号">20123324</prop></props></property></bean>
</beans>
  • 测试
public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");System.out.println(student.toString());}
}

3、扩展方式注入

可以通过p命名空间和c命名空间来注入

<?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"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--p命令空间注入,可以直接注入属性的值:Properties--><bean id="user" class="com.lengzher.pojo.User" p:name="李冲" p:age="1" p:password="203233"></bean><!--c命令空间注入,通过构造器注入,construction-args--><bean id="user" class="com.lengzher.pojo.User"  c:name="李白" c:age="1000" c:password="12321"></bean>
</beans>
  • 注意

    • p命令和c命令空间不能直接使用需要导入约束
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    

4、bean作用域

Scope Description
singleton (Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
  • 单例模式(Spring默认机制)

<bean id="user" class="com.lengzher.pojo.User"  c:name="李白" c:age="1000" c:password="12321" scope="singleton"></bean>
  • 原型模式(每次从容器中get的时候都会产生一个新对象)

<bean id="user" class="com.lengzher.pojo.User"  c:name="李白" c:age="1000" c:password="12321" scope="prototype"></bean>
  • 其他模式

其余的request、session、application这些只能在web开发中使用到

Spring5-Spring的基本配置相关推荐

  1. Spring的@Configuration配置类-Full和Lite模式

    前言 各位小伙伴大家好,我是A哥.这是一篇"插队"进来的文章,源于我公众号下面的这句评论: 官方管这两种模式分别叫:Full @Configuration和lite @Bean m ...

  2. 在Spring Boot中配置web app

    文章目录 添加依赖 配置端口 配置Context Path 配置错误页面 在程序中停止Spring Boot 配置日志级别 注册Servlet 切换嵌套服务器 在Spring Boot中配置web a ...

  3. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  4. 如何让Spring Boot 的配置 “动” 起来?

    前言 对于微服务而言配置本地化是个很大的鸡肋,不可能每次需要改个配置都要重新把服务重新启动一遍,因此最终的解决方案都是将配置外部化,托管在一个平台上达到不用重启服务即可一次修改多处生效的目的. 但是对 ...

  5. spring外部化配置

    例如 1 <bean id="dataSource" 2 3 class="....." 4 5 p:username="aa" 6 ...

  6. Spring多数据源配置和使用

    Spring多数据源配置和使用 1.配置信息 <!--==============================bpt_mobdb数据库配置========================== ...

  7. Spring Boot 属性配置和使用

    spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot 系列 Spring Boot 入门 S ...

  8. Spring定时任务的配置

    spring的定时任务配置分为三个步骤:  1.定义任务  2.任务执行策略配置  3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="test ...

  9. springboot原生mysql写法_【Rainbond最佳实践】Spring Boot框架配置MySQL

    Rainbond开源软件介绍: Rainbond是国内首个开源的生产级无服务器PaaS. 深度整合基于Kubernetes的容器管理.多类型CI/CD应用构建与交付.多数据中心的资源管理等技术,提供云 ...

  10. spring mvc mysql配置_spring mvc配置数据库连接

    ACM 配置中心实战:Spring + MyBatis + Druid + ACM 很多基于 Spring MVC 框架的 Web 开发中,Spring + MyBatis + Druid 是一个黄金 ...

最新文章

  1. 这才是微服务拆分的正确姿势,值得学习!
  2. hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
  3. 实现DDD领域驱动设计: Part 2
  4. php毕业设计遇到的问题,常见问题_php毕业设计_php课程设计_php大作业_原创作品下载网...
  5. 不使用加减乘除实现加法
  6. 华为手机如何固定横屏_华为手机如何录屏?原来方法这么简单,手把手教你学会...
  7. 偷源代码!还和前东家一起投标……最终被判 3 年
  8. 路由包含#号导致的nginx_分布式实战:Nginx缓存之OpenResty部署
  9. win7 vmware虚拟机 中 mac系统无法上网
  10. 在Eclipse中创建java类的注释模板
  11. CUDA编程-02: 初识CUDA编程
  12. 是否优化更新主题浏览量:_主题306:能力规划
  13. mysql服务注册表删除_怎么彻底删除mysql服务(清理注册表)详解
  14. 两个PDF比较标出差异_怎样核对两份word文档内容差异?我用2小时,同事仅用2分钟搞定...
  15. 云开发数据库update函数控制台显示更新成功,但数据库中的数据并没有更新(已解决)
  16. Xilinx 文件的编写
  17. django实现树形菜单
  18. 一步一步实现中后台管理平台模板-13-解决IE浏览器兼容性问题
  19. 虚拟机中新增磁盘空间并开机自动挂载
  20. 中国富豪的七条发家路--颇显中国特色

热门文章

  1. arcgis 二次开发学习笔记(一):了解二次开发有关的软件及其之间的关系
  2. 社会工程学之钓鱼attack常见手段和方法
  3. 用Go语言 实现的数学相关案例汇总 (Golang经典编程案例)
  4. 解决闪讯升级后猎豹不能用的问题
  5. 华为彭红华:700MHz!两大创新不可少
  6. 智云通CRM:获得客户承诺的法宝之一——选择、假设与赞美
  7. 网络安全——使用反弹木马进行提权获取主机Shell
  8. 期末实训作业C语言实现银行管理系统
  9. cocos2D(三)---- 第一个cocos2d程序的代码分析
  10. buuctf Had a bay day